Refactor Modbus switch to provide a base for other entities#33551
Refactor Modbus switch to provide a base for other entities#33551MartinHjelmare merged 8 commits intohome-assistant:devfrom Lutemi:feature/modbus-fan-and-light
Conversation
|
Hey there @adamchengtkc, @janiversen, mind taking a look at this pull request as its been labeled with a integration ( |
|
The CI issues should be fixed so it should run cleanly if you rebase https://developers.home-assistant.io/docs/development_catching_up/ |
janiversen
left a comment
There was a problem hiding this comment.
I have no problem letting fan/light be defined as mere name changes to switch, but the documentation needs to be updated to reflect these new entities.
I would also like to see test cases, however with #33447 not merged, it is something that can come later
|
@janiversen thanks for the comments. To sum up, the main purpose of this change is to define bare implementations for Modbus lights and fans. Albeit they are technically just switches at the moment, they will appear differently in the UI. Also, we can easily extend them later. The documentation was already written and approved (PR home-assistant/home-assistant.io#12640). I plan to write unit tests, but first I wanted to finally upstream my local changes. Now I maintain my own fork of Modbus platform, and it takes time to adapt my code. When it will be on-par with upstream, I can start focusing on tests and/or other features. |
|
I understand what you are doing, and I find it to be the right way, push the few changes and I will approve. Sorry for having overlooked the documentation. |
|
@janiversen no problem. If the implementation will be better thanks to our discussions, I'm all in for it :) |
|
Approved ready to go. I am trying to see if I can get a bit of help with getting PR #33447 merged, since that will allow you to make test cases. |
|
There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days. |
|
Still working on it. |
|
@janiversen please take a look. I reworked Modbus switch setup to async. Light and Fan entities inherit from Switch class, so I decided to do the changes in this PR. Functionality verified manually at the moment. |
|
What is the status of this PR ? |
|
@janiversen from my point of view it's ready. I already use this code in my setup. Modbus code is still synchronous but components (switch, fan and light) are now initialized asynchronously. It would be great to start the reviewing process and to get this code merged. Any tips, who should I tag or where should I write to move this thing forward? Thanks! |
|
First if all you need to resolve the 2 failing CI parts, then I will do a review. |
|
@janiversen OK, I will take a look. |
|
There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days. |
|
Still working on it. |
|
There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days. |
|
@makuser it looks like I will be refactoring the Fan component anyway. If you can provide your code, perhaps I could add support also for controlling speed. If your changes are public, please leave a link to your repository. And if not, maybe you could provide sample of your Fan configuration with allowed values. Then we can push all required changes in a single PR. |
|
| return self._available | ||
|
|
||
|
|
||
| class ModbusCoilSwitch(ModbusBaseSwitch): |
There was a problem hiding this comment.
We should inherit from SwitchEntity somewhere in the switch entities.
There was a problem hiding this comment.
I guess the original code was written before SwitchEntity even existed. Anyway, the SwitchEntity inherits from the ToggleEntity. I will modify the base class to inherit directly from the SwitchEntity. This should solve our problem.
I will let you know.
There was a problem hiding this comment.
If the base class inherits from SwitchEntity we cannot inherit from the base class in other platforms than switch.
There was a problem hiding this comment.
Good point. Probably I should add the inheritance into the ModbusCoilSwitch and ModbusRegisterSwitch. This would solve our problem, and yet Fan and Light can still inherit from it.
There was a problem hiding this comment.
that would be my preference.
There was a problem hiding this comment.
Just working on a change now. I will test it real quick, then we'll run automation tests, and we should be good to go.
There was a problem hiding this comment.
Code still works, and tests pass.
|
Ready to merge? |
Yes, if @janiversen has no objections. |
|
No objections look good to me, please merge. |
|
Great! In the upcoming days, I will create PRs for Fan and Light, and I will reference this PR, where we discussed a lot already.
@makuser you mentioned that you have your custom Fan implementation already. It would really help, if you could provide sample of your configuration. Especially I'm interested in the allowed values for the speed. My Fan doesn't have speed control, so I can only code the feature. If you can help with that, you won't need to create your own PR later :) |
Yes, indeed. Here are a couple of important registers, we set the speed to vol.Optional(CONF_SETPOINT_REGISTER, default=0): cv.positive_int,
vol.Optional(CONF_ACTUAL_POSITION_REGISTER, default=2): cv.positive_int,
vol.Optional(CONF_ACTUAL_FLOW_REGISTER, default=3): cv.positive_int,
vol.Optional(CONF_ACTUAL_FLOW_VOLUME_REGISTER, default=4): cv.positive_int,
vol.Optional(CONF_ACTUAL_PRESSURE_REGISTER, default=5): cv.positive_int,
vol.Optional(CONF_NOMINAL_FLOW_REGISTER, default=384): cv.positive_int,
The speed settings I would offer right now are: VALUE_TO_SPEED = {
0: SPEED_OFF,
2: SPEED_SLEEP,
5: SPEED_LOW,
10: SPEED_MEDIUM,
15: SPEED_HIGH,
24: SPEED_VERY_HIGH,
}Do you know of any way in home assistant to set an array as config entry? That way both the amount of speed entries and their values could be configured. |
|
Please do not continue discussing on a PR that is merged. Please create an issue so we can trace the progress |
|
First, I will need to check out how other Fan implementations work. If the speed is set as a number, e.g., via some slider in the UI, I will implement my code the same way. Thanks for your input, I will think about how can I incorporate it into my code. At the moment, I created a PR for Modbus light. It has some changes, which need to be merged before I can work on Fan. I will let you know when I'll have something ready to test. @janiversen good point. I will create an issue tomorrow. |
A slider would also be the best solution for me by far, but I have not seen this for fans yet. |
Proposed change
This PR refactors the Modbus
Switchentity, so that we can later buildLightandFanentities on top of theSwitch. Also, initialization was simplified to match with other refactored and new components.Type of change
Additional information
Checklist
black --fast homeassistant tests)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest.requirements_all.txt.Updated by running
python3 -m script.gen_requirements_all..coveragerc.The integration reached or maintains the following Integration Quality Scale: