2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Tellstick Net/Telstick Live sensors."""
|
2021-09-27 10:28:58 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2018-12-10 17:44:45 +00:00
|
|
|
from homeassistant.components import sensor, tellduslive
|
2021-09-27 10:28:58 +00:00
|
|
|
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
|
2018-05-05 13:37:40 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
DEVICE_CLASS_HUMIDITY,
|
|
|
|
DEVICE_CLASS_ILLUMINANCE,
|
|
|
|
DEVICE_CLASS_TEMPERATURE,
|
2020-09-23 02:09:17 +00:00
|
|
|
LENGTH_MILLIMETERS,
|
2020-09-23 18:48:01 +00:00
|
|
|
LIGHT_LUX,
|
2020-09-05 19:09:14 +00:00
|
|
|
PERCENTAGE,
|
2019-07-31 19:25:30 +00:00
|
|
|
POWER_WATT,
|
2020-11-09 09:09:53 +00:00
|
|
|
PRECIPITATION_MILLIMETERS_PER_HOUR,
|
2020-02-25 01:52:14 +00:00
|
|
|
SPEED_METERS_PER_SECOND,
|
2019-07-31 19:25:30 +00:00
|
|
|
TEMP_CELSIUS,
|
2020-04-21 17:45:53 +00:00
|
|
|
UV_INDEX,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-12-10 17:44:45 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2015-12-27 11:32:08 +00:00
|
|
|
|
2019-03-21 05:56:46 +00:00
|
|
|
from .entry import TelldusLiveEntity
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
SENSOR_TYPE_TEMPERATURE = "temp"
|
|
|
|
SENSOR_TYPE_HUMIDITY = "humidity"
|
|
|
|
SENSOR_TYPE_RAINRATE = "rrate"
|
|
|
|
SENSOR_TYPE_RAINTOTAL = "rtot"
|
|
|
|
SENSOR_TYPE_WINDDIRECTION = "wdir"
|
|
|
|
SENSOR_TYPE_WINDAVERAGE = "wavg"
|
|
|
|
SENSOR_TYPE_WINDGUST = "wgust"
|
|
|
|
SENSOR_TYPE_UV = "uv"
|
|
|
|
SENSOR_TYPE_WATT = "watt"
|
|
|
|
SENSOR_TYPE_LUMINANCE = "lum"
|
|
|
|
SENSOR_TYPE_DEW_POINT = "dewp"
|
|
|
|
SENSOR_TYPE_BAROMETRIC_PRESSURE = "barpress"
|
2015-12-27 11:32:08 +00:00
|
|
|
|
2021-09-27 10:28:58 +00:00
|
|
|
SENSOR_TYPES: dict[str, SensorEntityDescription] = {
|
|
|
|
SENSOR_TYPE_TEMPERATURE: SensorEntityDescription(
|
|
|
|
key=SENSOR_TYPE_TEMPERATURE,
|
|
|
|
name="Temperature",
|
|
|
|
native_unit_of_measurement=TEMP_CELSIUS,
|
|
|
|
device_class=DEVICE_CLASS_TEMPERATURE,
|
|
|
|
),
|
|
|
|
SENSOR_TYPE_HUMIDITY: SensorEntityDescription(
|
|
|
|
key=SENSOR_TYPE_HUMIDITY,
|
|
|
|
name="Humidity",
|
|
|
|
native_unit_of_measurement=PERCENTAGE,
|
|
|
|
device_class=DEVICE_CLASS_HUMIDITY,
|
|
|
|
),
|
|
|
|
SENSOR_TYPE_RAINRATE: SensorEntityDescription(
|
|
|
|
key=SENSOR_TYPE_RAINRATE,
|
|
|
|
name="Rain rate",
|
|
|
|
native_unit_of_measurement=PRECIPITATION_MILLIMETERS_PER_HOUR,
|
|
|
|
icon="mdi:water",
|
|
|
|
),
|
|
|
|
SENSOR_TYPE_RAINTOTAL: SensorEntityDescription(
|
|
|
|
key=SENSOR_TYPE_RAINTOTAL,
|
|
|
|
name="Rain total",
|
|
|
|
native_unit_of_measurement=LENGTH_MILLIMETERS,
|
|
|
|
icon="mdi:water",
|
|
|
|
),
|
|
|
|
SENSOR_TYPE_WINDDIRECTION: SensorEntityDescription(
|
|
|
|
key=SENSOR_TYPE_WINDDIRECTION,
|
|
|
|
name="Wind direction",
|
|
|
|
),
|
|
|
|
SENSOR_TYPE_WINDAVERAGE: SensorEntityDescription(
|
|
|
|
key=SENSOR_TYPE_WINDAVERAGE,
|
|
|
|
name="Wind average",
|
|
|
|
native_unit_of_measurement=SPEED_METERS_PER_SECOND,
|
|
|
|
),
|
|
|
|
SENSOR_TYPE_WINDGUST: SensorEntityDescription(
|
|
|
|
key=SENSOR_TYPE_WINDGUST,
|
|
|
|
name="Wind gust",
|
|
|
|
native_unit_of_measurement=SPEED_METERS_PER_SECOND,
|
|
|
|
),
|
|
|
|
SENSOR_TYPE_UV: SensorEntityDescription(
|
|
|
|
key=SENSOR_TYPE_UV,
|
|
|
|
name="UV",
|
|
|
|
native_unit_of_measurement=UV_INDEX,
|
|
|
|
),
|
|
|
|
SENSOR_TYPE_WATT: SensorEntityDescription(
|
|
|
|
key=SENSOR_TYPE_WATT,
|
|
|
|
name="Power",
|
|
|
|
native_unit_of_measurement=POWER_WATT,
|
|
|
|
),
|
|
|
|
SENSOR_TYPE_LUMINANCE: SensorEntityDescription(
|
|
|
|
key=SENSOR_TYPE_LUMINANCE,
|
|
|
|
name="Luminance",
|
|
|
|
native_unit_of_measurement=LIGHT_LUX,
|
|
|
|
device_class=DEVICE_CLASS_ILLUMINANCE,
|
|
|
|
),
|
|
|
|
SENSOR_TYPE_DEW_POINT: SensorEntityDescription(
|
|
|
|
key=SENSOR_TYPE_DEW_POINT,
|
|
|
|
name="Dew Point",
|
|
|
|
native_unit_of_measurement=TEMP_CELSIUS,
|
|
|
|
device_class=DEVICE_CLASS_TEMPERATURE,
|
|
|
|
),
|
|
|
|
SENSOR_TYPE_BAROMETRIC_PRESSURE: SensorEntityDescription(
|
|
|
|
key=SENSOR_TYPE_BAROMETRIC_PRESSURE,
|
|
|
|
name="Barometric Pressure",
|
|
|
|
native_unit_of_measurement="kPa",
|
|
|
|
),
|
2015-12-27 11:32:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-10 17:44:45 +00:00
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Set up tellduslive sensors dynamically."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2018-12-10 17:44:45 +00:00
|
|
|
async def async_discover_sensor(device_id):
|
|
|
|
"""Discover and add a discovered sensor."""
|
|
|
|
client = hass.data[tellduslive.DOMAIN]
|
|
|
|
async_add_entities([TelldusLiveSensor(client, device_id)])
|
|
|
|
|
|
|
|
async_dispatcher_connect(
|
|
|
|
hass,
|
2019-07-31 19:25:30 +00:00
|
|
|
tellduslive.TELLDUS_DISCOVERY_NEW.format(sensor.DOMAIN, tellduslive.DOMAIN),
|
|
|
|
async_discover_sensor,
|
|
|
|
)
|
2015-12-27 11:32:08 +00:00
|
|
|
|
|
|
|
|
2021-03-22 18:47:44 +00:00
|
|
|
class TelldusLiveSensor(TelldusLiveEntity, SensorEntity):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Representation of a Telldus Live sensor."""
|
2015-12-27 11:32:08 +00:00
|
|
|
|
2021-09-27 10:28:58 +00:00
|
|
|
def __init__(self, client, device_id):
|
|
|
|
"""Initialize TelldusLiveSensor."""
|
|
|
|
super().__init__(client, device_id)
|
|
|
|
if desc := SENSOR_TYPES.get(self._type):
|
|
|
|
self.entity_description = desc
|
|
|
|
|
2016-02-03 21:31:28 +00:00
|
|
|
@property
|
2016-12-12 05:39:37 +00:00
|
|
|
def device_id(self):
|
|
|
|
"""Return id of the device."""
|
|
|
|
return self._id[0]
|
2016-02-03 21:31:28 +00:00
|
|
|
|
|
|
|
@property
|
2016-12-12 05:39:37 +00:00
|
|
|
def _type(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the type of the sensor."""
|
2016-12-12 05:39:37 +00:00
|
|
|
return self._id[1]
|
2016-02-03 21:31:28 +00:00
|
|
|
|
|
|
|
@property
|
2016-12-12 05:39:37 +00:00
|
|
|
def _value(self):
|
|
|
|
"""Return value of the sensor."""
|
2018-08-26 19:25:39 +00:00
|
|
|
return self.device.value(*self._id[1:])
|
2016-02-03 21:31:28 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def _value_as_temperature(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the value as temperature."""
|
2016-12-12 05:39:37 +00:00
|
|
|
return round(float(self._value), 1)
|
2016-02-03 21:31:28 +00:00
|
|
|
|
2016-07-20 02:16:31 +00:00
|
|
|
@property
|
|
|
|
def _value_as_luminance(self):
|
|
|
|
"""Return the value as luminance."""
|
2016-12-12 05:39:37 +00:00
|
|
|
return round(float(self._value), 1)
|
2016-07-20 02:16:31 +00:00
|
|
|
|
2016-02-03 21:31:28 +00:00
|
|
|
@property
|
|
|
|
def _value_as_humidity(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the value as humidity."""
|
2016-12-12 05:39:37 +00:00
|
|
|
return int(round(float(self._value)))
|
2015-12-27 11:32:08 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the name of the sensor."""
|
2021-09-27 10:28:58 +00:00
|
|
|
quantity_name = (
|
|
|
|
self.entity_description.name if hasattr(self, "entity_description") else ""
|
|
|
|
)
|
|
|
|
return "{} {}".format(super().name, quantity_name or "").strip()
|
2015-12-27 11:32:08 +00:00
|
|
|
|
|
|
|
@property
|
2021-08-11 16:57:50 +00:00
|
|
|
def native_value(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the state of the sensor."""
|
2017-02-09 17:00:18 +00:00
|
|
|
if not self.available:
|
|
|
|
return None
|
2018-07-23 08:16:05 +00:00
|
|
|
if self._type == SENSOR_TYPE_TEMPERATURE:
|
2016-02-03 21:31:28 +00:00
|
|
|
return self._value_as_temperature
|
2018-07-23 08:16:05 +00:00
|
|
|
if self._type == SENSOR_TYPE_HUMIDITY:
|
2016-02-03 21:31:28 +00:00
|
|
|
return self._value_as_humidity
|
2018-07-23 08:16:05 +00:00
|
|
|
if self._type == SENSOR_TYPE_LUMINANCE:
|
2016-07-20 02:16:31 +00:00
|
|
|
return self._value_as_luminance
|
2017-07-06 06:30:01 +00:00
|
|
|
return self._value
|
2015-12-27 11:32:08 +00:00
|
|
|
|
2018-11-27 14:35:51 +00:00
|
|
|
@property
|
|
|
|
def unique_id(self) -> str:
|
|
|
|
"""Return a unique ID."""
|
2018-12-23 18:13:49 +00:00
|
|
|
return "{}-{}-{}".format(*self._id)
|