Refactor tellstick code (#4460)

* Refactor tellstick code for increased readability. Especially highlight if "device" is a telldus core device or a HA entity.

* Refactor Tellstick object model for increased clarity.

* Update comments. Unify better with sensors. Fix typo bug. Add debug logging.

* Refactor tellstick code for increased readability. Especially highlight if "device" is a telldus core device or a HA entity.

* Refactor Tellstick object model for increased clarity.

* Update comments. Unify better with sensors. Fix typo bug. Add debug logging.

* Fix lint issues.
This commit is contained in:
Magnus Ihse Bursie 2016-11-23 06:48:22 +01:00 committed by Paulus Schoutsen
parent 65b85ec6c0
commit 1d8a1df2c4
4 changed files with 204 additions and 175 deletions

View file

@ -6,61 +6,51 @@ https://home-assistant.io/components/switch.tellstick/
"""
import voluptuous as vol
from homeassistant.components import tellstick
from homeassistant.components.tellstick import (ATTR_DISCOVER_DEVICES,
ATTR_DISCOVER_CONFIG)
from homeassistant.components.tellstick import (DEFAULT_SIGNAL_REPETITIONS,
ATTR_DISCOVER_DEVICES,
ATTR_DISCOVER_CONFIG,
DOMAIN, TellstickDevice)
from homeassistant.helpers.entity import ToggleEntity
PLATFORM_SCHEMA = vol.Schema({vol.Required("platform"): tellstick.DOMAIN})
PLATFORM_SCHEMA = vol.Schema({vol.Required("platform"): DOMAIN})
# pylint: disable=unused-argument
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup Tellstick switches."""
if (discovery_info is None or
discovery_info[ATTR_DISCOVER_DEVICES] is None or
tellstick.TELLCORE_REGISTRY is None):
discovery_info[ATTR_DISCOVER_DEVICES] is None):
return
# Allow platform level override, fallback to module config
signal_repetitions = discovery_info.get(
ATTR_DISCOVER_CONFIG, tellstick.DEFAULT_SIGNAL_REPETITIONS)
signal_repetitions = discovery_info.get(ATTR_DISCOVER_CONFIG,
DEFAULT_SIGNAL_REPETITIONS)
add_devices(TellstickSwitchDevice(
tellstick.TELLCORE_REGISTRY.get_device(switch_id), signal_repetitions)
for switch_id in discovery_info[ATTR_DISCOVER_DEVICES])
add_devices(TellstickSwitch(tellcore_id, signal_repetitions)
for tellcore_id in discovery_info[ATTR_DISCOVER_DEVICES])
class TellstickSwitchDevice(tellstick.TellstickDevice, ToggleEntity):
class TellstickSwitch(TellstickDevice, ToggleEntity):
"""Representation of a Tellstick switch."""
@property
def is_on(self):
"""Return true if switch is on."""
return self._state
def _parse_ha_data(self, kwargs):
"""Turn the value from HA into something useful."""
return None
def set_tellstick_state(self, last_command_sent, last_data_sent):
"""Update the internal representation of the switch."""
from tellcore.constants import TELLSTICK_TURNON
self._state = last_command_sent == TELLSTICK_TURNON
def _parse_tellcore_data(self, tellcore_data):
"""Turn the value recieved from tellcore into something useful."""
return None
def _send_tellstick_command(self, command, data):
"""Handle the turn_on / turn_off commands."""
from tellcore.constants import TELLSTICK_TURNON, TELLSTICK_TURNOFF
if command == TELLSTICK_TURNON:
self.tellstick_device.turn_on()
elif command == TELLSTICK_TURNOFF:
self.tellstick_device.turn_off()
def _update_model(self, new_state, data):
"""Update the device entity state to match the arguments."""
self._state = new_state
def turn_on(self, **kwargs):
"""Turn the switch on."""
from tellcore.constants import TELLSTICK_TURNON
self.call_tellstick(TELLSTICK_TURNON)
def turn_off(self, **kwargs):
"""Turn the switch off."""
from tellcore.constants import TELLSTICK_TURNOFF
self.call_tellstick(TELLSTICK_TURNOFF)
def _send_tellstick_command(self):
"""Let tellcore update the device to match the current state."""
if self._state:
self._tellcore_device.turn_on()
else:
self._tellcore_device.turn_off()
@property
def force_update(self) -> bool: