mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Fix Modbus review comments (#33755)
* update common test for modbus integration * remove log messages from modbus setup function. * Make global method local * Change parameter name to snake_case
This commit is contained in:
parent
c19a1bf26d
commit
b3286a4a01
@ -97,7 +97,6 @@ async def async_setup(hass, config):
|
|||||||
"""Set up Modbus component."""
|
"""Set up Modbus component."""
|
||||||
hass.data[MODBUS_DOMAIN] = hub_collect = {}
|
hass.data[MODBUS_DOMAIN] = hub_collect = {}
|
||||||
|
|
||||||
_LOGGER.debug("registering hubs")
|
|
||||||
for client_config in config[MODBUS_DOMAIN]:
|
for client_config in config[MODBUS_DOMAIN]:
|
||||||
hub_collect[client_config[CONF_NAME]] = ModbusHub(client_config, hass.loop)
|
hub_collect[client_config[CONF_NAME]] = ModbusHub(client_config, hass.loop)
|
||||||
|
|
||||||
@ -109,7 +108,6 @@ async def async_setup(hass, config):
|
|||||||
def start_modbus(event):
|
def start_modbus(event):
|
||||||
"""Start Modbus service."""
|
"""Start Modbus service."""
|
||||||
for client in hub_collect.values():
|
for client in hub_collect.values():
|
||||||
_LOGGER.debug("setup hub %s", client.name)
|
|
||||||
client.setup()
|
client.setup()
|
||||||
|
|
||||||
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_modbus)
|
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_modbus)
|
||||||
@ -161,7 +159,6 @@ class ModbusHub:
|
|||||||
|
|
||||||
def __init__(self, client_config, main_loop):
|
def __init__(self, client_config, main_loop):
|
||||||
"""Initialize the Modbus hub."""
|
"""Initialize the Modbus hub."""
|
||||||
_LOGGER.debug("Preparing setup: %s", client_config)
|
|
||||||
|
|
||||||
# generic configuration
|
# generic configuration
|
||||||
self._loop = main_loop
|
self._loop = main_loop
|
||||||
@ -200,7 +197,6 @@ class ModbusHub:
|
|||||||
# Client* do deliver loop, client as result but
|
# Client* do deliver loop, client as result but
|
||||||
# pylint does not accept that fact
|
# pylint does not accept that fact
|
||||||
|
|
||||||
_LOGGER.debug("doing setup")
|
|
||||||
if self._config_type == "serial":
|
if self._config_type == "serial":
|
||||||
_, self._client = ClientSerial(
|
_, self._client = ClientSerial(
|
||||||
schedulers.ASYNC_IO,
|
schedulers.ASYNC_IO,
|
||||||
|
@ -54,7 +54,7 @@ PLATFORM_SCHEMA = vol.All(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_platform(hass, config, add_entities, discovery_info=None):
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
||||||
"""Set up the Modbus binary sensors."""
|
"""Set up the Modbus binary sensors."""
|
||||||
sensors = []
|
sensors = []
|
||||||
for entry in config[CONF_INPUTS]:
|
for entry in config[CONF_INPUTS]:
|
||||||
@ -70,7 +70,7 @@ async def async_setup_platform(hass, config, add_entities, discovery_info=None):
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
add_entities(sensors)
|
async_add_entities(sensors)
|
||||||
|
|
||||||
|
|
||||||
class ModbusBinarySensor(BinarySensorDevice):
|
class ModbusBinarySensor(BinarySensorDevice):
|
||||||
|
@ -72,7 +72,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_platform(hass, config, add_entities, discovery_info=None):
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
||||||
"""Set up the Modbus Thermostat Platform."""
|
"""Set up the Modbus Thermostat Platform."""
|
||||||
name = config[CONF_NAME]
|
name = config[CONF_NAME]
|
||||||
modbus_slave = config[CONF_SLAVE]
|
modbus_slave = config[CONF_SLAVE]
|
||||||
@ -91,7 +91,7 @@ async def async_setup_platform(hass, config, add_entities, discovery_info=None):
|
|||||||
hub_name = config[CONF_HUB]
|
hub_name = config[CONF_HUB]
|
||||||
hub = hass.data[MODBUS_DOMAIN][hub_name]
|
hub = hass.data[MODBUS_DOMAIN][hub_name]
|
||||||
|
|
||||||
add_entities(
|
async_add_entities(
|
||||||
[
|
[
|
||||||
ModbusThermostat(
|
ModbusThermostat(
|
||||||
hub,
|
hub,
|
||||||
|
@ -89,7 +89,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_platform(hass, config, add_entities, discovery_info=None):
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
||||||
"""Set up the Modbus sensors."""
|
"""Set up the Modbus sensors."""
|
||||||
sensors = []
|
sensors = []
|
||||||
data_types = {DATA_TYPE_INT: {1: "h", 2: "i", 4: "q"}}
|
data_types = {DATA_TYPE_INT: {1: "h", 2: "i", 4: "q"}}
|
||||||
@ -148,7 +148,7 @@ async def async_setup_platform(hass, config, add_entities, discovery_info=None):
|
|||||||
|
|
||||||
if not sensors:
|
if not sensors:
|
||||||
return False
|
return False
|
||||||
add_entities(sensors)
|
async_add_entities(sensors)
|
||||||
|
|
||||||
|
|
||||||
class ModbusRegisterSensor(RestoreEntity):
|
class ModbusRegisterSensor(RestoreEntity):
|
||||||
|
@ -76,7 +76,7 @@ PLATFORM_SCHEMA = vol.All(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_platform(hass, config, add_entities, discovery_info=None):
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
||||||
"""Read configuration and create Modbus devices."""
|
"""Read configuration and create Modbus devices."""
|
||||||
switches = []
|
switches = []
|
||||||
if CONF_COILS in config:
|
if CONF_COILS in config:
|
||||||
@ -109,7 +109,7 @@ async def async_setup_platform(hass, config, add_entities, discovery_info=None):
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
add_entities(switches)
|
async_add_entities(switches)
|
||||||
|
|
||||||
|
|
||||||
class ModbusCoilSwitch(ToggleEntity, RestoreEntity):
|
class ModbusCoilSwitch(ToggleEntity, RestoreEntity):
|
||||||
|
@ -32,9 +32,6 @@ def mock_hub(hass):
|
|||||||
return hub
|
return hub
|
||||||
|
|
||||||
|
|
||||||
common_register_config = {CONF_NAME: "test-config", CONF_REGISTER: 1234}
|
|
||||||
|
|
||||||
|
|
||||||
class ReadResult:
|
class ReadResult:
|
||||||
"""Storage class for register read results."""
|
"""Storage class for register read results."""
|
||||||
|
|
||||||
@ -46,18 +43,16 @@ class ReadResult:
|
|||||||
read_result = None
|
read_result = None
|
||||||
|
|
||||||
|
|
||||||
async def simulate_read_registers(unit, address, count):
|
|
||||||
"""Simulate modbus register read."""
|
|
||||||
del unit, address, count # not used in simulation, but in real connection
|
|
||||||
global read_result
|
|
||||||
return read_result
|
|
||||||
|
|
||||||
|
|
||||||
async def run_test(
|
async def run_test(
|
||||||
hass, mock_hub, register_config, entity_domain, register_words, expected
|
hass, use_mock_hub, register_config, entity_domain, register_words, expected
|
||||||
):
|
):
|
||||||
"""Run test for given config and check that sensor outputs expected result."""
|
"""Run test for given config and check that sensor outputs expected result."""
|
||||||
|
|
||||||
|
async def simulate_read_registers(unit, address, count):
|
||||||
|
"""Simulate modbus register read."""
|
||||||
|
del unit, address, count # not used in simulation, but in real connection
|
||||||
|
return read_result
|
||||||
|
|
||||||
# Full sensor configuration
|
# Full sensor configuration
|
||||||
sensor_name = "modbus_test_sensor"
|
sensor_name = "modbus_test_sensor"
|
||||||
scan_interval = 5
|
scan_interval = 5
|
||||||
@ -72,12 +67,11 @@ async def run_test(
|
|||||||
}
|
}
|
||||||
|
|
||||||
# Setup inputs for the sensor
|
# Setup inputs for the sensor
|
||||||
global read_result
|
|
||||||
read_result = ReadResult(register_words)
|
read_result = ReadResult(register_words)
|
||||||
if register_config.get(CONF_REGISTER_TYPE) == CALL_TYPE_REGISTER_INPUT:
|
if register_config.get(CONF_REGISTER_TYPE) == CALL_TYPE_REGISTER_INPUT:
|
||||||
mock_hub.read_input_registers = simulate_read_registers
|
use_mock_hub.read_input_registers = simulate_read_registers
|
||||||
else:
|
else:
|
||||||
mock_hub.read_holding_registers = simulate_read_registers
|
use_mock_hub.read_holding_registers = simulate_read_registers
|
||||||
|
|
||||||
# Initialize sensor
|
# Initialize sensor
|
||||||
now = dt_util.utcnow()
|
now = dt_util.utcnow()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user