mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 21:06:19 +00:00
Refactor tellstick_sensor to a sensor platform
This commit is contained in:
126
homeassistant/components/sensor/tellstick.py
Normal file
126
homeassistant/components/sensor/tellstick.py
Normal file
@@ -0,0 +1,126 @@
|
||||
"""
|
||||
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
|
||||
|
||||
from homeassistant.const import (
|
||||
ATTR_FRIENDLY_NAME, ATTR_UNIT_OF_MEASUREMENT, TEMP_CELCIUS)
|
||||
from homeassistant.helpers import Device
|
||||
|
||||
DatatypeDescription = namedtuple("DatatypeDescription", ['name', 'unit'])
|
||||
|
||||
|
||||
# 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 = []
|
||||
|
||||
for ts_sensor in core.sensors():
|
||||
try:
|
||||
sensor_name = config[str(ts_sensor.id)]
|
||||
except KeyError:
|
||||
if 'only_named' in config:
|
||||
continue
|
||||
sensor_name = str(ts_sensor.id)
|
||||
|
||||
for datatype in sensor_value_descriptions.keys():
|
||||
if datatype & int(config['datatype_mask']) and \
|
||||
ts_sensor.has_value(datatype):
|
||||
|
||||
sensor_info = sensor_value_descriptions[datatype]
|
||||
|
||||
sensors.append(
|
||||
TellstickSensor(
|
||||
sensor_name, ts_sensor, datatype, sensor_info))
|
||||
|
||||
add_devices(sensors)
|
||||
|
||||
|
||||
class TellstickSensor(Device):
|
||||
""" Represents a Tellstick sensor. """
|
||||
|
||||
def __init__(self, name, sensor, datatype, sensor_info):
|
||||
self.datatype = datatype
|
||||
self.sensor = sensor
|
||||
self.unit = sensor_info.unit or None
|
||||
|
||||
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
|
||||
|
||||
@property
|
||||
def state_attributes(self):
|
||||
""" Returns the state attributes. """
|
||||
attrs = {
|
||||
ATTR_FRIENDLY_NAME: self._name,
|
||||
}
|
||||
|
||||
if self.unit:
|
||||
attrs[ATTR_UNIT_OF_MEASUREMENT] = self.unit
|
||||
|
||||
return attrs
|
||||
Reference in New Issue
Block a user