Skip to content
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

Merged
merged 18 commits into from Feb 11, 2019
Merged

Conversation

benvm
Copy link
Contributor

@benvm benvm commented Jan 2, 2019

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

modbus:
  - name: hub1
    type: tcp
    host: 192.168.0.1
    port: 502

  - name: hub2
    type: rtuovertcp
    host: 192.168.0.2
    port: 8887

sensor:
  - platform: modbus
    scan_interval: 5
    registers:
      - name: first_sensor
        hub: hub1
        register: 20
        data_type: uint
      - name: another_sensor
        hub: hub2
        slave: 1
        register: 2
        register_type: holding
        count: 1

Checklist:

  • The code change is tested and works locally.
  • Local tests pass with tox. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.

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

@janitza-bjag
Copy link

very cool, waiting for this!

@benvm benvm changed the title [WIP] Support for Multiple modbus hubs Support for Multiple modbus hubs Jan 16, 2019
Copy link
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! Some comments.

@@ -16,11 +16,13 @@
_LOGGER = logging.getLogger(__name__)
DEPENDENCIES = ['modbus']

CONF_HUB = 'hub'
Copy link
Member

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.

Copy link
Contributor Author

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,
Copy link
Member

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.

Copy link
Contributor Author

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
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed for all platforms.


DOMAIN:
vol.All(cv.ensure_list,
vol.Schema([vol.Any(SERIAL_SCHEMA, ETHERNET_SCHEMA)]))
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

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?

Copy link
Contributor Author

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.

hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_modbus)

# Register services for modbus
hass.services.register(
DOMAIN, SERVICE_WRITE_REGISTER, write_register,
DOMAIN,
Copy link
Member

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.

homeassistant/components/switch/modbus.py Outdated Show resolved Hide resolved
homeassistant/components/switch/modbus.py Outdated Show resolved Hide resolved
homeassistant/components/switch/modbus.py Outdated Show resolved Hide resolved
homeassistant/components/climate/modbus.py Outdated Show resolved Hide resolved
homeassistant/components/climate/modbus.py Outdated Show resolved Hide resolved
Copy link
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.

Great!

Copy link
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, 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,
Copy link
Member

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.

Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Made hub name optional.

ATTR_UNIT = 'unit'
ATTR_VALUE = 'value'

SERVICE_WRITE_REGISTER_SCHEMA = vol.Schema({
vol.Optional(ATTR_HUB, DEFAULT_HUB): cv.string,
Copy link
Member

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.

Copy link
Contributor Author

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.

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,
Copy link
Member

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,
Copy link
Member

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,
Copy link
Member

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,
Copy link
Member

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,
Copy link
Member

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,
Copy link
Member

Choose a reason for hiding this comment

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

Optional.

@MartinHjelmare
Copy link
Member

Can be merged when build passes.

@balloob
Copy link
Member

balloob commented Feb 6, 2019

Can you merge upstream or rebase so we can merge this? Thanks.

@benvm
Copy link
Contributor Author

benvm commented Feb 6, 2019

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.

@MartinHjelmare
Copy link
Member

Can be merged when build passes.

@cgarwood cgarwood merged commit 861d58f into home-assistant:dev Feb 11, 2019
@ghost ghost removed the in progress label Feb 11, 2019
@balloob balloob mentioned this pull request Feb 20, 2019
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.

None yet

8 participants