2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Tellstick Net/Telstick Live sensors."""
|
2015-12-27 11:32:08 +00:00
|
|
|
import logging
|
|
|
|
|
2018-12-10 17:44:45 +00:00
|
|
|
from homeassistant.components import sensor, tellduslive
|
2018-05-05 13:37:40 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
DEVICE_CLASS_HUMIDITY, DEVICE_CLASS_ILLUMINANCE, DEVICE_CLASS_TEMPERATURE,
|
2019-03-21 05:56:46 +00:00
|
|
|
POWER_WATT, TEMP_CELSIUS)
|
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
|
|
|
|
|
2015-12-27 11:32:08 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2017-11-27 09:11:00 +00:00
|
|
|
SENSOR_TYPE_TEMPERATURE = 'temp'
|
2016-12-12 05:39:37 +00:00
|
|
|
SENSOR_TYPE_HUMIDITY = 'humidity'
|
|
|
|
SENSOR_TYPE_RAINRATE = 'rrate'
|
|
|
|
SENSOR_TYPE_RAINTOTAL = 'rtot'
|
|
|
|
SENSOR_TYPE_WINDDIRECTION = 'wdir'
|
|
|
|
SENSOR_TYPE_WINDAVERAGE = 'wavg'
|
|
|
|
SENSOR_TYPE_WINDGUST = 'wgust'
|
2017-11-27 09:11:00 +00:00
|
|
|
SENSOR_TYPE_UV = 'uv'
|
2016-12-12 05:39:37 +00:00
|
|
|
SENSOR_TYPE_WATT = 'watt'
|
|
|
|
SENSOR_TYPE_LUMINANCE = 'lum'
|
2017-11-27 09:11:00 +00:00
|
|
|
SENSOR_TYPE_DEW_POINT = 'dewp'
|
|
|
|
SENSOR_TYPE_BAROMETRIC_PRESSURE = 'barpress'
|
2015-12-27 11:32:08 +00:00
|
|
|
|
|
|
|
SENSOR_TYPES = {
|
2018-12-04 09:08:40 +00:00
|
|
|
SENSOR_TYPE_TEMPERATURE:
|
|
|
|
['Temperature', TEMP_CELSIUS, None, DEVICE_CLASS_TEMPERATURE],
|
2018-05-05 13:37:40 +00:00
|
|
|
SENSOR_TYPE_HUMIDITY: ['Humidity', '%', None, DEVICE_CLASS_HUMIDITY],
|
|
|
|
SENSOR_TYPE_RAINRATE: ['Rain rate', 'mm/h', 'mdi:water', None],
|
|
|
|
SENSOR_TYPE_RAINTOTAL: ['Rain total', 'mm', 'mdi:water', None],
|
|
|
|
SENSOR_TYPE_WINDDIRECTION: ['Wind direction', '', '', None],
|
|
|
|
SENSOR_TYPE_WINDAVERAGE: ['Wind average', 'm/s', '', None],
|
|
|
|
SENSOR_TYPE_WINDGUST: ['Wind gust', 'm/s', '', None],
|
|
|
|
SENSOR_TYPE_UV: ['UV', 'UV', '', None],
|
2019-03-02 10:29:59 +00:00
|
|
|
SENSOR_TYPE_WATT: ['Power', POWER_WATT, '', None],
|
2018-05-05 13:37:40 +00:00
|
|
|
SENSOR_TYPE_LUMINANCE: ['Luminance', 'lx', None, DEVICE_CLASS_ILLUMINANCE],
|
|
|
|
SENSOR_TYPE_DEW_POINT:
|
2018-12-04 09:08:40 +00:00
|
|
|
['Dew Point', TEMP_CELSIUS, None, DEVICE_CLASS_TEMPERATURE],
|
2018-05-05 13:37:40 +00:00
|
|
|
SENSOR_TYPE_BAROMETRIC_PRESSURE: ['Barometric Pressure', 'kPa', '', None],
|
2015-12-27 11:32:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-01 17:01:31 +00:00
|
|
|
async def async_setup_platform(
|
|
|
|
hass, config, async_add_entities, discovery_info=None):
|
2018-12-10 17:44:45 +00:00
|
|
|
"""Old way of setting up TelldusLive.
|
|
|
|
|
|
|
|
Can only be called when a user accidentally mentions the platform in their
|
|
|
|
config. But even in that case it would have been ignored.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Set up tellduslive sensors dynamically."""
|
|
|
|
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,
|
|
|
|
tellduslive.TELLDUS_DISCOVERY_NEW.format(
|
|
|
|
sensor.DOMAIN, tellduslive.DOMAIN), async_discover_sensor)
|
2015-12-27 11:32:08 +00:00
|
|
|
|
|
|
|
|
2016-12-12 05:39:37 +00:00
|
|
|
class TelldusLiveSensor(TelldusLiveEntity):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Representation of a Telldus Live sensor."""
|
2015-12-27 11:32:08 +00:00
|
|
|
|
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."""
|
2018-12-04 09:08:40 +00:00
|
|
|
return '{} {}'.format(super().name, self.quantity_name or '').strip()
|
2015-12-27 11:32:08 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state(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
|
|
|
|
2016-02-03 21:31:28 +00:00
|
|
|
@property
|
|
|
|
def quantity_name(self):
|
2016-02-23 05:21:49 +00:00
|
|
|
"""Name of quantity."""
|
2016-12-12 05:39:37 +00:00
|
|
|
return SENSOR_TYPES[self._type][0] \
|
|
|
|
if self._type in SENSOR_TYPES else None
|
2016-02-03 21:31:28 +00:00
|
|
|
|
2015-12-27 11:32:08 +00:00
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the unit of measurement."""
|
2016-12-12 05:39:37 +00:00
|
|
|
return SENSOR_TYPES[self._type][1] \
|
|
|
|
if self._type in SENSOR_TYPES else None
|
2015-12-27 11:32:08 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the icon."""
|
2016-12-12 05:39:37 +00:00
|
|
|
return SENSOR_TYPES[self._type][2] \
|
|
|
|
if self._type in SENSOR_TYPES else None
|
2018-05-05 13:37:40 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Return the device class."""
|
|
|
|
return SENSOR_TYPES[self._type][3] \
|
|
|
|
if self._type in SENSOR_TYPES else None
|
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)
|