Support for Multiple modbus hubs (#19726)
* modbus: support multiple modbus hub * update data after entities added * pass hub object to each entity. and save hub to hass.data but not in module level * add hub_client setup log * don't update when adding device, because hub_client is not ready right now * support restore last state * remove useless func * compatible with python35 * removed unrelated style changes * Update flexit for multi-device modbus * change how hubs are referenced in the configuration * Also update climate/modbus.py * Remove unwanted whitescapce * Defined common constants centrally * Update DOMAIN in climate and switch components * Removed unnecessary vol.schema * Make hub name optional * Add name property to ModbusHub
This commit is contained in:
1 parent
49ecca9cb9
commit
861d58f58f
6 files changed
+163
-94
No files matched your search
@@ -17,8 +17,8 @@ from homeassistant.const import (
|
||||
CONF_NAME, CONF_SLAVE, ATTR_TEMPERATURE)
|
||||
from homeassistant.components.climate import (
|
||||
ClimateDevice, PLATFORM_SCHEMA, SUPPORT_TARGET_TEMPERATURE)
|
||||
|
||||
from homeassistant.components import modbus
|
||||
from homeassistant.components.modbus import (
|
||||
CONF_HUB, DEFAULT_HUB, DOMAIN as MODBUS_DOMAIN)
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
||||
DEPENDENCIES = ['modbus']
|
||||
@@ -35,6 +35,7 @@ DATA_TYPE_UINT = 'uint'
|
||||
DATA_TYPE_FLOAT = 'float'
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||
vol.Optional(CONF_HUB, default=DEFAULT_HUB): cv.string,
|
||||
vol.Required(CONF_NAME): cv.string,
|
||||
vol.Required(CONF_SLAVE): cv.positive_int,
|
||||
vol.Required(CONF_TARGET_TEMP): cv.positive_int,
|
||||
@@ -59,8 +60,10 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
data_type = config.get(CONF_DATA_TYPE)
|
||||
count = config.get(CONF_COUNT)
|
||||
precision = config.get(CONF_PRECISION)
|
||||
hub_name = config.get(CONF_HUB)
|
||||
hub = hass.data[MODBUS_DOMAIN][hub_name]
|
||||
|
||||
add_entities([ModbusThermostat(name, modbus_slave,
|
||||
add_entities([ModbusThermostat(hub, name, modbus_slave,
|
||||
target_temp_register, current_temp_register,
|
||||
data_type, count, precision)], True)
|
||||
|
||||
@@ -68,9 +71,10 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
class ModbusThermostat(ClimateDevice):
|
||||
"""Representation of a Modbus Thermostat."""
|
||||
|
||||
def __init__(self, name, modbus_slave, target_temp_register,
|
||||
def __init__(self, hub, name, modbus_slave, target_temp_register,
|
||||
current_temp_register, data_type, count, precision):
|
||||
"""Initialize the unit."""
|
||||
self._hub = hub
|
||||
self._name = name
|
||||
self._slave = modbus_slave
|
||||
self._target_temperature_register = target_temp_register
|
||||
@@ -133,8 +137,8 @@ class ModbusThermostat(ClimateDevice):
|
||||
def read_register(self, register):
|
||||
"""Read holding register using the modbus hub slave."""
|
||||
try:
|
||||
result = modbus.HUB.read_holding_registers(self._slave, register,
|
||||
self._count)
|
||||
result = self._hub.read_holding_registers(self._slave, register,
|
||||
self._count)
|
||||
except AttributeError as ex:
|
||||
_LOGGER.error(ex)
|
||||
byte_string = b''.join(
|
||||
@@ -145,4 +149,4 @@ class ModbusThermostat(ClimateDevice):
|
||||
|
||||
def write_register(self, register, value):
|
||||
"""Write register using the modbus hub slave."""
|
||||
modbus.HUB.write_registers(self._slave, register, [value, 0])
|
||||
self._hub.write_registers(self._slave, register, [value, 0])
|
||||
Reference in new issue
Block a user