2020-02-10 22:56:40 +01:00
|
|
|
"""Support for Modbus Coil and Discrete Input sensors."""
|
2021-03-18 13:07:04 +01:00
|
|
|
from __future__ import annotations
|
2019-02-24 10:22:17 +01:00
|
|
|
|
2021-03-27 22:48:06 +01:00
|
|
|
import logging
|
|
|
|
|
2021-05-28 12:06:46 +02:00
|
|
|
from homeassistant.components.binary_sensor import BinarySensorEntity
|
|
|
|
from homeassistant.const import CONF_BINARY_SENSORS, CONF_NAME, STATE_ON
|
2021-04-19 10:13:32 +02:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-05-22 13:38:05 +02:00
|
|
|
from homeassistant.helpers.restore_state import RestoreEntity
|
2021-04-19 10:13:32 +02:00
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2016-09-13 22:47:44 +02:00
|
|
|
|
2021-08-08 22:48:33 +02:00
|
|
|
from . import get_hub
|
2021-05-20 16:56:11 +02:00
|
|
|
from .base_platform import BasePlatform
|
2021-03-27 22:48:06 +01:00
|
|
|
|
2021-05-15 19:54:17 +02:00
|
|
|
PARALLEL_UPDATES = 1
|
2021-03-27 22:48:06 +01:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-03-20 22:56:46 -07:00
|
|
|
|
2021-03-27 22:48:06 +01:00
|
|
|
async def async_setup_platform(
|
2021-04-19 10:13:32 +02:00
|
|
|
hass: HomeAssistant,
|
2021-03-27 22:48:06 +01:00
|
|
|
config: ConfigType,
|
|
|
|
async_add_entities,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
):
|
2017-04-30 07:04:49 +02:00
|
|
|
"""Set up the Modbus binary sensors."""
|
2016-09-13 22:47:44 +02:00
|
|
|
sensors = []
|
2021-03-27 22:48:06 +01:00
|
|
|
|
2021-05-28 12:06:46 +02:00
|
|
|
if discovery_info is None: # pragma: no cover
|
|
|
|
return
|
2021-03-27 22:48:06 +01:00
|
|
|
|
|
|
|
for entry in discovery_info[CONF_BINARY_SENSORS]:
|
2021-08-08 22:48:33 +02:00
|
|
|
hub = get_hub(hass, discovery_info[CONF_NAME])
|
2021-05-20 16:56:11 +02:00
|
|
|
sensors.append(ModbusBinarySensor(hub, entry))
|
2019-02-24 10:22:17 +01:00
|
|
|
|
2021-03-27 22:48:06 +01:00
|
|
|
async_add_entities(sensors)
|
2016-09-13 22:47:44 +02:00
|
|
|
|
|
|
|
|
2021-05-22 13:38:05 +02:00
|
|
|
class ModbusBinarySensor(BasePlatform, RestoreEntity, BinarySensorEntity):
|
2020-02-10 22:56:40 +01:00
|
|
|
"""Modbus binary sensor."""
|
2016-09-13 22:47:44 +02:00
|
|
|
|
2021-03-27 22:48:06 +01:00
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Handle entity which will be added."""
|
2021-05-20 16:56:11 +02:00
|
|
|
await self.async_base_added_to_hass()
|
2021-05-22 13:38:05 +02:00
|
|
|
state = await self.async_get_last_state()
|
|
|
|
if state:
|
2021-08-08 23:23:21 +02:00
|
|
|
self._attr_is_on = state.state == STATE_ON
|
2016-09-13 22:47:44 +02:00
|
|
|
|
2021-05-15 19:54:17 +02:00
|
|
|
async def async_update(self, now=None):
|
2016-09-13 22:47:44 +02:00
|
|
|
"""Update the state of the sensor."""
|
2021-07-13 21:45:42 +02:00
|
|
|
|
|
|
|
# do not allow multiple active calls to the same platform
|
|
|
|
if self._call_active:
|
|
|
|
return
|
|
|
|
self._call_active = True
|
2021-05-17 11:20:12 +02:00
|
|
|
result = await self._hub.async_pymodbus_call(
|
|
|
|
self._slave, self._address, 1, self._input_type
|
|
|
|
)
|
2021-07-13 21:45:42 +02:00
|
|
|
self._call_active = False
|
2021-04-19 17:18:15 +02:00
|
|
|
if result is None:
|
2021-08-21 15:49:50 +02:00
|
|
|
if self._lazy_errors:
|
|
|
|
self._lazy_errors -= 1
|
|
|
|
return
|
|
|
|
self._lazy_errors = self._lazy_error_count
|
2021-07-26 21:20:34 +02:00
|
|
|
self._attr_available = False
|
2021-05-15 19:54:17 +02:00
|
|
|
self.async_write_ha_state()
|
2020-02-12 18:37:16 +01:00
|
|
|
return
|
|
|
|
|
2021-08-21 15:49:50 +02:00
|
|
|
self._lazy_errors = self._lazy_error_count
|
2021-08-08 23:23:21 +02:00
|
|
|
self._attr_is_on = result.bits[0] & 1
|
2021-07-26 21:20:34 +02:00
|
|
|
self._attr_available = True
|
2021-05-15 19:54:17 +02:00
|
|
|
self.async_write_ha_state()
|