2019-02-14 05:35:12 +01:00
|
|
|
"""Support for Modbus Register sensors."""
|
2021-03-18 13:07:04 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2015-04-15 16:47:42 +02:00
|
|
|
import logging
|
2021-05-26 20:28:14 +03:00
|
|
|
from typing import Any
|
2019-12-06 06:10:29 +01:00
|
|
|
|
2021-05-26 20:28:14 +03:00
|
|
|
from homeassistant.components.sensor import SensorEntity
|
2021-07-20 06:52:58 +02:00
|
|
|
from homeassistant.const import CONF_NAME, CONF_SENSORS, CONF_UNIT_OF_MEASUREMENT
|
2021-04-19 10:13:32 +02:00
|
|
|
from homeassistant.core import HomeAssistant
|
2019-02-24 10:22:17 +01:00
|
|
|
from homeassistant.helpers.restore_state import RestoreEntity
|
2021-04-19 10:13:32 +02:00
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2015-04-15 16:47:42 +02:00
|
|
|
|
2021-08-08 22:48:33 +02:00
|
|
|
from . import get_hub
|
2021-07-20 06:52:58 +02:00
|
|
|
from .base_platform import BaseStructPlatform
|
2021-05-26 20:28:14 +03:00
|
|
|
from .modbus import ModbusHub
|
2019-03-20 22:56:46 -07:00
|
|
|
|
2021-05-15 19:54:17 +02:00
|
|
|
PARALLEL_UPDATES = 1
|
2015-04-15 16:47:42 +02:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2017-05-02 18:18:47 +02:00
|
|
|
|
2019-08-10 03:03:12 +03: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,
|
|
|
|
):
|
2016-10-30 09:58:34 +01:00
|
|
|
"""Set up the Modbus sensors."""
|
2015-04-15 16:47:42 +02:00
|
|
|
sensors = []
|
2017-11-13 23:27:15 +01:00
|
|
|
|
2021-05-26 20:28:14 +03:00
|
|
|
if discovery_info is None: # pragma: no cover
|
|
|
|
return
|
2021-03-27 22:48:06 +01:00
|
|
|
|
|
|
|
for entry in discovery_info[CONF_SENSORS]:
|
2021-08-08 22:48:33 +02:00
|
|
|
hub = get_hub(hass, discovery_info[CONF_NAME])
|
2021-05-26 20:28:14 +03:00
|
|
|
sensors.append(ModbusRegisterSensor(hub, entry))
|
2017-11-13 23:27:15 +01:00
|
|
|
|
2021-05-27 08:28:31 +02:00
|
|
|
async_add_entities(sensors)
|
2015-04-15 16:47:42 +02:00
|
|
|
|
2015-04-21 16:40:13 +02:00
|
|
|
|
2021-07-20 06:52:58 +02:00
|
|
|
class ModbusRegisterSensor(BaseStructPlatform, RestoreEntity, SensorEntity):
|
2017-09-23 17:15:46 +02:00
|
|
|
"""Modbus register sensor."""
|
2015-04-15 16:47:42 +02:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
def __init__(
|
|
|
|
self,
|
2021-05-26 20:28:14 +03:00
|
|
|
hub: ModbusHub,
|
|
|
|
entry: dict[str, Any],
|
|
|
|
) -> None:
|
2016-09-13 22:47:44 +02:00
|
|
|
"""Initialize the modbus register sensor."""
|
2021-05-20 16:56:11 +02:00
|
|
|
super().__init__(hub, entry)
|
2021-07-26 21:20:34 +02:00
|
|
|
self._attr_unit_of_measurement = entry.get(CONF_UNIT_OF_MEASUREMENT)
|
2015-04-15 16:47:42 +02:00
|
|
|
|
2019-02-11 20:00:37 +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()
|
2019-02-11 20:00:37 +01:00
|
|
|
state = await self.async_get_last_state()
|
2021-03-27 22:48:06 +01:00
|
|
|
if state:
|
|
|
|
self._value = state.state
|
|
|
|
|
2015-04-15 16:47:42 +02:00
|
|
|
@property
|
|
|
|
def state(self):
|
2016-03-08 16:46:34 +01:00
|
|
|
"""Return the state of the sensor."""
|
2016-09-13 22:47:44 +02:00
|
|
|
return self._value
|
2015-04-15 16:47:42 +02:00
|
|
|
|
2021-05-15 19:54:17 +02:00
|
|
|
async def async_update(self, now=None):
|
2016-02-23 06:21:49 +01:00
|
|
|
"""Update the state of the sensor."""
|
2021-05-15 19:54:17 +02:00
|
|
|
# remark "now" is a dummy parameter to avoid problems with
|
|
|
|
# async_track_time_interval
|
2021-05-17 11:20:12 +02:00
|
|
|
result = await self._hub.async_pymodbus_call(
|
2021-05-26 20:28:14 +03:00
|
|
|
self._slave, self._address, self._count, self._input_type
|
2021-05-17 11:20:12 +02:00
|
|
|
)
|
2021-04-19 17:18:15 +02:00
|
|
|
if result is None:
|
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-07-20 06:52:58 +02:00
|
|
|
self.unpack_structure_result(result.registers)
|
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()
|