Use properties instead of getters for Device class

This commit is contained in:
Paulus Schoutsen 2015-01-11 09:20:41 -08:00
parent 035e3e686e
commit 283b187501
7 changed files with 123 additions and 80 deletions

View file

@ -77,15 +77,36 @@ class HueLight(ToggleDevice):
self.bridge = bridge
self.update_lights = update_lights
def get_name(self):
""" Get the mame of the Hue light. """
return self.info['name']
@property
def unique_id(self):
""" Returns the id of this Hue light """
return "{}.{}".format(
self.__class__, self.info.get('uniqueid', self.get_name()))
self.__class__, self.info.get('uniqueid', self.name))
@property
def name(self):
""" Get the mame of the Hue light. """
return self.info.get('name', 'No name')
@property
def state_attributes(self):
""" Returns optional state attributes. """
attr = {
ATTR_FRIENDLY_NAME: self.name
}
if self.is_on:
attr[ATTR_BRIGHTNESS] = self.info['state']['bri']
attr[ATTR_XY_COLOR] = self.info['state']['xy']
return attr
@property
def is_on(self):
""" True if device is on. """
self.update_lights()
return self.info['state']['reachable'] and self.info['state']['on']
def turn_on(self, **kwargs):
""" Turn the specified or all lights on. """
@ -124,24 +145,6 @@ class HueLight(ToggleDevice):
self.bridge.set_light(self.light_id, command)
def is_on(self):
""" True if device is on. """
self.update_lights()
return self.info['state']['reachable'] and self.info['state']['on']
def get_state_attributes(self):
""" Returns optional state attributes. """
attr = {
ATTR_FRIENDLY_NAME: self.get_name()
}
if self.is_on():
attr[ATTR_BRIGHTNESS] = self.info['state']['bri']
attr[ATTR_XY_COLOR] = self.info['state']['xy']
return attr
def update(self):
""" Synchronize state with bridge. """
self.update_lights(no_throttle=True)

View file

@ -88,7 +88,7 @@ def setup(hass, config):
if switch is not None and switch not in switches.values():
switch.entity_id = util.ensure_unique_string(
ENTITY_ID_FORMAT.format(util.slugify(switch.get_name())),
ENTITY_ID_FORMAT.format(util.slugify(switch.name)),
switches.keys())
switches[switch.entity_id] = switch

View file

@ -36,10 +36,24 @@ class TellstickSwitch(ToggleDevice):
self.tellstick = tellstick
self.state_attr = {ATTR_FRIENDLY_NAME: tellstick.name}
def get_name(self):
@property
def name(self):
""" Returns the name of the switch if any. """
return self.tellstick.name
@property
def state_attributes(self):
""" Returns optional state attributes. """
return self.state_attr
@property
def is_on(self):
""" True if switch is on. """
last_command = self.tellstick.last_sent_command(
self.last_sent_command_mask)
return last_command == tc_constants.TELLSTICK_TURNON
# pylint: disable=unused-argument
def turn_on(self, **kwargs):
""" Turns the switch on. """
@ -49,14 +63,3 @@ class TellstickSwitch(ToggleDevice):
def turn_off(self, **kwargs):
""" Turns the switch off. """
self.tellstick.turn_off()
def is_on(self):
""" True if switch is on. """
last_command = self.tellstick.last_sent_command(
self.last_sent_command_mask)
return last_command == tc_constants.TELLSTICK_TURNON
def get_state_attributes(self):
""" Returns optional state attributes. """
return self.state_attr

View file

@ -64,23 +64,13 @@ class WemoSwitch(ToggleDevice):
""" Returns the id of this WeMo switch """
return "{}.{}".format(self.__class__, self.wemo.serialnumber)
def get_name(self):
@property
def name(self):
""" Returns the name of the switch if any. """
return self.wemo.name
def turn_on(self, **kwargs):
""" Turns the switch on. """
self.wemo.on()
def turn_off(self):
""" Turns the switch off. """
self.wemo.off()
def is_on(self):
""" True if switch is on. """
return self.wemo.get_state(True)
def get_state_attributes(self):
@property
def state_attributes(self):
""" Returns optional state attributes. """
if self.wemo.model.startswith('Belkin Insight'):
cur_info = self.wemo.insight_params
@ -92,3 +82,16 @@ class WemoSwitch(ToggleDevice):
}
else:
return {ATTR_FRIENDLY_NAME: self.wemo.name}
@property
def is_on(self):
""" True if switch is on. """
return self.wemo.get_state(True)
def turn_on(self, **kwargs):
""" Turns the switch on. """
self.wemo.on()
def turn_off(self):
""" Turns the switch off. """
self.wemo.off()

View file

