Fix consumer modules - #3865
Conversation
e751745 to
bcff2e1
Compare
There was a problem hiding this comment.
Pull request overview
This PR standardizes and tightens typing across many consumer/device modules (notably around Optional[...] usage and config defaults) to improve static analysis and reduce ambiguous None handling, with a few small behavioral tweaks.
Changes:
- Add/adjust
Optional[...]annotations for lazily-initialized client/session variables across many consumers. - Refine multiple consumer
config.pyconstructors to use non-optional primitive defaults (e.g.,port: int = 502) and markconfigurationparameters asOptional[...]. - Minor functional updates: Shelly generation typing cleanup, AVM consumer now raises when the configured device name is not found, my-PV consumers switched from
write_registerstowrite_register.
Reviewed changes
Copilot reviewed 39 out of 39 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/modules/devices/shelly/shelly/status_handler.py | Tightens get_generation() return typing (always returns an int generation). |
| packages/modules/consumers/viessmann/viessmann_heat_pump/consumer.py | Adds Optional typing for the lazily initialized Modbus client. |
| packages/modules/consumers/viessmann/viessmann_heat_pump/config.py | Makes port/modbus_id non-optional defaults; configuration becomes Optional[...]. |
| packages/modules/consumers/stiebel/stiebel/consumer.py | Adds Optional typing for Modbus client and sim counter. |
| packages/modules/consumers/stiebel/stiebel/config.py | Makes port/modbus_id non-optional defaults; configuration becomes Optional[...]. |
| packages/modules/consumers/solvis/solvis/consumer.py | Adds Optional typing for Modbus client and sim counter. |
| packages/modules/consumers/solvis/solvis/config.py | Makes port/modbus_id non-optional defaults; configuration becomes Optional[...]. |
| packages/modules/consumers/solarfocus/vampair/consumer.py | Adds Optional typing for Modbus client and sim counter. |
| packages/modules/consumers/solarfocus/vampair/config.py | Makes port/modbus_id non-optional defaults; configuration becomes Optional[...]. |
| packages/modules/consumers/shelly/shelly_pm/consumer.py | Adds Optional typing for sim counter/model and explicit int for generation. |
| packages/modules/consumers/shelly/shelly_pm/config.py | Makes factor/phase non-optional defaults; configuration becomes Optional[...]. |
| packages/modules/consumers/shelly/shelly_em/consumer.py | Adds Optional typing for sim counter and explicit int for generation. |
| packages/modules/consumers/shelly/shelly_em/config.py | Makes factor/phase non-optional defaults; configuration becomes Optional[...]. |
| packages/modules/consumers/ratiotherm/ratiotherm/consumer.py | Adds Optional typing for Modbus client and sim counter; minor formatting tweak. |
| packages/modules/consumers/ratiotherm/ratiotherm/config.py | Makes port/modbus_id non-optional defaults; configuration becomes Optional[...]. |
| packages/modules/consumers/ovum/ovum/consumer.py | Adds Optional typing for Modbus client and sim counter. |
| packages/modules/consumers/ovum/ovum/config.py | Makes port/modbus_id non-optional defaults; configuration becomes Optional[...]. |
| packages/modules/consumers/nibe/nibe_s_series/consumer.py | Adds Optional typing for Modbus client and sim counter. |
| packages/modules/consumers/nibe/nibe_s_series/config.py | Makes modbus_id non-optional default; configuration becomes Optional[...]. |
| packages/modules/consumers/mystrom/mystrom/consumer.py | Adds Optional typing for HTTP session and sim counter. |
| packages/modules/consumers/mystrom/mystrom/config.py | Marks configuration as Optional[...]. |
| packages/modules/consumers/my_pv/elwa_e/consumer.py | Renames “STATUS” to “STATE” mapping and switches to write_register for setpoint writes. |
| packages/modules/consumers/my_pv/elwa_e/config.py | Makes port/modbus_id non-optional defaults; configuration becomes Optional[...]. |
| packages/modules/consumers/my_pv/acthor/consumer.py | Adds Optional typing and switches to write_register for setpoint writes. |
| packages/modules/consumers/my_pv/acthor/config.py | Makes several fields non-optional defaults; configuration becomes Optional[...]. |
| packages/modules/consumers/lambda_/lambda_/consumer.py | Adds Optional typing for Modbus client and sim counter. |
| packages/modules/consumers/lambda_/lambda_/config.py | Makes port/modbus_id non-optional defaults; configuration becomes Optional[...]. |
| packages/modules/consumers/idm/idm/consumer.py | Adds Optional typing for Modbus client and sim counter. |
| packages/modules/consumers/idm/idm/config.py | Makes port/modbus_id non-optional defaults; configuration becomes Optional[...]. |
| packages/modules/consumers/generic/json/consumer.py | Adds Optional typing for session/sim counter and corrects update() return type annotation. |
| packages/modules/consumers/generic/json/config.py | Makes many config fields explicitly Optional[str]; configuration becomes Optional[...]. |
| packages/modules/consumers/generic/http/consumer.py | Adds Optional typing for session/sim counter and request/post callables; minor call-site adjustment. |
| packages/modules/consumers/generic/http/config.py | Makes many config fields explicitly Optional[str]; configuration becomes Optional[...]. |
| packages/modules/consumers/generic/dac/consumer.py | Makes Modbus client Optional[...]; comment capitalization + minor formatting. |
| packages/modules/consumers/generic/dac/config.py | Makes port/modbus_id non-optional defaults; configuration becomes Optional[...]. |
| packages/modules/consumers/avm/avm/consumer.py | Types ain as Optional[str] and raises if the configured device name is not found. |
| packages/modules/consumers/avm/avm/config.py | Changes session_mtime type to Optional[float]; configuration becomes Optional[...]. |
| packages/modules/consumers/askoma/askoheat/consumer.py | Adds Optional typing for Modbus client and sim counter. |
| packages/modules/consumers/askoma/askoheat/config.py | Makes port/modbus_id non-optional defaults; configuration becomes Optional[...]. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
bcff2e1 to
d1e5980
Compare
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
| client: Optional[ModbusTcpClient_] = None | ||
| sim_counter: Optional[SimCounterConsumer] = None |
There was a problem hiding this comment.
| client: Optional[ModbusTcpClient_] = None | |
| sim_counter: Optional[SimCounterConsumer] = None | |
| client: ModbusTcpClient_ | |
| sim_counter: SimCounterConsumer |
So sind die pyright-Meldungen komplett verschwunden. Und ob client gar nicht oder mit None initialisiert ist, kommt beim Aufruf von read_input_registers in beiden Fällen eine Exception und intializer sollte sowieso vorher aufgerufen werden.
There was a problem hiding this comment.
Das Ziel der Anpassung war ja nicht, die Meldungen "verschwinden" zu lassen, sondern den Code robuster zu machen. Wenn klar ist, dass client und sim_counter zuerst None sind, wird das auch in der IDE entsprechend mit Hinweisen versehen und es kann darauf reagiert werden.
In diesem Modul kommt es z.B. in Zeile 28 zu einer Warnung, dass client ggf. Noneist. Ohne weitere Anpassung wird eine Exception geworfen, die irgendetwas von None hat keine Eigenschaft XYZ (sinngemäß) erzählt. Damit kann außer einem Entwickler niemand etwas anfangen.
Es wäre nach meiner Meinung sinnvoller, wenn vorab auf client is None geprüft wird und dann eine Klartextmeldung kommt, die z.B. auch der Support versteht. Das mag nicht unbedingt "Python Style" sein, hätte aber durchaus Vorteile. Ich vermute mal, dass nicht ohne Grund angemeckert wird, dass client iin Zeile 28 None sein könnte.
* fix configuration type hints * fix consumer type hints and errors * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
No description provided.