2019-02-13 20:21:14 +00:00
|
|
|
"""Support for ADS sensors."""
|
2017-12-05 08:44:22 +00:00
|
|
|
import voluptuous as vol
|
2018-01-21 06:35:38 +00:00
|
|
|
|
|
|
|
from homeassistant.components import ads
|
2021-03-22 11:37:16 +00:00
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
|
2017-12-05 08:44:22 +00:00
|
|
|
from homeassistant.const import CONF_NAME, CONF_UNIT_OF_MEASUREMENT
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
2021-07-05 10:03:16 +00:00
|
|
|
from homeassistant.helpers.typing import StateType
|
2017-12-05 08:44:22 +00:00
|
|
|
|
2019-12-08 12:43:32 +00:00
|
|
|
from . import CONF_ADS_FACTOR, CONF_ADS_TYPE, CONF_ADS_VAR, STATE_KEY_STATE, AdsEntity
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2018-01-21 06:35:38 +00:00
|
|
|
DEFAULT_NAME = "ADS sensor"
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_ADS_VAR): cv.string,
|
|
|
|
vol.Optional(CONF_ADS_FACTOR): cv.positive_int,
|
|
|
|
vol.Optional(CONF_ADS_TYPE, default=ads.ADSTYPE_INT): vol.In(
|
|
|
|
[
|
|
|
|
ads.ADSTYPE_INT,
|
|
|
|
ads.ADSTYPE_UINT,
|
|
|
|
ads.ADSTYPE_BYTE,
|
|
|
|
ads.ADSTYPE_DINT,
|
|
|
|
ads.ADSTYPE_UDINT,
|
|
|
|
]
|
|
|
|
),
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
vol.Optional(CONF_UNIT_OF_MEASUREMENT, default=""): cv.string,
|
|
|
|
}
|
|
|
|
)
|
2017-12-05 08:44:22 +00:00
|
|
|
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-12-05 08:44:22 +00:00
|
|
|
"""Set up an ADS sensor device."""
|
|
|
|
ads_hub = hass.data.get(ads.DATA_ADS)
|
|
|
|
|
2020-04-08 10:59:38 +00:00
|
|
|
ads_var = config[CONF_ADS_VAR]
|
|
|
|
ads_type = config[CONF_ADS_TYPE]
|
|
|
|
name = config[CONF_NAME]
|
2017-12-05 08:44:22 +00:00
|
|
|
unit_of_measurement = config.get(CONF_UNIT_OF_MEASUREMENT)
|
|
|
|
factor = config.get(CONF_ADS_FACTOR)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
entity = AdsSensor(ads_hub, ads_var, ads_type, name, unit_of_measurement, factor)
|
2017-12-05 08:44:22 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities([entity])
|
2017-12-05 08:44:22 +00:00
|
|
|
|
|
|
|
|
2021-03-22 19:05:13 +00:00
|
|
|
class AdsSensor(AdsEntity, SensorEntity):
|
2017-12-05 08:44:22 +00:00
|
|
|
"""Representation of an ADS sensor entity."""
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
def __init__(self, ads_hub, ads_var, ads_type, name, unit_of_measurement, factor):
|
2017-12-05 08:44:22 +00:00
|
|
|
"""Initialize AdsSensor entity."""
|
2019-04-01 03:28:43 +00:00
|
|
|
super().__init__(ads_hub, name, ads_var)
|
2021-08-11 08:45:05 +00:00
|
|
|
self._attr_native_unit_of_measurement = unit_of_measurement
|
2019-04-01 03:28:43 +00:00
|
|
|
self._ads_type = ads_type
|
|
|
|
self._factor = factor
|
2017-12-05 08:44:22 +00:00
|
|
|
|
2018-10-01 06:55:43 +00:00
|
|
|
async def async_added_to_hass(self):
|
2017-12-05 08:44:22 +00:00
|
|
|
"""Register device notification."""
|
2019-04-01 03:28:43 +00:00
|
|
|
await self.async_initialize_device(
|
|
|
|
self._ads_var,
|
|
|
|
self._ads_hub.ADS_TYPEMAP[self._ads_type],
|
|
|
|
STATE_KEY_STATE,
|
2019-07-31 19:25:30 +00:00
|
|
|
self._factor,
|
|
|
|
)
|
2019-02-12 17:42:09 +00:00
|
|
|
|
2017-12-05 08:44:22 +00:00
|
|
|
@property
|
2021-08-11 08:45:05 +00:00
|
|
|
def native_value(self) -> StateType:
|
2017-12-05 08:44:22 +00:00
|
|
|
"""Return the state of the device."""
|
2019-04-01 03:28:43 +00:00
|
|
|
return self._state_dict[STATE_KEY_STATE]
|