-
-
Notifications
You must be signed in to change notification settings - Fork 38.2k
Refactor Modbus switch to provide a base for other entities #33551
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
Merged
MartinHjelmare
merged 8 commits into
home-assistant:dev
from
Lutemi:feature/modbus-fan-and-light
Oct 12, 2020
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
76f36df
Add Modbus light and fan entities
vzahradnik 99ff45c
Rework Fan and Light components to new config structure
vzahradnik 950eae7
Fix Linter issue
vzahradnik 533c758
Rework Modbus switch initialization
vzahradnik 07454dd
Properly update state on actuator methods
vzahradnik 868f795
Remove Fan and Light entity from this change. They will be merged sep…
vzahradnik 2c71a27
Fix loading of the Switch platform
vzahradnik dd71f19
Modbus switch - inherit from the SwitchEntity
vzahradnik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| """Support for Modbus switches.""" | ||
| from abc import ABC | ||
| import logging | ||
| from typing import Optional | ||
| from typing import Any, Dict, Optional | ||
|
|
||
| from pymodbus.exceptions import ConnectionException, ModbusException | ||
| from pymodbus.pdu import ExceptionResponse | ||
|
|
@@ -17,7 +18,9 @@ | |
| from homeassistant.helpers import config_validation as cv | ||
| from homeassistant.helpers.entity import ToggleEntity | ||
| from homeassistant.helpers.restore_state import RestoreEntity | ||
| from homeassistant.helpers.typing import ConfigType, HomeAssistantType | ||
|
|
||
| from . import ModbusHub | ||
| from .const import ( | ||
| CALL_TYPE_COIL, | ||
| CALL_TYPE_REGISTER_HOLDING, | ||
|
|
@@ -76,51 +79,31 @@ | |
| ) | ||
|
|
||
|
|
||
| def setup_platform(hass, config, add_entities, discovery_info=None): | ||
| """Read configuration and create Modbus devices.""" | ||
| async def async_setup_platform( | ||
| hass: HomeAssistantType, config: ConfigType, async_add_entities, discovery_info=None | ||
| ): | ||
| """Read configuration and create Modbus switches.""" | ||
| switches = [] | ||
| if CONF_COILS in config: | ||
| for coil in config[CONF_COILS]: | ||
| hub_name = coil[CONF_HUB] | ||
| hub = hass.data[MODBUS_DOMAIN][hub_name] | ||
| switches.append( | ||
| ModbusCoilSwitch( | ||
| hub, coil[CONF_NAME], coil[CONF_SLAVE], coil[CALL_TYPE_COIL] | ||
| ) | ||
| ) | ||
| hub: ModbusHub = hass.data[MODBUS_DOMAIN][coil[CONF_HUB]] | ||
| switches.append(ModbusCoilSwitch(hub, coil)) | ||
| if CONF_REGISTERS in config: | ||
| for register in config[CONF_REGISTERS]: | ||
| hub_name = register[CONF_HUB] | ||
| hub = hass.data[MODBUS_DOMAIN][hub_name] | ||
|
|
||
| switches.append( | ||
| ModbusRegisterSwitch( | ||
| hub, | ||
| register[CONF_NAME], | ||
| register.get(CONF_SLAVE), | ||
| register[CONF_REGISTER], | ||
| register[CONF_COMMAND_ON], | ||
| register[CONF_COMMAND_OFF], | ||
| register[CONF_VERIFY_STATE], | ||
| register.get(CONF_VERIFY_REGISTER), | ||
| register[CONF_REGISTER_TYPE], | ||
| register.get(CONF_STATE_ON), | ||
| register.get(CONF_STATE_OFF), | ||
| ) | ||
| ) | ||
| hub: ModbusHub = hass.data[MODBUS_DOMAIN][register[CONF_HUB]] | ||
| switches.append(ModbusRegisterSwitch(hub, register)) | ||
|
|
||
| add_entities(switches) | ||
| async_add_entities(switches) | ||
|
|
||
|
|
||
| class ModbusCoilSwitch(ToggleEntity, RestoreEntity): | ||
| """Representation of a Modbus coil switch.""" | ||
| class ModbusBaseSwitch(ToggleEntity, RestoreEntity, ABC): | ||
| """Base class representing a Modbus switch.""" | ||
|
|
||
| def __init__(self, hub, name, slave, coil): | ||
| """Initialize the coil switch.""" | ||
| self._hub = hub | ||
| self._name = name | ||
| self._slave = int(slave) if slave else None | ||
| self._coil = int(coil) | ||
| def __init__(self, hub: ModbusHub, config: Dict[str, Any]): | ||
| """Initialize the switch.""" | ||
| self._hub: ModbusHub = hub | ||
| self._name = config[CONF_NAME] | ||
| self._slave = config.get(CONF_SLAVE) | ||
| self._is_on = None | ||
| self._available = True | ||
|
|
||
|
|
@@ -146,13 +129,24 @@ def available(self) -> bool: | |
| """Return True if entity is available.""" | ||
| return self._available | ||
|
|
||
|
|
||
| class ModbusCoilSwitch(ModbusBaseSwitch): | ||
| """Representation of a Modbus coil switch.""" | ||
|
|
||
| def __init__(self, hub: ModbusHub, config: Dict[str, Any]): | ||
| """Initialize the coil switch.""" | ||
| super().__init__(hub, config) | ||
| self._coil = config[CALL_TYPE_COIL] | ||
|
|
||
| def turn_on(self, **kwargs): | ||
| """Set switch on.""" | ||
| self._write_coil(self._coil, True) | ||
| self._is_on = True | ||
|
|
||
| def turn_off(self, **kwargs): | ||
| """Set switch off.""" | ||
| self._write_coil(self._coil, False) | ||
| self._is_on = False | ||
|
|
||
| def update(self): | ||
| """Update the state of the switch.""" | ||
|
|
@@ -172,7 +166,7 @@ def _read_coil(self, coil) -> bool: | |
|
|
||
| self._available = True | ||
| # bits[0] select the lowest bit in result, | ||
| # is_on for a binary_sensor is true if the bit are 1 | ||
| # is_on for a binary_sensor is true if the bit is 1 | ||
| # The other bits are not considered. | ||
| return bool(result.bits[0] & 1) | ||
|
|
||
|
|
@@ -187,46 +181,21 @@ def _write_coil(self, coil, value): | |
| self._available = True | ||
|
|
||
|
|
||
| class ModbusRegisterSwitch(ModbusCoilSwitch): | ||
| class ModbusRegisterSwitch(ModbusBaseSwitch): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Switch changes are ok |
||
| """Representation of a Modbus register switch.""" | ||
|
|
||
| # pylint: disable=super-init-not-called | ||
|
vzahradnik marked this conversation as resolved.
|
||
| def __init__( | ||
| self, | ||
| hub, | ||
| name, | ||
| slave, | ||
| register, | ||
| command_on, | ||
| command_off, | ||
| verify_state, | ||
| verify_register, | ||
| register_type, | ||
| state_on, | ||
| state_off, | ||
| ): | ||
| def __init__(self, hub: ModbusHub, config: Dict[str, Any]): | ||
| """Initialize the register switch.""" | ||
| self._hub = hub | ||
| self._name = name | ||
| self._slave = slave | ||
| self._register = register | ||
| self._command_on = command_on | ||
| self._command_off = command_off | ||
| self._verify_state = verify_state | ||
| self._verify_register = verify_register if verify_register else self._register | ||
| self._register_type = register_type | ||
| super().__init__(hub, config) | ||
| self._register = config[CONF_REGISTER] | ||
| self._command_on = config[CONF_COMMAND_ON] | ||
| self._command_off = config[CONF_COMMAND_OFF] | ||
| self._state_on = config.get(CONF_STATE_ON, self._command_on) | ||
| self._state_off = config.get(CONF_STATE_OFF, self._command_off) | ||
| self._verify_state = config[CONF_VERIFY_STATE] | ||
| self._verify_register = config.get(CONF_VERIFY_REGISTER, self._register) | ||
| self._register_type = config[CONF_REGISTER_TYPE] | ||
| self._available = True | ||
|
|
||
| if state_on is not None: | ||
| self._state_on = state_on | ||
| else: | ||
| self._state_on = self._command_on | ||
|
|
||
| if state_off is not None: | ||
| self._state_off = state_off | ||
| else: | ||
| self._state_off = self._command_off | ||
|
|
||
| self._is_on = None | ||
|
|
||
| def turn_on(self, **kwargs): | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
We should inherit from
SwitchEntitysomewhere in the switch entities.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.
I guess the original code was written before
SwitchEntityeven existed. Anyway, theSwitchEntityinherits from theToggleEntity. I will modify the base class to inherit directly from theSwitchEntity. This should solve our problem.I will let you know.
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.
If the base class inherits from
SwitchEntitywe cannot inherit from the base class in other platforms than switch.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.
Good point. Probably I should add the inheritance into the
ModbusCoilSwitchandModbusRegisterSwitch. This would solve our problem, and yet Fan and Light can still inherit from 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.
that would be my preference.
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.
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.
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.