@ -2,6 +2,9 @@
# Can be used to specify a catch all when registering state or event listeners.
MATCH_ALL = '*'
# If no name is specified
DEVICE_DEFAULT_NAME = "Unnamed Device"
# #### CONFIG ####
CONF_LATITUDE = "latitude"
CONF_LONGITUDE = "longitude"

View file

@ -7,7 +7,8 @@ from homeassistant import NoEntitySpecifiedError
from homeassistant.loader import get_component
from homeassistant.const import (
ATTR_ENTITY_ID, STATE_ON, STATE_OFF, CONF_PLATFORM, CONF_TYPE)
ATTR_ENTITY_ID, STATE_ON, STATE_OFF, CONF_PLATFORM, CONF_TYPE,
DEVICE_DEFAULT_NAME)
from homeassistant.util import ensure_unique_string, slugify
@ -147,16 +148,16 @@ def platform_devices_from_config(config, domain, hass,
devices.extend(p_devices)
# Setup entity IDs for each device
no_name_count = 1
device_dict = {}
for device in devices:
name = device.get_name()
no_name_count = 0
if name is None:
name = "{} #{}".format(domain, no_name_count)
for device in devices:
name = device.name
if name == DEVICE_DEFAULT_NAME:
no_name_count += 1
name = "{} #{}".format(domain, no_name_count)
entity_id = ensure_unique_string(
entity_id_format.format(slugify(name)),
@ -179,9 +180,29 @@ class Device(object):
""" Returns a unique id. """
return "{}.{}".format(self.__class__, id(self))
@property
def name(self):
""" Returns the name of the device. """
return self.get_name()
@property
def state(self):
""" Returns the state of the device. """
return self.get_state()
@property
def state_attributes(self):
""" Returns the state attributes. """
return {}
# DEPRECATION NOTICE:
# Device is moving from getters to properties.
# For now the new properties will call the old functions
# This will be removed in the future.
def get_name(self):
""" Returns the name of the device if any. """
return "No Name"
return DEVICE_DEFAULT_NAME
def get_state(self):
""" Returns state of the device. """
@ -202,13 +223,13 @@ class Device(object):
"""
if self.entity_id is None:
raise NoEntitySpecifiedError(
"No entity specified for device {}".format(self.get_name()))
"No entity specified for device {}".format(self.name))
if force_refresh:
self.update()
return hass.states.set(self.entity_id, self.get_state(),
self.get_state_attributes())
return hass.states.set(self.entity_id, self.state,
self.state_attributes)
def __eq__(self, other):
return (isinstance(other, Device) and
@ -219,9 +240,15 @@ class ToggleDevice(Device):
""" ABC for devices that can be turned on and off. """
# pylint: disable=no-self-use
def get_state(self):
@property
def state(self):
""" Returns the state. """
return STATE_ON if self.is_on() else STATE_OFF
return STATE_ON if self.is_on else STATE_OFF
@property
def is_on(self):
""" True if device is on. """
return False
def turn_on(self, **kwargs):
""" Turn the device on. """
@ -230,7 +257,3 @@ class ToggleDevice(Device):
def turn_off(self, **kwargs):
""" Turn the device off. """
pass
def is_on(self):
""" True if device is on. """
return False

View file

@ -8,7 +8,7 @@ import os
import homeassistant as ha
from homeassistant.helpers import ToggleDevice
from homeassistant.const import STATE_ON, STATE_OFF
from homeassistant.const import STATE_ON, STATE_OFF, DEVICE_DEFAULT_NAME
def get_test_home_assistant():
@ -45,29 +45,37 @@ class MockModule(object):
class MockToggleDevice(ToggleDevice):
""" Provides a mock toggle device. """
def __init__(self, name, state):
self.name = name
self.state = state
self._name = name or DEVICE_DEFAULT_NAME
self._state = state
self.calls = []
def get_name(self):
@property
def name(self):
""" Returns the name of the device if any. """
self.calls.append(('get_name', {}))
return self.name
self.calls.append(('name', {}))
return self._name
@property
def state(self):
""" Returns the name of the device if any. """
self.calls.append(('state', {}))
return self._state
@property
def is_on(self):
""" True if device is on. """
self.calls.append(('is_on', {}))
return self._state == STATE_ON
def turn_on(self, **kwargs):
""" Turn the device on. """
self.calls.append(('turn_on', kwargs))
self.state = STATE_ON
self._state = STATE_ON
def turn_off(self, **kwargs):
""" Turn the device off. """
self.calls.append(('turn_off', kwargs))
self.state = STATE_OFF
def is_on(self):
""" True if device is on. """
self.calls.append(('is_on', {}))
return self.state == STATE_ON
self._state = STATE_OFF
def last_call(self, method=None):
if method is None: