2021-05-20 14:56:11 +00:00
|
|
|
"""Base implementation for all modbus platforms."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from abc import abstractmethod
|
|
|
|
from datetime import timedelta
|
|
|
|
import logging
|
2021-07-20 04:52:58 +00:00
|
|
|
import struct
|
2021-05-20 14:56:11 +00:00
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_ADDRESS,
|
2021-05-24 18:13:25 +00:00
|
|
|
CONF_COMMAND_OFF,
|
|
|
|
CONF_COMMAND_ON,
|
2021-07-20 04:52:58 +00:00
|
|
|
CONF_COUNT,
|
2021-05-24 18:13:25 +00:00
|
|
|
CONF_DELAY,
|
2021-05-20 14:56:11 +00:00
|
|
|
CONF_DEVICE_CLASS,
|
|
|
|
CONF_NAME,
|
2021-07-20 04:52:58 +00:00
|
|
|
CONF_OFFSET,
|
2021-05-20 14:56:11 +00:00
|
|
|
CONF_SCAN_INTERVAL,
|
|
|
|
CONF_SLAVE,
|
2021-07-20 04:52:58 +00:00
|
|
|
CONF_STRUCTURE,
|
2021-05-24 18:13:25 +00:00
|
|
|
STATE_ON,
|
2021-05-20 14:56:11 +00:00
|
|
|
)
|
2021-08-08 21:23:21 +00:00
|
|
|
from homeassistant.helpers.entity import Entity, ToggleEntity
|
2021-05-24 18:13:25 +00:00
|
|
|
from homeassistant.helpers.event import async_call_later, async_track_time_interval
|
|
|
|
from homeassistant.helpers.restore_state import RestoreEntity
|
2021-05-20 14:56:11 +00:00
|
|
|
|
2021-05-24 18:13:25 +00:00
|
|
|
from .const import (
|
|
|
|
CALL_TYPE_COIL,
|
2021-07-12 18:22:53 +00:00
|
|
|
CALL_TYPE_REGISTER_HOLDING,
|
2021-05-24 18:13:25 +00:00
|
|
|
CALL_TYPE_WRITE_COIL,
|
2021-07-12 18:22:53 +00:00
|
|
|
CALL_TYPE_WRITE_COILS,
|
2021-05-24 18:13:25 +00:00
|
|
|
CALL_TYPE_WRITE_REGISTER,
|
2021-07-12 18:22:53 +00:00
|
|
|
CALL_TYPE_WRITE_REGISTERS,
|
|
|
|
CALL_TYPE_X_COILS,
|
|
|
|
CALL_TYPE_X_REGISTER_HOLDINGS,
|
2021-07-20 04:52:58 +00:00
|
|
|
CONF_DATA_TYPE,
|
2021-05-24 18:13:25 +00:00
|
|
|
CONF_INPUT_TYPE,
|
2021-07-20 04:52:58 +00:00
|
|
|
CONF_PRECISION,
|
|
|
|
CONF_SCALE,
|
2021-05-24 18:13:25 +00:00
|
|
|
CONF_STATE_OFF,
|
|
|
|
CONF_STATE_ON,
|
2021-07-20 04:52:58 +00:00
|
|
|
CONF_SWAP,
|
|
|
|
CONF_SWAP_BYTE,
|
|
|
|
CONF_SWAP_WORD,
|
|
|
|
CONF_SWAP_WORD_BYTE,
|
2021-05-24 18:13:25 +00:00
|
|
|
CONF_VERIFY,
|
|
|
|
CONF_WRITE_TYPE,
|
2021-07-20 04:52:58 +00:00
|
|
|
DATA_TYPE_STRING,
|
2021-05-24 18:13:25 +00:00
|
|
|
)
|
2021-05-20 14:56:11 +00:00
|
|
|
from .modbus import ModbusHub
|
|
|
|
|
|
|
|
PARALLEL_UPDATES = 1
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class BasePlatform(Entity):
|
|
|
|
"""Base for readonly platforms."""
|
|
|
|
|
|
|
|
def __init__(self, hub: ModbusHub, entry: dict[str, Any]) -> None:
|
|
|
|
"""Initialize the Modbus binary sensor."""
|
|
|
|
self._hub = hub
|
2021-08-05 00:03:31 +00:00
|
|
|
# temporary fix,
|
|
|
|
# make sure slave is always defined to avoid an error in pymodbus
|
|
|
|
# attr(in_waiting) not defined.
|
|
|
|
# see issue #657 and PR #660 in riptideio/pymodbus
|
|
|
|
self._slave = entry.get(CONF_SLAVE, 0)
|
2021-05-20 14:56:11 +00:00
|
|
|
self._address = int(entry[CONF_ADDRESS])
|
|
|
|
self._input_type = entry[CONF_INPUT_TYPE]
|
|
|
|
self._value = None
|
2021-05-24 10:59:55 +00:00
|
|
|
self._scan_interval = int(entry[CONF_SCAN_INTERVAL])
|
2021-07-13 19:45:42 +00:00
|
|
|
self._call_active = False
|
2021-05-20 14:56:11 +00:00
|
|
|
|
2021-07-26 19:20:34 +00:00
|
|
|
self._attr_name = entry[CONF_NAME]
|
|
|
|
self._attr_should_poll = False
|
|
|
|
self._attr_device_class = entry.get(CONF_DEVICE_CLASS)
|
|
|
|
self._attr_available = True
|
|
|
|
self._attr_unit_of_measurement = None
|
|
|
|
|
2021-05-20 14:56:11 +00:00
|
|
|
@abstractmethod
|
|
|
|
async def async_update(self, now=None):
|
|
|
|
"""Virtual function to be overwritten."""
|
|
|
|
|
|
|
|
async def async_base_added_to_hass(self):
|
|
|
|
"""Handle entity which will be added."""
|
2021-05-24 10:59:55 +00:00
|
|
|
if self._scan_interval > 0:
|
|
|
|
async_track_time_interval(
|
|
|
|
self.hass, self.async_update, timedelta(seconds=self._scan_interval)
|
|
|
|
)
|
2021-05-20 14:56:11 +00:00
|
|
|
|
2021-05-24 18:13:25 +00:00
|
|
|
|
2021-07-20 04:52:58 +00:00
|
|
|
class BaseStructPlatform(BasePlatform, RestoreEntity):
|
|
|
|
"""Base class representing a sensor/climate."""
|
|
|
|
|
|
|
|
def __init__(self, hub: ModbusHub, config: dict) -> None:
|
|
|
|
"""Initialize the switch."""
|
|
|
|
super().__init__(hub, config)
|
|
|
|
self._swap = config[CONF_SWAP]
|
|
|
|
self._data_type = config[CONF_DATA_TYPE]
|
|
|
|
self._structure = config.get(CONF_STRUCTURE)
|
|
|
|
self._precision = config[CONF_PRECISION]
|
|
|
|
self._scale = config[CONF_SCALE]
|
|
|
|
self._offset = config[CONF_OFFSET]
|
|
|
|
self._count = config[CONF_COUNT]
|
|
|
|
|
|
|
|
def _swap_registers(self, registers):
|
|
|
|
"""Do swap as needed."""
|
2021-07-29 23:20:03 +00:00
|
|
|
if self._swap in (CONF_SWAP_BYTE, CONF_SWAP_WORD_BYTE):
|
2021-07-20 04:52:58 +00:00
|
|
|
# convert [12][34] --> [21][43]
|
|
|
|
for i, register in enumerate(registers):
|
|
|
|
registers[i] = int.from_bytes(
|
|
|
|
register.to_bytes(2, byteorder="little"),
|
|
|
|
byteorder="big",
|
|
|
|
signed=False,
|
|
|
|
)
|
2021-07-29 23:20:03 +00:00
|
|
|
if self._swap in (CONF_SWAP_WORD, CONF_SWAP_WORD_BYTE):
|
2021-07-20 04:52:58 +00:00
|
|
|
# convert [12][34] ==> [34][12]
|
|
|
|
registers.reverse()
|
|
|
|
return registers
|
|
|
|
|
|
|
|
def unpack_structure_result(self, registers):
|
|
|
|
"""Convert registers to proper result."""
|
|
|
|
|
|
|
|
registers = self._swap_registers(registers)
|
|
|
|
byte_string = b"".join([x.to_bytes(2, byteorder="big") for x in registers])
|
|
|
|
if self._data_type == DATA_TYPE_STRING:
|
2021-08-08 21:23:21 +00:00
|
|
|
return byte_string.decode()
|
2021-07-20 04:52:58 +00:00
|
|
|
|
2021-08-08 21:23:21 +00:00
|
|
|
val = struct.unpack(self._structure, byte_string)
|
|
|
|
# Issue: https://github.com/home-assistant/core/issues/41944
|
|
|
|
# If unpack() returns a tuple greater than 1, don't try to process the value.
|
|
|
|
# Instead, return the values of unpack(...) separated by commas.
|
|
|
|
if len(val) > 1:
|
|
|
|
# Apply scale and precision to floats and ints
|
|
|
|
v_result = []
|
|
|
|
for entry in val:
|
|
|
|
v_temp = self._scale * entry + self._offset
|
2021-07-20 04:52:58 +00:00
|
|
|
|
|
|
|
# We could convert int to float, and the code would still work; however
|
|
|
|
# we lose some precision, and unit tests will fail. Therefore, we do
|
|
|
|
# the conversion only when it's absolutely necessary.
|
2021-08-08 21:23:21 +00:00
|
|
|
if isinstance(v_temp, int) and self._precision == 0:
|
|
|
|
v_result.append(str(v_temp))
|
2021-07-20 04:52:58 +00:00
|
|
|
else:
|
2021-08-08 21:23:21 +00:00
|
|
|
v_result.append(f"{float(v_temp):.{self._precision}f}")
|
|
|
|
return ",".join(map(str, v_result))
|
|
|
|
|
|
|
|
# Apply scale and precision to floats and ints
|
|
|
|
val = self._scale * val[0] + self._offset
|
2021-07-20 04:52:58 +00:00
|
|
|
|
2021-08-08 21:23:21 +00:00
|
|
|
# We could convert int to float, and the code would still work; however
|
|
|
|
# we lose some precision, and unit tests will fail. Therefore, we do
|
|
|
|
# the conversion only when it's absolutely necessary.
|
|
|
|
if isinstance(val, int) and self._precision == 0:
|
|
|
|
return str(val)
|
|
|
|
return f"{float(val):.{self._precision}f}"
|
2021-07-20 04:52:58 +00:00
|
|
|
|
2021-08-08 21:23:21 +00:00
|
|
|
|
|
|
|
class BaseSwitch(BasePlatform, ToggleEntity, RestoreEntity):
|
2021-05-24 18:13:25 +00:00
|
|
|
"""Base class representing a Modbus switch."""
|
|
|
|
|
|
|
|
def __init__(self, hub: ModbusHub, config: dict) -> None:
|
|
|
|
"""Initialize the switch."""
|
|
|
|
config[CONF_INPUT_TYPE] = ""
|
|
|
|
super().__init__(hub, config)
|
2021-08-08 21:23:21 +00:00
|
|
|
self._attr_is_on = False
|
2021-07-12 18:22:53 +00:00
|
|
|
convert = {
|
|
|
|
CALL_TYPE_REGISTER_HOLDING: (
|
|
|
|
CALL_TYPE_REGISTER_HOLDING,
|
|
|
|
CALL_TYPE_WRITE_REGISTER,
|
|
|
|
),
|
|
|
|
CALL_TYPE_COIL: (CALL_TYPE_COIL, CALL_TYPE_WRITE_COIL),
|
|
|
|
CALL_TYPE_X_COILS: (CALL_TYPE_COIL, CALL_TYPE_WRITE_COILS),
|
|
|
|
CALL_TYPE_X_REGISTER_HOLDINGS: (
|
|
|
|
CALL_TYPE_REGISTER_HOLDING,
|
|
|
|
CALL_TYPE_WRITE_REGISTERS,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
self._write_type = convert[config[CONF_WRITE_TYPE]][1]
|
2021-05-24 18:13:25 +00:00
|
|
|
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] = {}
|
|
|
|
self._verify_active = True
|
|
|
|
self._verify_delay = config[CONF_VERIFY].get(CONF_DELAY, 0)
|
|
|
|
self._verify_address = config[CONF_VERIFY].get(
|
|
|
|
CONF_ADDRESS, config[CONF_ADDRESS]
|
|
|
|
)
|
2021-08-16 02:57:37 +00:00
|
|
|
self._verify_type = convert[
|
|
|
|
config[CONF_VERIFY].get(CONF_INPUT_TYPE, config[CONF_WRITE_TYPE])
|
|
|
|
][0]
|
2021-05-24 18:13:25 +00:00
|
|
|
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)
|
|
|
|
else:
|
|
|
|
self._verify_active = False
|
|
|
|
|
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Handle entity which will be added."""
|
|
|
|
await self.async_base_added_to_hass()
|
|
|
|
state = await self.async_get_last_state()
|
|
|
|
if state:
|
2021-08-08 21:23:21 +00:00
|
|
|
self._attr_is_on = state.state == STATE_ON
|
2021-05-24 18:13:25 +00:00
|
|
|
|
|
|
|
async def async_turn(self, command):
|
|
|
|
"""Evaluate switch result."""
|
|
|
|
result = await self._hub.async_pymodbus_call(
|
|
|
|
self._slave, self._address, command, self._write_type
|
|
|
|
)
|
|
|
|
if result is None:
|
2021-07-26 19:20:34 +00:00
|
|
|
self._attr_available = False
|
2021-05-24 18:13:25 +00:00
|
|
|
self.async_write_ha_state()
|
|
|
|
return
|
|
|
|
|
2021-07-26 19:20:34 +00:00
|
|
|
self._attr_available = True
|
2021-05-24 18:13:25 +00:00
|
|
|
if not self._verify_active:
|
2021-08-08 21:23:21 +00:00
|
|
|
self._attr_is_on = command == self.command_on
|
2021-05-24 18:13:25 +00:00
|
|
|
self.async_write_ha_state()
|
|
|
|
return
|
|
|
|
|
|
|
|
if self._verify_delay:
|
|
|
|
async_call_later(self.hass, self._verify_delay, self.async_update)
|
|
|
|
else:
|
|
|
|
await self.async_update()
|
|
|
|
|
|
|
|
async def async_turn_off(self, **kwargs):
|
|
|
|
"""Set switch off."""
|
|
|
|
await self.async_turn(self._command_off)
|
|
|
|
|
|
|
|
async def async_update(self, now=None):
|
|
|
|
"""Update the entity state."""
|
|
|
|
# remark "now" is a dummy parameter to avoid problems with
|
|
|
|
# async_track_time_interval
|
|
|
|
if not self._verify_active:
|
2021-07-26 19:20:34 +00:00
|
|
|
self._attr_available = True
|
2021-05-24 18:13:25 +00:00
|
|
|
self.async_write_ha_state()
|
|
|
|
return
|
|
|
|
|
2021-07-13 19:45:42 +00:00
|
|
|
# do not allow multiple active calls to the same platform
|
|
|
|
if self._call_active:
|
|
|
|
return
|
|
|
|
self._call_active = True
|
2021-05-24 18:13:25 +00:00
|
|
|
result = await self._hub.async_pymodbus_call(
|
|
|
|
self._slave, self._verify_address, 1, self._verify_type
|
|
|
|
)
|
2021-07-13 19:45:42 +00:00
|
|
|
self._call_active = False
|
2021-05-24 18:13:25 +00:00
|
|
|
if result is None:
|
2021-07-26 19:20:34 +00:00
|
|
|
self._attr_available = False
|
2021-05-24 18:13:25 +00:00
|
|
|
self.async_write_ha_state()
|
|
|
|
return
|
|
|
|
|
2021-07-26 19:20:34 +00:00
|
|
|
self._attr_available = True
|
2021-05-24 18:13:25 +00:00
|
|
|
if self._verify_type == CALL_TYPE_COIL:
|
2021-08-08 21:23:21 +00:00
|
|
|
self._attr_is_on = bool(result.bits[0] & 1)
|
2021-05-24 18:13:25 +00:00
|
|
|
else:
|
|
|
|
value = int(result.registers[0])
|
|
|
|
if value == self._state_on:
|
2021-08-08 21:23:21 +00:00
|
|
|
self._attr_is_on = True
|
2021-05-24 18:13:25 +00:00
|
|
|
elif value == self._state_off:
|
2021-08-08 21:23:21 +00:00
|
|
|
self._attr_is_on = False
|
2021-05-24 18:13:25 +00:00
|
|
|
elif value is not None:
|
|
|
|
_LOGGER.error(
|
|
|
|
"Unexpected response from modbus device slave %s register %s, got 0x%2x",
|
|
|
|
self._slave,
|
|
|
|
self._verify_address,
|
|
|
|
value,
|
|
|
|
)
|
|
|
|
self.async_write_ha_state()
|