-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/ha discovery #27
Feature/ha discovery #27
Conversation
Thanks for the PR. It will take a couple of days before I can get around to reviewing this. From a quick glance, it seems this PR contains a couple of things that likely should be seperate PRs, and the testing code perhaps should not bleed into the implementation. That being said, this isn't a comprehensive review and just an initial impression. Thanks for the contribution, please keep up the great work. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks again for filing this PR. I think it contains very interesting and worthwhile ideas. Since the PR is rather big and contains several unrelated changes, I'd suggest to do the following:
- remove the development related files and directories
- the testing code should be done in a distinct PR without having to branch between test and non-test code. I.e. the implementation should be oblivious to the fact whether it is run in a test environment or not.
- move structs into their own source file
- consider using
protobuf_json
crate instead of the rolling your own conversion to json - implement the auto discovery in a non-breaking way
While the above may seem like a lot, I think the PR is a great first stab into the right direction. The intent for the PR is great, please keep up the good work.
.devcontainer/Dockerfile
Outdated
@@ -0,0 +1 @@ | |||
FROM rust:buster |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The files in .devcontainer should not be part of this PR.
.vscode/launch.json
Outdated
@@ -0,0 +1,21 @@ | |||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The files in .vscode/*
should not be part of this PR.
src/inverter.rs
Outdated
@@ -39,7 +40,42 @@ impl<'a> Inverter<'a> { | |||
} | |||
} | |||
|
|||
pub fn update_state(&mut self) -> Option<HMSStateResponse> { | |||
pub fn mock_state(&mut self) -> Option<HMSStateResponse> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test code should be separated from the implementation. This could be done in a tool simulating the behavior of an actual inverter. Issue #11 is tracking such a feature. This should be dropped from the PR.
src/main.rs
Outdated
mqtt.publish(&r); | ||
} | ||
|
||
// TODO: this has to move into the Inverter struct in an async implementation | ||
thread::sleep(Duration::from_millis(REQUEST_DELAY)); | ||
if !cli.test{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See above for the comment that API simulation should implemented in #11.
src/mqtt.rs
Outdated
impl HMSStateResponse { | ||
fn get_model(&self) -> String { | ||
// TODO: identify model from dtu_sn | ||
"HMS-800W-T2".to_string() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not necessarily the inverter. While tested, the tool likely works with all devices of the HMS-XXXX-2T series.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, since we currently don't have a way to determine the exact model name, I've removed the mention of 800W-2T
src/mqtt.rs
Outdated
} | ||
|
||
|
||
impl DeviceConfig { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
consider moving the struct into a separate source file
src/mqtt.rs
Outdated
identifiers, | ||
manufacturer: "Hoymiles".to_string(), | ||
/// Rust compiler sets the CARGO_PKG_VERSION environment from the Cargo.toml . | ||
sw_version: env!("CARGO_PKG_VERSION").to_string(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is arguably not part the version of the inverter device
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, but I thought it makes sense to log the version of the publisher here (we can also change it in the future in case we figure out a way to extract the sw version of the inverter)
src/mqtt.rs
Outdated
/// https://developers.home-assistant.io/docs/core/entity/sensor/ | ||
/// | ||
#[derive(Serialize)] | ||
pub struct SensorConfig { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
consider moving this to a separate source file
src/mqtt.rs
Outdated
} | ||
|
||
|
||
fn create_sensor_configs(hms_state: &HMSStateResponse, state_topic: &str) -> Vec<SensorConfig> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this be a ::from(...)
member function rather than a free factory?
self.client | ||
.subscribe("hms800wt2/pv_current_power", QoS::AtMostOnce) | ||
.unwrap(); | ||
match self.client.publish( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this seems like a breaking change.
Thanks for your comprehensive review!
I will create a separate PR for this
Good point. Didn't have time to address this properly so just hacked in something quickly that allowed me to do some minimal testing during the addon development. I will remove the testing related code for now
Will do. Wasn't sure how to structure it or whether it's necessary to split the mqtt implementation up in two parts, one for home assistant, one without home assistant support.
Thought about this as well, but since the values are scaled during the serialization, I decided to just do it the manual way. I assume the protobuf schemas won't often if at all, so I hope that's okay.
The breaking change is unfortunate. But apart from home assistant support, another upside is that it would allow utilizing multiple inverters in the future (part of the serial number is added to the topic). Another part of the breaking change is that the whole update is sent json-encoded to a single topic instead of splitting it to multiple topics. This should make it more efficient in combination with home assistant (and possibly other endpoints) |
That's a good point. Thanks for the clarification. I think the json-encoded single topic could be done independently, is that right? If so, we could first add things, and consider the breaking change at a later point. Would this make sense? |
Yes, we could change the home assistant device configurations and publish on separate topics to delay the breaking change. But if we're certain it's going to happen sooner or later (and I think supporting multiple inverters is a enough good reason), delaying the breaking change will only cause more users to be affected. While we're at it we should also remove the "800W-2T" references, e.g. from the topic, to prevent a future breaking change. |
Ok, that's a convincing argument. Let's do the breaking change now, and aim at the same time to minimize the size of this PR. |
73cd9fb
to
d4e0540
Compare
Very cool. Please have a bit of patience, since it will again take a couple of days before I can get to it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- run
cargo fmt
- run
cargo clippy
- move non-functional changes into distinct PRs to separate concerns
This is looking really close. Couple of remarks on the PR that shouldn't take too long to resolve. Then we can get this in.
To give some perspective of where I am taking the architecture of the tool are probably helpful. Similar to the idea of supporting several inputs, I'd like the tool to support several output channels (for a lack of a better name). I.e. there's a simple MQTT publisher, there's the HA autodiscovery and publishing of this PR, and other outputs like InfluxDB and perhaps others. The user will be able to configure where to publish.
Once this PR is ready, I want to start implementing these channels by building right on top of this branch and then merge the two in short succession once both are ready.
I did a very hasty implementation of output channels (cf. #27 (review)) on top of your PR: 7460c0e |
@@ -1,9 +1,12 @@ | |||
use std::{thread, time::Duration}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in light of the output channel feature (see below) it probably makes sense to rename this file to home_assistent.rs
since it will implement the output channel to a HA instance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will merge changes and do a couple of minor changes on top. Thanks so much for the contribution
Sorry I was preoccupied with my day job and family. I'll have a look at the output channels PR |
No worries. Life comes first, hacking on code second. 😉 |
This is a work in progress, but wanted to get some early feedback. The main contributions of this PR are:
Other notable changes / additions:
Note that this is my first time developing in rust and there's probably a lot of things to improve / things that an experienced rust developer would've probably done differently. I'm happy to receive some constructive feedback and improve where necessary :-)
TODO: