Update remaining modbus platforms to use pymodbus_call (#50768)

This commit is contained in:
jan iversen 2021-05-17 22:12:18 +02:00 committed by GitHub
parent f762d3c748
commit ba827db8ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 44 additions and 97 deletions

View File

@ -28,7 +28,7 @@ from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from .const import (
ATTR_TEMPERATURE,
CALL_TYPE_REGISTER_HOLDING,
CALL_TYPE_REGISTER_INPUT,
CALL_TYPE_WRITE_REGISTERS,
CONF_CLIMATES,
CONF_CURRENT_TEMP,
CONF_CURRENT_TEMP_REGISTER_TYPE,
@ -208,10 +208,11 @@ class ModbusThermostat(ClimateEntity):
)
byte_string = struct.pack(self._structure, target_temperature)
register_value = struct.unpack(">h", byte_string[0:2])[0]
self._available = await self._hub.async_write_registers(
self._available = await self._hub.async_pymodbus_call(
self._slave,
self._target_temperature_register,
register_value,
CALL_TYPE_WRITE_REGISTERS,
)
await self.async_update()
@ -235,14 +236,9 @@ class ModbusThermostat(ClimateEntity):
async def _async_read_register(self, register_type, register) -> float | None:
"""Read register using the Modbus hub slave."""
if register_type == CALL_TYPE_REGISTER_INPUT:
result = await self._hub.async_read_input_registers(
self._slave, register, self._count
)
else:
result = await self._hub.async_read_holding_registers(
self._slave, register, self._count
)
result = await self._hub.async_pymodbus_call(
self._slave, register, self._count, register_type
)
if result is None:
self._available = False
return -1

View File

@ -27,7 +27,8 @@ from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from .const import (
CALL_TYPE_COIL,
CALL_TYPE_REGISTER_HOLDING,
CALL_TYPE_REGISTER_INPUT,
CALL_TYPE_WRITE_COIL,
CALL_TYPE_WRITE_REGISTER,
CONF_REGISTER,
CONF_STATE_CLOSED,
CONF_STATE_CLOSING,
@ -94,18 +95,22 @@ class ModbusCover(CoverEntity, RestoreEntity):
# If we read cover status from coil, and not from optional status register,
# we interpret boolean value False as closed cover, and value True as open cover.
# Intermediate states are not supported in such a setup.
if self._coil is not None and self._status_register is None:
self._state_closed = False
self._state_open = True
self._state_closing = None
self._state_opening = None
if self._coil is not None:
self._write_type = CALL_TYPE_WRITE_COIL
if self._status_register is None:
self._state_closed = False
self._state_open = True
self._state_closing = None
self._state_opening = None
# If we read cover status from the main register (i.e., an optional
# status register is not specified), we need to make sure the register_type
# is set to "holding".
if self._register is not None and self._status_register is None:
self._status_register = self._register
self._status_register_type = CALL_TYPE_REGISTER_HOLDING
if self._register is not None:
self._write_type = CALL_TYPE_WRITE_REGISTER
if self._status_register is None:
self._status_register = self._register
self._status_register_type = CALL_TYPE_REGISTER_HOLDING
async def async_added_to_hass(self):
"""Handle entity which will be added."""
@ -169,21 +174,17 @@ class ModbusCover(CoverEntity, RestoreEntity):
async def async_open_cover(self, **kwargs: Any) -> None:
"""Open cover."""
if self._coil is not None:
await self._async_write_coil(True)
else:
await self._async_write_register(self._state_open)
await self.async_update()
self._available = await self._hub.async_pymodbus_call(
self._slave, self._register, self._state_open, self._write_type
)
self.async_update()
async def async_close_cover(self, **kwargs: Any) -> None:
"""Close cover."""
if self._coil is not None:
await self._async_write_coil(False)
else:
await self._async_write_register(self._state_closed)
await self.async_update()
self._available = await self._hub.async_pymodbus_call(
self._slave, self._register, self._state_closed, self._write_type
)
self.async_update()
async def async_update(self, now=None):
"""Update the state of the cover."""
@ -198,14 +199,9 @@ class ModbusCover(CoverEntity, RestoreEntity):
async def _async_read_status_register(self) -> int | None:
"""Read status register using the Modbus hub slave."""
if self._status_register_type == CALL_TYPE_REGISTER_INPUT:
result = await self._hub.async_read_input_registers(
self._slave, self._status_register, 1
)
else:
result = await self._hub.async_read_holding_registers(
self._slave, self._status_register, 1
)
result = await self._hub.async_pymodbus_call(
self._slave, self._status_register, 1, self._status_register_type
)
if result is None:
self._available = False
return None
@ -215,24 +211,14 @@ class ModbusCover(CoverEntity, RestoreEntity):
return value
async def _async_write_register(self, value):
"""Write holding register using the Modbus hub slave."""
self._available = await self._hub.async_write_register(
self._slave, self._register, value
)
async def _async_read_coil(self) -> bool | None:
"""Read coil using the Modbus hub slave."""
result = await self._hub.async_read_coils(self._slave, self._coil, 1)
result = await self._hub.async_pymodbus_call(
self._slave, self._coil, 1, CALL_TYPE_COIL
)
if result is None:
self._available = False
return None
value = bool(result.bits[0] & 1)
return value
async def _async_write_coil(self, value):
"""Write coil using the Modbus hub slave."""
self._available = await self._hub.async_write_coil(
self._slave, self._coil, value
)

View File

@ -327,26 +327,6 @@ class ModbusHub:
self._pymodbus_call, unit, address, value, use_call
)
async def async_read_coils(self, unit, address, count):
"""Read coils."""
return await self.async_pymodbus_call(unit, address, count, CALL_TYPE_COIL)
async def async_read_discrete_inputs(self, unit, address, count):
"""Read discrete inputs."""
return await self.async_pymodbus_call(unit, address, count, CALL_TYPE_DISCRETE)
async def async_read_input_registers(self, unit, address, count):
"""Read input registers."""
return await self.async_pymodbus_call(
unit, address, count, CALL_TYPE_REGISTER_INPUT
)
async def async_read_holding_registers(self, unit, address, count):
"""Read holding registers."""
return await self.async_pymodbus_call(
unit, address, count, CALL_TYPE_REGISTER_HOLDING
)
async def async_write_coil(self, unit, address, value) -> bool:
"""Write coil."""
return await self.async_pymodbus_call(

View File

@ -22,9 +22,6 @@ from homeassistant.helpers.typing import ConfigType
from .const import (
CALL_TYPE_COIL,
CALL_TYPE_DISCRETE,
CALL_TYPE_REGISTER_HOLDING,
CALL_TYPE_REGISTER_INPUT,
CONF_INPUT_TYPE,
CONF_STATE_OFF,
CONF_STATE_ON,
@ -62,14 +59,9 @@ class ModbusSwitch(SwitchEntity, RestoreEntity):
self._available = True
self._scan_interval = timedelta(seconds=config[CONF_SCAN_INTERVAL])
self._address = config[CONF_ADDRESS]
if config[CONF_WRITE_TYPE] == CALL_TYPE_COIL:
self._async_write_func = self._hub.async_write_coil
self._command_on = 0x01
self._command_off = 0x00
else:
self._async_write_func = self._hub.async_write_register
self._command_on = config[CONF_COMMAND_ON]
self._command_off = config[CONF_COMMAND_OFF]
self._write_type = config[CONF_WRITE_TYPE]
self._command_on = config[CONF_COMMAND_ON]
self._command_off = config[CONF_COMMAND_OFF]
if CONF_VERIFY in config:
if config[CONF_VERIFY] is None:
config[CONF_VERIFY] = {}
@ -82,15 +74,6 @@ class ModbusSwitch(SwitchEntity, RestoreEntity):
)
self._state_on = config[CONF_VERIFY].get(CONF_STATE_ON, self._command_on)
self._state_off = config[CONF_VERIFY].get(CONF_STATE_OFF, self._command_off)
if self._verify_type == CALL_TYPE_REGISTER_HOLDING:
self._async_read_func = self._hub.async_read_holding_registers
elif self._verify_type == CALL_TYPE_DISCRETE:
self._async_read_func = self._hub.async_read_discrete_inputs
elif self._verify_type == CALL_TYPE_REGISTER_INPUT:
self._async_read_func = self._hub.async_read_input_registers
else: # self._verify_type == CALL_TYPE_COIL:
self._async_read_func = self._hub.async_read_coils
else:
self._verify_active = False
@ -125,8 +108,8 @@ class ModbusSwitch(SwitchEntity, RestoreEntity):
async def async_turn_on(self, **kwargs):
"""Set switch on."""
result = await self._async_write_func(
self._slave, self._address, self._command_on
result = await self._hub.async_pymodbus_call(
self._slave, self._address, self._command_on, self._write_type
)
if result is False:
self._available = False
@ -141,8 +124,8 @@ class ModbusSwitch(SwitchEntity, RestoreEntity):
async def async_turn_off(self, **kwargs):
"""Set switch off."""
result = await self._async_write_func(
self._slave, self._address, self._command_off
result = await self._hub.async_pymodbus_call(
self._slave, self._address, self._command_off, self._write_type
)
if result is False:
self._available = False
@ -164,7 +147,9 @@ class ModbusSwitch(SwitchEntity, RestoreEntity):
self.async_write_ha_state()
return
result = await self._async_read_func(self._slave, self._verify_address, 1)
result = await self._hub.async_pymodbus_call(
self._slave, self._verify_address, 1, self._verify_type
)
if result is None:
self._available = False
self.async_write_ha_state()