Skip to content

Refactor Modbus switch to provide a base for other entities#33551

Merged
MartinHjelmare merged 8 commits intohome-assistant:devfrom
Lutemi:feature/modbus-fan-and-light
Oct 12, 2020
Merged

Refactor Modbus switch to provide a base for other entities#33551
MartinHjelmare merged 8 commits intohome-assistant:devfrom
Lutemi:feature/modbus-fan-and-light

Conversation

@vzahradnik
Copy link
Copy Markdown
Contributor

@vzahradnik vzahradnik commented Apr 2, 2020

Proposed change

This PR refactors the Modbus Switch entity, so that we can later build Light and Fan entities on top of the Switch. Also, initialization was simplified to match with other refactored and new components.

Existing functionality is not affected by this change.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request: N/A

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • The code has been formatted using Black (black --fast homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • Untested files have been added to .coveragerc.

The integration reached or maintains the following Integration Quality Scale:

  • No score or internal
  • 🥈 Silver
  • 🥇 Gold
  • 🏆 Platinum

@probot-home-assistant
Copy link
Copy Markdown

Hey there @adamchengtkc, @janiversen, mind taking a look at this pull request as its been labeled with a integration (modbus) you are listed as a codeowner for? Thanks!

@bdraco
Copy link
Copy Markdown
Member

bdraco commented Apr 2, 2020

@vzahradnik

The CI issues should be fixed so it should run cleanly if you rebase

https://developers.home-assistant.io/docs/development_catching_up/

Copy link
Copy Markdown
Member

@janiversen janiversen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread homeassistant/components/modbus/fan.py Outdated
Comment thread homeassistant/components/modbus/light.py Outdated
Comment thread homeassistant/components/modbus/switch.py Outdated
Comment thread homeassistant/components/modbus/switch.py Outdated
Comment thread homeassistant/components/modbus/switch.py Outdated
Comment thread homeassistant/components/modbus/switch.py Outdated
Comment thread homeassistant/components/modbus/switch.py Outdated
@vzahradnik
Copy link
Copy Markdown
Contributor Author

@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.

@janiversen
Copy link
Copy Markdown
Member

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.

@vzahradnik
Copy link
Copy Markdown
Contributor Author

@janiversen no problem. If the implementation will be better thanks to our discussions, I'm all in for it :)

@janiversen
Copy link
Copy Markdown
Member

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.

@stale
Copy link
Copy Markdown

stale Bot commented May 16, 2020

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.
Thank you for your contributions.

@stale stale Bot added the stale label May 16, 2020
@vzahradnik
Copy link
Copy Markdown
Contributor Author

Still working on it.

@stale stale Bot removed the stale label May 20, 2020
@vzahradnik vzahradnik marked this pull request as ready for review June 4, 2020 15:02
@vzahradnik
Copy link
Copy Markdown
Contributor Author

@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.

@janiversen
Copy link
Copy Markdown
Member

What is the status of this PR ?

@vzahradnik
Copy link
Copy Markdown
Contributor Author

@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!

@janiversen
Copy link
Copy Markdown
Member

First if all you need to resolve the 2 failing CI parts, then I will do a review.

@vzahradnik
Copy link
Copy Markdown
Contributor Author

@janiversen OK, I will take a look.

@stale
Copy link
Copy Markdown

stale Bot commented Aug 1, 2020

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.
Thank you for your contributions.

@stale stale Bot added the stale label Aug 1, 2020
@vzahradnik
Copy link
Copy Markdown
Contributor Author

Still working on it.

@stale stale Bot removed the stale label Aug 3, 2020
@stale
Copy link
Copy Markdown

stale Bot commented Sep 2, 2020

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.
Thank you for your contributions.

@vzahradnik
Copy link
Copy Markdown
Contributor Author

@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.

@vzahradnik vzahradnik changed the title Add Modbus light and fan entities Refactor Modbus switch to provide a base for other entities Oct 12, 2020
@vzahradnik
Copy link
Copy Markdown
Contributor Author

  • I removed the Light and Fan code. As discussed with @MartinHjelmare, this PR will be only for the necessary Switch refactor
  • Once this change gets merged, I will open separate PRs for Light and Fan
  • Description and title of this PR was updated. @MartinHjelmare please remove the new-platform flag
  • Tests are passing, code was tested locally

Copy link
Copy Markdown
Member

@MartinHjelmare MartinHjelmare left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

Copy link
Copy Markdown
Member

@MartinHjelmare MartinHjelmare left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I missed one thing.

return self._available


class ModbusCoilSwitch(ModbusBaseSwitch):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should inherit from SwitchEntity somewhere in the switch entities.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the base class inherits from SwitchEntity we cannot inherit from the base class in other platforms than switch.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that would be my preference.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code still works, and tests pass.

Copy link
Copy Markdown
Member

@janiversen janiversen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

Copy link
Copy Markdown
Member

@MartinHjelmare MartinHjelmare left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@MartinHjelmare
Copy link
Copy Markdown
Member

Ready to merge?

@vzahradnik
Copy link
Copy Markdown
Contributor Author

Ready to merge?

Yes, if @janiversen has no objections.

@janiversen
Copy link
Copy Markdown
Member

No objections look good to me, please merge.

@MartinHjelmare MartinHjelmare merged commit 111afbb into home-assistant:dev Oct 12, 2020
@vzahradnik
Copy link
Copy Markdown
Contributor Author

vzahradnik commented Oct 12, 2020

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.

  • I'll start with the Light, because this should be ready to merge rather quickly
  • The Fan entity will require more work. Also, I see that people like @makuser would appreciate speed control. We'll do that in this PR, too.

@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 :)

@vzahradnik vzahradnik mentioned this pull request Oct 20, 2020
21 tasks
@makuser
Copy link
Copy Markdown
Contributor

makuser commented Oct 30, 2020

@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.

Yes, indeed. Here are a couple of important registers, we set the speed to CONF_SETPOINT_REGISTER, which is the only register to write to, the rest are just attributes that need to be added to the entity for monitoring purposes.

        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,

CONF_ACTUAL_PRESSURE_REGISTER needs a scaling factor of 10x, the others 100x.

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.
Otherwise, these speed entries seem to be sensible defaults for common 125mm air pipes@530m³ nominal flow.

@janiversen
Copy link
Copy Markdown
Member

Please do not continue discussing on a PR that is merged. Please create an issue so we can trace the progress

@vzahradnik
Copy link
Copy Markdown
Contributor Author

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.

@makuser
Copy link
Copy Markdown
Contributor

makuser commented Oct 31, 2020

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.

A slider would also be the best solution for me by far, but I have not seen this for fans yet.

@vzahradnik vzahradnik mentioned this pull request Oct 31, 2020
21 tasks
@vzahradnik vzahradnik mentioned this pull request Mar 31, 2021
21 tasks
@vzahradnik vzahradnik deleted the feature/modbus-fan-and-light branch April 7, 2021 12:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants