2015-12-27 11:32:08 +00:00
|
|
|
"""
|
2016-02-23 05:21:49 +00:00
|
|
|
Support for Tellstick Net/Telstick Live.
|
2015-12-27 11:32:08 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
from homeassistant.components import tellduslive
|
2016-02-19 05:27:50 +00:00
|
|
|
from homeassistant.const import (
|
2016-04-20 03:30:44 +00:00
|
|
|
ATTR_BATTERY_LEVEL, DEVICE_DEFAULT_NAME, TEMP_CELSIUS)
|
2016-02-19 05:27:50 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2015-12-27 11:32:08 +00:00
|
|
|
|
|
|
|
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"
|
2016-07-20 02:16:31 +00:00
|
|
|
SENSOR_TYPE_LUMINANCE = "lum"
|
2015-12-27 11:32:08 +00:00
|
|
|
|
|
|
|
SENSOR_TYPES = {
|
2016-04-20 03:30:44 +00:00
|
|
|
SENSOR_TYPE_TEMP: ['Temperature', TEMP_CELSIUS, "mdi:thermometer"],
|
2015-12-27 11:32:08 +00:00
|
|
|
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', ""],
|
2016-07-20 02:16:31 +00:00
|
|
|
SENSOR_TYPE_LUMINANCE: ['Luminance', 'lx', ""],
|
2015-12-27 11:32:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Setup 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):
|
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
|
|
|
def __init__(self, sensor_id):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Initialize the sensor."""
|
2016-02-03 21:31:28 +00:00
|
|
|
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):
|
2016-02-23 05:21:49 +00:00
|
|
|
"""Update sensor values."""
|
2016-02-03 21:31:28 +00:00
|
|
|
tellduslive.NETWORK.update_sensors()
|
|
|
|
self._sensor = tellduslive.NETWORK.get_sensor(self._id)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def _sensor_name(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the name of the sensor."""
|
2016-02-03 21:31:28 +00:00
|
|
|
return self._sensor["name"]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def _sensor_value(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the value the sensor."""
|
2016-02-03 21:31:28 +00:00
|
|
|
return self._sensor["data"]["value"]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def _sensor_type(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the type of the sensor."""
|
2016-02-03 21:31:28 +00:00
|
|
|
return self._sensor["data"]["name"]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def _battery_level(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the battery level of a sensor."""
|
2016-02-03 21:31:28 +00:00
|
|
|
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):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the last update."""
|
2016-02-03 21:31:28 +00:00
|
|
|
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):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the value as temperature."""
|
2016-02-03 21:31:28 +00:00
|
|
|
return round(float(self._sensor_value), 1)
|
|
|
|
|
2016-07-20 02:16:31 +00:00
|
|
|
@property
|
|
|
|
def _value_as_luminance(self):
|
|
|
|
"""Return the value as luminance."""
|
|
|
|
return round(float(self._sensor_value), 1)
|
|
|
|
|
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-02-03 21:31:28 +00:00
|
|
|
return int(round(float(self._sensor_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."""
|
2016-02-03 21:31:28 +00:00
|
|
|
return "{} {}".format(self._sensor_name or DEVICE_DEFAULT_NAME,
|
|
|
|
self.quantity_name)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return true if the sensor is available."""
|
2016-02-03 21:31:28 +00:00
|
|
|
return not self._sensor.get("offline", False)
|
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."""
|
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
|
2016-07-20 02:16:31 +00:00
|
|
|
elif self._sensor_type == SENSOR_TYPE_LUMINANCE:
|
|
|
|
return self._value_as_luminance
|
2015-12-27 11:32:08 +00:00
|
|
|
|
|
|
|
@property
|
2016-02-07 06:28:29 +00:00
|
|
|
def device_state_attributes(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the state attributes."""
|
2016-02-07 06:28:29 +00:00
|
|
|
attrs = {}
|
2015-12-27 11:32:08 +00:00
|
|
|
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):
|
2016-02-23 05:21:49 +00:00
|
|
|
"""Name of quantity."""
|
2016-02-03 21:31:28 +00:00
|
|
|
return SENSOR_TYPES[self._sensor_type][0]
|
|
|
|
|
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."""
|
2015-12-27 11:32:08 +00:00
|
|
|
return SENSOR_TYPES[self._sensor_type][1]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the icon."""
|
2015-12-27 11:32:08 +00:00
|
|
|
return SENSOR_TYPES[self._sensor_type][2]
|