2015-03-04 06:17:47 +00:00
|
|
|
"""
|
|
|
|
homeassistant.components.sensor.tellstick
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Shows sensor values from tellstick sensors.
|
|
|
|
|
|
|
|
Possible config keys:
|
|
|
|
|
|
|
|
id of the sensor: Name the sensor with ID
|
|
|
|
135=Outside
|
|
|
|
|
|
|
|
only_named: Only show the named sensors
|
|
|
|
only_named=1
|
|
|
|
|
|
|
|
temperature_scale: The scale of the temperature value
|
|
|
|
temperature_scale=°C
|
|
|
|
|
|
|
|
datatype_mask: mask to determine which sensor values to show based on
|
|
|
|
https://tellcore-py.readthedocs.org
|
|
|
|
/en/v1.0.4/constants.html#module-tellcore.constants
|
|
|
|
|
|
|
|
datatype_mask=1 # only show temperature
|
|
|
|
datatype_mask=12 # only show rain rate and rain total
|
|
|
|
datatype_mask=127 # show all sensor values
|
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
from collections import namedtuple
|
|
|
|
|
|
|
|
import tellcore.telldus as telldus
|
|
|
|
import tellcore.constants as tellcore_constants
|
|
|
|
|
2015-03-19 06:02:58 +00:00
|
|
|
from homeassistant.const import TEMP_CELCIUS
|
2015-03-22 02:16:13 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2015-03-19 19:34:22 +00:00
|
|
|
import homeassistant.util as util
|
2015-03-04 06:17:47 +00:00
|
|
|
|
|
|
|
DatatypeDescription = namedtuple("DatatypeDescription", ['name', 'unit'])
|
|
|
|
|
2015-08-30 01:39:50 +00:00
|
|
|
REQUIREMENTS = ['tellcore-py==1.0.4']
|
2015-08-09 04:22:34 +00:00
|
|
|
|
2015-03-04 06:17:47 +00:00
|
|
|
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
|
|
|
""" Sets up Tellstick sensors. """
|
|
|
|
sensor_value_descriptions = {
|
|
|
|
tellcore_constants.TELLSTICK_TEMPERATURE:
|
|
|
|
DatatypeDescription(
|
|
|
|
'temperature', config.get('temperature_scale', TEMP_CELCIUS)),
|
|
|
|
|
|
|
|
tellcore_constants.TELLSTICK_HUMIDITY:
|
|
|
|
DatatypeDescription('humidity', '%'),
|
|
|
|
|
|
|
|
tellcore_constants.TELLSTICK_RAINRATE:
|
|
|
|
DatatypeDescription('rain rate', ''),
|
|
|
|
|
|
|
|
tellcore_constants.TELLSTICK_RAINTOTAL:
|
|
|
|
DatatypeDescription('rain total', ''),
|
|
|
|
|
|
|
|
tellcore_constants.TELLSTICK_WINDDIRECTION:
|
|
|
|
DatatypeDescription('wind direction', ''),
|
|
|
|
|
|
|
|
tellcore_constants.TELLSTICK_WINDAVERAGE:
|
|
|
|
DatatypeDescription('wind average', ''),
|
|
|
|
|
|
|
|
tellcore_constants.TELLSTICK_WINDGUST:
|
|
|
|
DatatypeDescription('wind gust', '')
|
|
|
|
}
|
|
|
|
|
|
|
|
try:
|
|
|
|
core = telldus.TelldusCore()
|
|
|
|
except OSError:
|
|
|
|
logging.getLogger(__name__).exception(
|
|
|
|
'Could not initialize Tellstick.')
|
|
|
|
return
|
|
|
|
|
|
|
|
sensors = []
|
2015-03-19 19:34:22 +00:00
|
|
|
datatype_mask = util.convert(config.get('datatype_mask'), int, 127)
|
2015-03-04 06:17:47 +00:00
|
|
|
|
|
|
|
for ts_sensor in core.sensors():
|
|
|
|
try:
|
2015-03-22 02:38:43 +00:00
|
|
|
sensor_name = config[ts_sensor.id]
|
2015-03-04 06:17:47 +00:00
|
|
|
except KeyError:
|
2015-07-23 20:24:48 +00:00
|
|
|
if util.convert(config.get('only_named'), bool, False):
|
2015-03-04 06:17:47 +00:00
|
|
|
continue
|
|
|
|
sensor_name = str(ts_sensor.id)
|
|
|
|
|
|
|
|
for datatype in sensor_value_descriptions.keys():
|
2015-03-19 19:34:22 +00:00
|
|
|
if datatype & datatype_mask and ts_sensor.has_value(datatype):
|
2015-03-04 06:17:47 +00:00
|
|
|
|
|
|
|
sensor_info = sensor_value_descriptions[datatype]
|
|
|
|
|
|
|
|
sensors.append(
|
|
|
|
TellstickSensor(
|
|
|
|
sensor_name, ts_sensor, datatype, sensor_info))
|
|
|
|
|
|
|
|
add_devices(sensors)
|
|
|
|
|
|
|
|
|
2015-03-22 02:16:13 +00:00
|
|
|
class TellstickSensor(Entity):
|
2015-03-04 06:17:47 +00:00
|
|
|
""" Represents a Tellstick sensor. """
|
|
|
|
|
|
|
|
def __init__(self, name, sensor, datatype, sensor_info):
|
|
|
|
self.datatype = datatype
|
|
|
|
self.sensor = sensor
|
2015-03-22 05:26:41 +00:00
|
|
|
self._unit_of_measurement = sensor_info.unit or None
|
2015-03-04 06:17:47 +00:00
|
|
|
|
|
|
|
self._name = "{} {}".format(name, sensor_info.name)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
""" Returns the name of the device. """
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
""" Returns the state of the device. """
|
|
|
|
return self.sensor.value(self.datatype).value
|
2015-03-22 05:26:41 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
return self._unit_of_measurement
|