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
|
2021-03-27 21:48:06 +00:00
|
|
|
|
2021-05-28 10:06:46 +00:00
|
|
|
from homeassistant.components.binary_sensor import BinarySensorEntity
|
|
|
|
from homeassistant.const import CONF_BINARY_SENSORS, CONF_NAME, STATE_ON
|
2021-04-19 08:13:32 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
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
|
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
|
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."""
|
2016-09-13 20:47:44 +00:00
|
|
|
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
|
|
|
|
|
|
|
for entry in discovery_info[CONF_BINARY_SENSORS]:
|
2021-08-08 20:48:33 +00:00
|
|
|
hub = get_hub(hass, discovery_info[CONF_NAME])
|
2021-05-20 14:56:11 +00:00
|
|
|
sensors.append(ModbusBinarySensor(hub, entry))
|
2019-02-24 09:22:17 +00:00
|
|
|
|
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
|
|
|
|
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-05-22 11:38:05 +00:00
|
|
|
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
|
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(
|
|
|
|
self._slave, self._address, 1, self._input_type
|
|
|
|
)
|
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
|
2021-05-15 17:54:17 +00:00
|
|
|
self.async_write_ha_state()
|
2020-02-12 17:37:16 +00:00
|
|
|
return
|
|
|
|
|
2021-08-21 13:49:50 +00:00
|
|
|
self._lazy_errors = self._lazy_error_count
|
2021-08-08 21:23:21 +00:00
|
|
|
self._attr_is_on = result.bits[0] & 1
|
2021-07-26 19:20:34 +00:00
|
|
|
self._attr_available = True
|
2021-05-15 17:54:17 +00:00
|
|
|
self.async_write_ha_state()
|