2015-12-27 11:32:08 +00:00
|
|
|
"""
|
|
|
|
homeassistant.components.sensor.tellduslive
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
Shows sensor values from Tellstick Net/Telstick Live.
|
|
|
|
|
|
|
|
For more details about this platform, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/sensor.tellduslive/
|
|
|
|
|
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from datetime import datetime
|
|
|
|
|
2016-02-01 11:08:08 +00:00
|
|
|
from homeassistant.const import (TEMP_CELCIUS,
|
|
|
|
ATTR_BATTERY_LEVEL,
|
|
|
|
DEVICE_DEFAULT_NAME)
|
2015-12-27 11:32:08 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
from homeassistant.components import tellduslive
|
|
|
|
|
|
|
|
ATTR_LAST_UPDATED = "time_last_updated"
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
SENSOR_TYPE_TEMP = "temp"
|
|
|
|
SENSOR_TYPE_HUMIDITY = "humidity"
|
2016-01-02 21:21:04 +00:00
|
|
|
SENSOR_TYPE_RAINRATE = "rrate"
|
|
|
|
SENSOR_TYPE_RAINTOTAL = "rtot"
|
2016-01-02 21:28:15 +00:00
|
|
|
SENSOR_TYPE_WINDDIRECTION = "wdir"
|
|
|
|
SENSOR_TYPE_WINDAVERAGE = "wavg"
|
|
|
|
SENSOR_TYPE_WINDGUST = "wgust"
|
2016-01-02 21:30:02 +00:00
|
|
|
SENSOR_TYPE_WATT = "watt"
|
2015-12-27 11:32:08 +00:00
|
|
|
|
|
|
|
SENSOR_TYPES = {
|
|
|
|
SENSOR_TYPE_TEMP: ['Temperature', TEMP_CELCIUS, "mdi:thermometer"],
|
|
|
|
SENSOR_TYPE_HUMIDITY: ['Humidity', '%', "mdi:water"],
|
2016-01-03 00:00:10 +00:00
|
|
|
SENSOR_TYPE_RAINRATE: ['Rain rate', 'mm', "mdi:water"],
|
|
|
|
SENSOR_TYPE_RAINTOTAL: ['Rain total', 'mm', "mdi:water"],
|
|
|
|
SENSOR_TYPE_WINDDIRECTION: ['Wind direction', '', ""],
|
|
|
|
SENSOR_TYPE_WINDAVERAGE: ['Wind average', 'm/s', ""],
|
|
|
|
SENSOR_TYPE_WINDGUST: ['Wind gust', 'm/s', ""],
|
|
|
|
SENSOR_TYPE_WATT: ['Watt', 'W', ""],
|
2015-12-27 11:32:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
|
|
|
""" Sets up Tellstick sensors. """
|
2016-01-18 18:41:41 +00:00
|
|
|
if discovery_info is None:
|
|
|
|
return
|
2016-02-03 21:31:28 +00:00
|
|
|
add_devices(TelldusLiveSensor(sensor) for sensor in discovery_info)
|
2015-12-27 11:32:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TelldusLiveSensor(Entity):
|
|
|
|
""" Represents a Telldus Live sensor. """
|
|
|
|
|
2016-02-03 21:31:28 +00:00
|
|
|
def __init__(self, sensor_id):
|
|
|
|
self._id = sensor_id
|
2015-12-27 11:32:08 +00:00
|
|
|
self.update()
|
2016-02-03 21:31:28 +00:00
|
|
|
_LOGGER.debug("created sensor %s", self)
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
""" update sensor values """
|
|
|
|
tellduslive.NETWORK.update_sensors()
|
|
|
|
self._sensor = tellduslive.NETWORK.get_sensor(self._id)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def _sensor_name(self):
|
|
|
|
return self._sensor["name"]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def _sensor_value(self):
|
|
|
|
return self._sensor["data"]["value"]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def _sensor_type(self):
|
|
|
|
return self._sensor["data"]["name"]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def _battery_level(self):
|
|
|
|
sensor_battery_level = self._sensor.get("battery")
|
|
|
|
return round(sensor_battery_level * 100 / 255) \
|
|
|
|
if sensor_battery_level else None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def _last_updated(self):
|
|
|
|
sensor_last_updated = self._sensor.get("lastUpdated")
|
|
|
|
return str(datetime.fromtimestamp(sensor_last_updated)) \
|
|
|
|
if sensor_last_updated else None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def _value_as_temperature(self):
|
|
|
|
return round(float(self._sensor_value), 1)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def _value_as_humidity(self):
|
|
|
|
return int(round(float(self._sensor_value)))
|
2015-12-27 11:32:08 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
""" Returns the name of the device. """
|
2016-02-03 21:31:28 +00:00
|
|
|
return "{} {}".format(self._sensor_name or DEVICE_DEFAULT_NAME,
|
|
|
|
self.quantity_name)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self):
|
|
|
|
return not self._sensor.get("offline", False)
|
2015-12-27 11:32:08 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
""" Returns the state of the device. """
|
2016-02-03 21:31:28 +00:00
|
|
|
if self._sensor_type == SENSOR_TYPE_TEMP:
|
|
|
|
return self._value_as_temperature
|
|
|
|
elif self._sensor_type == SENSOR_TYPE_HUMIDITY:
|
|
|
|
return self._value_as_humidity
|
2015-12-27 11:32:08 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state_attributes(self):
|
|
|
|
attrs = dict()
|
|
|
|
if self._battery_level is not None:
|
|
|
|
attrs[ATTR_BATTERY_LEVEL] = self._battery_level
|
2016-02-03 21:31:28 +00:00
|
|
|
if self._last_updated is not None:
|
|
|
|
attrs[ATTR_LAST_UPDATED] = self._last_updated
|
2015-12-27 11:32:08 +00:00
|
|
|
return attrs
|
|
|
|
|
2016-02-03 21:31:28 +00:00
|
|
|
@property
|
|
|
|
def quantity_name(self):
|
|
|
|
""" name of quantity """
|
|
|
|
return SENSOR_TYPES[self._sensor_type][0]
|
|
|
|
|
2015-12-27 11:32:08 +00:00
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
return SENSOR_TYPES[self._sensor_type][1]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
return SENSOR_TYPES[self._sensor_type][2]
|