mirror of
https://github.com/home-assistant/core.git
synced 2025-07-20 03:37:07 +00:00
Use voluptuous for Tellstick (#3367)
* Migrate to voluptuous * Update tellstick.py
This commit is contained in:
parent
c6fa07d059
commit
d029861c93
@ -7,25 +7,42 @@ https://home-assistant.io/components/sensor.tellstick/
|
|||||||
import logging
|
import logging
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
|
|
||||||
import homeassistant.util as util
|
import voluptuous as vol
|
||||||
|
|
||||||
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
||||||
from homeassistant.const import TEMP_CELSIUS
|
from homeassistant.const import TEMP_CELSIUS
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
|
import homeassistant.helpers.config_validation as cv
|
||||||
DatatypeDescription = namedtuple("DatatypeDescription", ['name', 'unit'])
|
|
||||||
|
|
||||||
REQUIREMENTS = ['tellcore-py==1.1.2']
|
REQUIREMENTS = ['tellcore-py==1.1.2']
|
||||||
|
|
||||||
|
DatatypeDescription = namedtuple('DatatypeDescription', ['name', 'unit'])
|
||||||
|
|
||||||
|
CONF_DATATYPE_MASK = 'datatype_mask'
|
||||||
|
CONF_ONLY_NAMED = 'only_named'
|
||||||
|
CONF_TEMPERATURE_SCALE = 'temperature_scale'
|
||||||
|
|
||||||
|
DEFAULT_DATATYPE_MASK = 127
|
||||||
|
DEFAULT_ONLY_NAMED = False
|
||||||
|
DEFAULT_TEMPERATURE_SCALE = TEMP_CELSIUS
|
||||||
|
|
||||||
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||||
|
vol.Optional(CONF_ONLY_NAMED, default=DEFAULT_ONLY_NAMED): cv.boolean,
|
||||||
|
vol.Optional(CONF_TEMPERATURE_SCALE, default=DEFAULT_TEMPERATURE_SCALE):
|
||||||
|
cv.string,
|
||||||
|
vol.Optional(CONF_DATATYPE_MASK, default=DEFAULT_DATATYPE_MASK): cv.positive_int,
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
"""Setup Tellstick sensors."""
|
"""Setup the Tellstick sensors."""
|
||||||
import tellcore.telldus as telldus
|
import tellcore.telldus as telldus
|
||||||
import tellcore.constants as tellcore_constants
|
import tellcore.constants as tellcore_constants
|
||||||
|
|
||||||
sensor_value_descriptions = {
|
sensor_value_descriptions = {
|
||||||
tellcore_constants.TELLSTICK_TEMPERATURE:
|
tellcore_constants.TELLSTICK_TEMPERATURE:
|
||||||
DatatypeDescription(
|
DatatypeDescription('temperature', config.get(CONF_TEMPERATURE_SCALE)),
|
||||||
'temperature', config.get('temperature_scale', TEMP_CELSIUS)),
|
|
||||||
|
|
||||||
tellcore_constants.TELLSTICK_HUMIDITY:
|
tellcore_constants.TELLSTICK_HUMIDITY:
|
||||||
DatatypeDescription('humidity', '%'),
|
DatatypeDescription('humidity', '%'),
|
||||||
@ -49,28 +66,24 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
|||||||
try:
|
try:
|
||||||
core = telldus.TelldusCore()
|
core = telldus.TelldusCore()
|
||||||
except OSError:
|
except OSError:
|
||||||
logging.getLogger(__name__).exception(
|
logging.getLogger(__name__).exception('Could not initialize Tellstick')
|
||||||
'Could not initialize Tellstick.')
|
|
||||||
return
|
return
|
||||||
|
|
||||||
sensors = []
|
sensors = []
|
||||||
datatype_mask = util.convert(config.get('datatype_mask'), int, 127)
|
datatype_mask = config.get(CONF_DATATYPE_MASK)
|
||||||
|
|
||||||
for ts_sensor in core.sensors():
|
for ts_sensor in core.sensors():
|
||||||
try:
|
try:
|
||||||
sensor_name = config[ts_sensor.id]
|
sensor_name = config[ts_sensor.id]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
if util.convert(config.get('only_named'), bool, False):
|
if config.get(CONF_ONLY_NAMED):
|
||||||
continue
|
continue
|
||||||
sensor_name = str(ts_sensor.id)
|
sensor_name = str(ts_sensor.id)
|
||||||
|
|
||||||
for datatype in sensor_value_descriptions:
|
for datatype in sensor_value_descriptions:
|
||||||
if datatype & datatype_mask and ts_sensor.has_value(datatype):
|
if datatype & datatype_mask and ts_sensor.has_value(datatype):
|
||||||
|
|
||||||
sensor_info = sensor_value_descriptions[datatype]
|
sensor_info = sensor_value_descriptions[datatype]
|
||||||
|
sensors.append(TellstickSensor(
|
||||||
sensors.append(
|
|
||||||
TellstickSensor(
|
|
||||||
sensor_name, ts_sensor, datatype, sensor_info))
|
sensor_name, ts_sensor, datatype, sensor_info))
|
||||||
|
|
||||||
add_devices(sensors)
|
add_devices(sensors)
|
||||||
@ -85,7 +98,7 @@ class TellstickSensor(Entity):
|
|||||||
self.sensor = sensor
|
self.sensor = sensor
|
||||||
self._unit_of_measurement = sensor_info.unit or None
|
self._unit_of_measurement = sensor_info.unit or None
|
||||||
|
|
||||||
self._name = "{} {}".format(name, sensor_info.name)
|
self._name = '{} {}'.format(name, sensor_info.name)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
|
@ -6,37 +6,37 @@ https://home-assistant.io/components/Tellstick/
|
|||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
import threading
|
import threading
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.helpers import discovery
|
from homeassistant.helpers import discovery
|
||||||
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
|
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
|
|
||||||
DOMAIN = "tellstick"
|
DOMAIN = 'tellstick'
|
||||||
|
|
||||||
REQUIREMENTS = ['tellcore-py==1.1.2']
|
REQUIREMENTS = ['tellcore-py==1.1.2']
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
ATTR_SIGNAL_REPETITIONS = "signal_repetitions"
|
ATTR_SIGNAL_REPETITIONS = 'signal_repetitions'
|
||||||
DEFAULT_SIGNAL_REPETITIONS = 1
|
DEFAULT_SIGNAL_REPETITIONS = 1
|
||||||
|
|
||||||
ATTR_DISCOVER_DEVICES = "devices"
|
ATTR_DISCOVER_DEVICES = 'devices'
|
||||||
ATTR_DISCOVER_CONFIG = "config"
|
ATTR_DISCOVER_CONFIG = 'config'
|
||||||
|
|
||||||
# Use a global tellstick domain lock to handle
|
# Use a global tellstick domain lock to handle Tellcore errors then calling
|
||||||
# tellcore errors then calling to concurrently
|
# to concurrently
|
||||||
TELLSTICK_LOCK = threading.Lock()
|
TELLSTICK_LOCK = threading.Lock()
|
||||||
|
|
||||||
# Keep a reference the the callback registry
|
# Keep a reference the the callback registry. Used from entities that register
|
||||||
# Used from entities that register callback listeners
|
# callback listeners
|
||||||
TELLCORE_REGISTRY = None
|
TELLCORE_REGISTRY = None
|
||||||
|
|
||||||
CONFIG_SCHEMA = vol.Schema({
|
CONFIG_SCHEMA = vol.Schema({
|
||||||
DOMAIN: vol.Schema({
|
DOMAIN: vol.Schema({
|
||||||
vol.Optional(ATTR_SIGNAL_REPETITIONS,
|
vol.Optional(ATTR_SIGNAL_REPETITIONS,
|
||||||
default=DEFAULT_SIGNAL_REPETITIONS):
|
default=DEFAULT_SIGNAL_REPETITIONS): vol.Coerce(int),
|
||||||
vol.Coerce(int),
|
|
||||||
}),
|
}),
|
||||||
}, extra=vol.ALLOW_EXTRA)
|
}, extra=vol.ALLOW_EXTRA)
|
||||||
|
|
||||||
@ -46,8 +46,8 @@ def _discover(hass, config, found_devices, component_name):
|
|||||||
if not len(found_devices):
|
if not len(found_devices):
|
||||||
return
|
return
|
||||||
|
|
||||||
_LOGGER.info("discovered %d new %s devices",
|
_LOGGER.info(
|
||||||
len(found_devices), component_name)
|
"Discovered %d new %s devices", len(found_devices), component_name)
|
||||||
|
|
||||||
signal_repetitions = config[DOMAIN].get(ATTR_SIGNAL_REPETITIONS)
|
signal_repetitions = config[DOMAIN].get(ATTR_SIGNAL_REPETITIONS)
|
||||||
|
|
||||||
@ -78,29 +78,28 @@ def setup(hass, config):
|
|||||||
_discover(hass, config, [switch.id for switch in
|
_discover(hass, config, [switch.id for switch in
|
||||||
devices if not switch.methods(
|
devices if not switch.methods(
|
||||||
tellcore_constants.TELLSTICK_DIM)],
|
tellcore_constants.TELLSTICK_DIM)],
|
||||||
"switch")
|
'switch')
|
||||||
|
|
||||||
# Discover the lights
|
# Discover the lights
|
||||||
_discover(hass, config, [light.id for light in
|
_discover(hass, config, [light.id for light in
|
||||||
devices if light.methods(
|
devices if light.methods(
|
||||||
tellcore_constants.TELLSTICK_DIM)],
|
tellcore_constants.TELLSTICK_DIM)],
|
||||||
"light")
|
'light')
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
class TellstickRegistry:
|
class TellstickRegistry(object):
|
||||||
"""Handle everything around tellstick callbacks.
|
"""Handle everything around Tellstick callbacks.
|
||||||
|
|
||||||
Keeps a map device ids to home-assistant entities.
|
Keeps a map device ids to home-assistant entities.
|
||||||
Also responsible for registering / cleanup of callbacks.
|
Also responsible for registering / cleanup of callbacks.
|
||||||
|
|
||||||
All device specific logic should be elsewhere (Entities).
|
All device specific logic should be elsewhere (Entities).
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, hass, tellcore_lib):
|
def __init__(self, hass, tellcore_lib):
|
||||||
"""Init the tellstick mappings and callbacks."""
|
"""Initialize the Tellstick mappings and callbacks."""
|
||||||
self._core_lib = tellcore_lib
|
self._core_lib = tellcore_lib
|
||||||
# used when map callback device id to ha entities.
|
# used when map callback device id to ha entities.
|
||||||
self._id_to_entity_map = {}
|
self._id_to_entity_map = {}
|
||||||
@ -108,7 +107,7 @@ class TellstickRegistry:
|
|||||||
self._setup_device_callback(hass, tellcore_lib)
|
self._setup_device_callback(hass, tellcore_lib)
|
||||||
|
|
||||||
def _device_callback(self, tellstick_id, method, data, cid):
|
def _device_callback(self, tellstick_id, method, data, cid):
|
||||||
"""Handle the actual callback from tellcore."""
|
"""Handle the actual callback from Tellcore."""
|
||||||
entity = self._id_to_entity_map.get(tellstick_id, None)
|
entity = self._id_to_entity_map.get(tellstick_id, None)
|
||||||
if entity is not None:
|
if entity is not None:
|
||||||
entity.set_tellstick_state(method, data)
|
entity.set_tellstick_state(method, data)
|
||||||
@ -116,8 +115,7 @@ class TellstickRegistry:
|
|||||||
|
|
||||||
def _setup_device_callback(self, hass, tellcore_lib):
|
def _setup_device_callback(self, hass, tellcore_lib):
|
||||||
"""Register the callback handler."""
|
"""Register the callback handler."""
|
||||||
callback_id = tellcore_lib.register_device_event(
|
callback_id = tellcore_lib.register_device_event(self._device_callback)
|
||||||
self._device_callback)
|
|
||||||
|
|
||||||
def clean_up_callback(event):
|
def clean_up_callback(event):
|
||||||
"""Unregister the callback bindings."""
|
"""Unregister the callback bindings."""
|
||||||
@ -132,8 +130,8 @@ class TellstickRegistry:
|
|||||||
|
|
||||||
def register_devices(self, devices):
|
def register_devices(self, devices):
|
||||||
"""Register a list of devices."""
|
"""Register a list of devices."""
|
||||||
self._id_to_device_map.update({device.id:
|
self._id_to_device_map.update(
|
||||||
device for device in devices})
|
{device.id: device for device in devices})
|
||||||
|
|
||||||
def get_device(self, tellcore_id):
|
def get_device(self, tellcore_id):
|
||||||
"""Return a device by tellcore_id."""
|
"""Return a device by tellcore_id."""
|
||||||
@ -141,18 +139,17 @@ class TellstickRegistry:
|
|||||||
|
|
||||||
|
|
||||||
class TellstickDevice(Entity):
|
class TellstickDevice(Entity):
|
||||||
"""Represents a Tellstick device.
|
"""Representation of a Tellstick device.
|
||||||
|
|
||||||
Contains the common logic for all Tellstick devices.
|
Contains the common logic for all Tellstick devices.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, tellstick_device, signal_repetitions):
|
def __init__(self, tellstick_device, signal_repetitions):
|
||||||
"""Init the tellstick device."""
|
"""Initalize the Tellstick device."""
|
||||||
self.signal_repetitions = signal_repetitions
|
self.signal_repetitions = signal_repetitions
|
||||||
self._state = None
|
self._state = None
|
||||||
self.tellstick_device = tellstick_device
|
self.tellstick_device = tellstick_device
|
||||||
# add to id to entity mapping
|
# Add to id to entity mapping
|
||||||
TELLCORE_REGISTRY.register_entity(tellstick_device.id, self)
|
TELLCORE_REGISTRY.register_entity(tellstick_device.id, self)
|
||||||
# Query tellcore for the current state
|
# Query tellcore for the current state
|
||||||
self.update()
|
self.update()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user