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
Support for Multiple modbus hubs #19726
Conversation
very cool, waiting for this! |
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.
Looks good! Some comments.
@@ -16,11 +16,13 @@ | |||
_LOGGER = logging.getLogger(__name__) | |||
DEPENDENCIES = ['modbus'] | |||
|
|||
CONF_HUB = 'hub' |
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.
Maybe we should define this in the modbus component instead, and import it where needed.
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.
Yes, that seems better. Done.
CONF_COIL = 'coil' | ||
CONF_COILS = 'coils' | ||
|
||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ | ||
vol.Required(CONF_COILS): [{ | ||
vol.Required(CONF_HUB, default='default'): cv.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.
The default value should also be defined centrally.
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.
Done. Defined DEFAULT_HUB.
@@ -7,7 +7,7 @@ | |||
import logging | |||
import voluptuous as vol | |||
|
|||
from homeassistant.components import modbus | |||
from homeassistant.components.modbus import DOMAIN |
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 platform belongs to the binary_sensor domain. Import the modbus domain as another name.
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.
Changed for all platforms.
homeassistant/components/modbus.py
Outdated
|
||
DOMAIN: | ||
vol.All(cv.ensure_list, | ||
vol.Schema([vol.Any(SERIAL_SCHEMA, ETHERNET_SCHEMA)])) |
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.
Do we need schema definition here? Try without 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.
I'm not entirely sure what you mean here. Do you want me to revert to what was there before?
That gives an 'Invalid config' when you actually define multiple hubs because a list isn't expected then.
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, I just think we don't need to add vol.Schema
around the list of schemas. Have you tried without that?
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.
Sorry, got confused. I removed it and that seems to work.
homeassistant/components/modbus.py
Outdated
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_modbus) | ||
|
||
# Register services for modbus | ||
hass.services.register( | ||
DOMAIN, SERVICE_WRITE_REGISTER, write_register, | ||
DOMAIN, |
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.
Please don't change unrelated formatting.
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.
Great!
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.
Sorry, found some more corrections needed.
@@ -40,6 +41,7 @@ | |||
|
|||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ | |||
vol.Required(CONF_REGISTERS): [{ | |||
vol.Required(CONF_HUB, DEFAULT_HUB): cv.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.
Please use keyword argument default
for default value.
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.
Since we set a default we can make this optional.
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.
Made hub name optional.
homeassistant/components/modbus.py
Outdated
ATTR_UNIT = 'unit' | ||
ATTR_VALUE = 'value' | ||
|
||
SERVICE_WRITE_REGISTER_SCHEMA = vol.Schema({ | ||
vol.Optional(ATTR_HUB, DEFAULT_HUB): cv.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.
Use default
keyword argument for the default value. The second argument is message, so we have to specify with name what argument we're providing.
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 catch, I must have accidentally deleted those while I was adding DEFAULT_HUB.
homeassistant/components/modbus.py
Outdated
vol.Required(ATTR_UNIT): cv.positive_int, | ||
vol.Required(ATTR_ADDRESS): cv.positive_int, | ||
vol.Required(ATTR_VALUE): vol.All(cv.ensure_list, [cv.positive_int]) | ||
}) | ||
|
||
SERVICE_WRITE_COIL_SCHEMA = vol.Schema({ | ||
vol.Optional(ATTR_HUB, DEFAULT_HUB): cv.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.
Same as above.
@@ -21,6 +22,7 @@ | |||
|
|||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ | |||
vol.Required(CONF_COILS): [{ | |||
vol.Required(CONF_HUB, default=DEFAULT_HUB): cv.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.
Make it optional.
import homeassistant.helpers.config_validation as cv | ||
|
||
REQUIREMENTS = ['pyflexit==0.3'] | ||
DEPENDENCIES = ['modbus'] | ||
|
||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ | ||
vol.Required(CONF_HUB, default=DEFAULT_HUB): cv.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.
Optional.
@@ -35,6 +35,7 @@ | |||
DATA_TYPE_FLOAT = 'float' | |||
|
|||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ | |||
vol.Required(CONF_HUB, default=DEFAULT_HUB): cv.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.
Optional.
@@ -31,6 +33,7 @@ | |||
REGISTER_TYPE_INPUT = 'input' | |||
|
|||
REGISTERS_SCHEMA = vol.Schema({ | |||
vol.Required(CONF_HUB, default=DEFAULT_HUB): cv.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.
Optional.
@@ -46,6 +49,7 @@ | |||
}) | |||
|
|||
COILS_SCHEMA = vol.Schema({ | |||
vol.Required(CONF_HUB, default=DEFAULT_HUB): cv.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.
Optional.
Can be merged when build passes. |
Can you merge upstream or rebase so we can merge this? Thanks. |
Rebase done. Also one more small commit added, sorry. I added a name property to ModbusHub. The platforms retrieve that to include in error messages. |
Can be merged when build passes. |
Description:
This PR implements support for multiple modbus hubs. It adds new configuration variables to distinguish between different hubs.
In the modbus section a new variable "name" is introduced. Sensors can then reference this name in a new "hub" variable. Both default to "default" when omitted so existing configuration should continue to work.
This is a continuation of #17901 with some alterations. The original author indicated that he didn't have time to finish it.
Pull request in home-assistant.io with documentation (if applicable): home-assistant/home-assistant.io#8027
Example entry for
configuration.yaml
(if applicable):Checklist:
tox
. Your PR cannot be merged unless tests passIf user exposed functionality or configuration variables are added/changed: