2020-02-10 21:56:40 +00:00
|
|
|
"""Support for Modbus Coil and Discrete Input sensors."""
|
2021-03-18 12:07:04 +00:00
|
|
|
from __future__ import annotations
|
2019-02-24 09:22:17 +00:00
|
|
|
|
2021-09-18 19:54:11 +00:00
|
|
|
from datetime import datetime
|
2022-02-21 18:22:50 +00:00
|
|
|
import logging
|
|
|
|
from typing import Any
|
2021-03-27 21:48:06 +00:00
|
|
|
|
2021-05-28 10:06:46 +00:00
|
|
|
from homeassistant.components.binary_sensor import BinarySensorEntity
|
2022-02-21 18:22:50 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_BINARY_SENSORS,
|
|
|
|
CONF_DEVICE_CLASS,
|
|
|
|
CONF_NAME,
|
|
|
|
STATE_ON,
|
|
|
|
)
|
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2021-09-18 19:54:11 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2021-05-22 11:38:05 +00:00
|
|
|
from homeassistant.helpers.restore_state import RestoreEntity
|
2021-04-19 08:13:32 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2022-02-21 18:22:50 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import (
|
|
|
|
CoordinatorEntity,
|
|
|
|
DataUpdateCoordinator,
|
|
|
|
)
|
2016-09-13 20:47:44 +00:00
|
|
|
|
2021-08-08 20:48:33 +00:00
|
|
|
from . import get_hub
|
2021-05-20 14:56:11 +00:00
|
|
|
from .base_platform import BasePlatform
|
2022-02-21 18:22:50 +00:00
|
|
|
from .const import CONF_SLAVE_COUNT
|
|
|
|
from .modbus import ModbusHub
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2021-03-27 21:48:06 +00:00
|
|
|
|
2021-05-15 17:54:17 +00:00
|
|
|
PARALLEL_UPDATES = 1
|
2021-03-27 21:48:06 +00:00
|
|
|
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2021-03-27 21:48:06 +00:00
|
|
|
async def async_setup_platform(
|
2021-04-19 08:13:32 +00:00
|
|
|
hass: HomeAssistant,
|
2021-03-27 21:48:06 +00:00
|
|
|
config: ConfigType,
|
2021-09-18 19:54:11 +00:00
|
|
|
async_add_entities: AddEntitiesCallback,
|
2021-03-27 21:48:06 +00:00
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
2021-09-18 19:54:11 +00:00
|
|
|
) -> None:
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Set up the Modbus binary sensors."""
|
2021-03-27 21:48:06 +00:00
|
|
|
|
2021-05-28 10:06:46 +00:00
|
|
|
if discovery_info is None: # pragma: no cover
|
|
|
|
return
|
2021-03-27 21:48:06 +00:00
|
|
|
|
2022-02-21 18:22:50 +00:00
|
|
|
sensors: list[ModbusBinarySensor | SlaveSensor] = []
|
|
|
|
hub = get_hub(hass, discovery_info[CONF_NAME])
|
2021-03-27 21:48:06 +00:00
|
|
|
for entry in discovery_info[CONF_BINARY_SENSORS]:
|
2022-02-21 18:22:50 +00:00
|
|
|
slave_count = entry.get(CONF_SLAVE_COUNT, 0)
|
|
|
|
sensor = ModbusBinarySensor(hub, entry, slave_count)
|
|
|
|
if slave_count > 0:
|
|
|
|
sensors.extend(await sensor.async_setup_slaves(hass, slave_count, entry))
|
|
|
|
sensors.append(sensor)
|
2021-03-27 21:48:06 +00:00
|
|
|
async_add_entities(sensors)
|
2016-09-13 20:47:44 +00:00
|
|
|
|
|
|
|
|
2021-05-22 11:38:05 +00:00
|
|
|
class ModbusBinarySensor(BasePlatform, RestoreEntity, BinarySensorEntity):
|
2020-02-10 21:56:40 +00:00
|
|
|
"""Modbus binary sensor."""
|
2016-09-13 20:47:44 +00:00
|
|
|
|
2022-02-21 18:22:50 +00:00
|
|
|
def __init__(self, hub: ModbusHub, entry: dict[str, Any], slave_count: int) -> None:
|
|
|
|
"""Initialize the Modbus binary sensor."""
|
|
|
|
self._count = slave_count + 1
|
|
|
|
self._coordinator: DataUpdateCoordinator[Any] | None = None
|
|
|
|
self._result = None
|
|
|
|
super().__init__(hub, entry)
|
|
|
|
|
|
|
|
async def async_setup_slaves(
|
|
|
|
self, hass: HomeAssistant, slave_count: int, entry: dict[str, Any]
|
|
|
|
) -> list[SlaveSensor]:
|
|
|
|
"""Add slaves as needed (1 read for multiple sensors)."""
|
|
|
|
|
|
|
|
# Add a dataCoordinator for each sensor that have slaves
|
|
|
|
# this ensures that idx = bit position of value in result
|
|
|
|
# polling is done with the base class
|
|
|
|
name = self._attr_name if self._attr_name else "modbus_sensor"
|
|
|
|
self._coordinator = DataUpdateCoordinator(
|
|
|
|
hass,
|
|
|
|
_LOGGER,
|
|
|
|
name=name,
|
|
|
|
)
|
|
|
|
|
|
|
|
slaves: list[SlaveSensor] = []
|
|
|
|
for idx in range(0, slave_count):
|
|
|
|
slaves.append(SlaveSensor(self._coordinator, idx, entry))
|
|
|
|
return slaves
|
|
|
|
|
2021-09-18 19:54:11 +00:00
|
|
|
async def async_added_to_hass(self) -> None:
|
2021-03-27 21:48:06 +00:00
|
|
|
"""Handle entity which will be added."""
|
2021-05-20 14:56:11 +00:00
|
|
|
await self.async_base_added_to_hass()
|
2021-10-30 14:30:13 +00:00
|
|
|
if state := await self.async_get_last_state():
|
2021-08-08 21:23:21 +00:00
|
|
|
self._attr_is_on = state.state == STATE_ON
|
2016-09-13 20:47:44 +00:00
|
|
|
|
2021-09-18 19:54:11 +00:00
|
|
|
async def async_update(self, now: datetime | None = None) -> None:
|
2016-09-13 20:47:44 +00:00
|
|
|
"""Update the state of the sensor."""
|
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-17 09:20:12 +00:00
|
|
|
result = await self._hub.async_pymodbus_call(
|
2022-02-21 18:22:50 +00:00
|
|
|
self._slave, self._address, self._count, self._input_type
|
2021-05-17 09:20:12 +00:00
|
|
|
)
|
2021-07-13 19:45:42 +00:00
|
|
|
self._call_active = False
|
2021-04-19 15:18:15 +00:00
|
|
|
if result is None:
|
2021-08-21 13:49:50 +00:00
|
|
|
if self._lazy_errors:
|
|
|
|
self._lazy_errors -= 1
|
|
|
|
return
|
|
|
|
self._lazy_errors = self._lazy_error_count
|
2021-07-26 19:20:34 +00:00
|
|
|
self._attr_available = False
|
2022-02-21 18:22:50 +00:00
|
|
|
self._result = None
|
|
|
|
else:
|
|
|
|
self._lazy_errors = self._lazy_error_count
|
|
|
|
self._attr_is_on = result.bits[0] & 1
|
|
|
|
self._attr_available = True
|
|
|
|
self._result = result
|
2020-02-12 17:37:16 +00:00
|
|
|
|
2021-05-15 17:54:17 +00:00
|
|
|
self.async_write_ha_state()
|
2022-02-21 18:22:50 +00:00
|
|
|
if self._coordinator:
|
|
|
|
self._coordinator.async_set_updated_data(self._result)
|
|
|
|
|
|
|
|
|
|
|
|
class SlaveSensor(CoordinatorEntity, RestoreEntity, BinarySensorEntity):
|
|
|
|
"""Modbus slave binary sensor."""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self, coordinator: DataUpdateCoordinator[Any], idx: int, entry: dict[str, Any]
|
|
|
|
) -> None:
|
|
|
|
"""Initialize the Modbus binary sensor."""
|
|
|
|
idx += 1
|
|
|
|
self._attr_name = f"{entry[CONF_NAME]}_{idx}"
|
|
|
|
self._attr_device_class = entry.get(CONF_DEVICE_CLASS)
|
|
|
|
self._attr_available = False
|
|
|
|
self._result_inx = int(idx / 8)
|
|
|
|
self._result_bit = 2 ** (idx % 8)
|
|
|
|
super().__init__(coordinator)
|
|
|
|
|
|
|
|
async def async_added_to_hass(self) -> None:
|
|
|
|
"""Handle entity which will be added."""
|
|
|
|
if state := await self.async_get_last_state():
|
|
|
|
self._attr_is_on = state.state == STATE_ON
|
|
|
|
self.async_write_ha_state()
|
|
|
|
await super().async_added_to_hass()
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def _handle_coordinator_update(self) -> None:
|
|
|
|
"""Handle updated data from the coordinator."""
|
|
|
|
result = self.coordinator.data
|
|
|
|
if result:
|
|
|
|
self._attr_is_on = result.bits[self._result_inx] & self._result_bit
|
|
|
|
super()._handle_coordinator_update()
|