Use properties instead of getters for Device class
parent
035e3e686e
commit
283b187501
|
@ -77,15 +77,36 @@ class HueLight(ToggleDevice):
|
||||||
self.bridge = bridge
|
self.bridge = bridge
|
||||||
self.update_lights = update_lights
|
self.update_lights = update_lights
|
||||||
|
|
||||||
def get_name(self):
|
|
||||||
""" Get the mame of the Hue light. """
|
|
||||||
return self.info['name']
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def unique_id(self):
|
def unique_id(self):
|
||||||
""" Returns the id of this Hue light """
|
""" Returns the id of this Hue light """
|
||||||
return "{}.{}".format(
|
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):
|
def turn_on(self, **kwargs):
|
||||||
""" Turn the specified or all lights on. """
|
""" Turn the specified or all lights on. """
|
||||||
|
@ -124,24 +145,6 @@ class HueLight(ToggleDevice):
|
||||||
|
|
||||||
self.bridge.set_light(self.light_id, command)
|
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):
|
def update(self):
|
||||||
""" Synchronize state with bridge. """
|
""" Synchronize state with bridge. """
|
||||||
self.update_lights(no_throttle=True)
|
self.update_lights(no_throttle=True)
|
||||||
|
|
|
@ -88,7 +88,7 @@ def setup(hass, config):
|
||||||
|
|
||||||
if switch is not None and switch not in switches.values():
|
if switch is not None and switch not in switches.values():
|
||||||
switch.entity_id = util.ensure_unique_string(
|
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.keys())
|
||||||
|
|
||||||
switches[switch.entity_id] = switch
|
switches[switch.entity_id] = switch
|
||||||
|
|
|
@ -36,10 +36,24 @@ class TellstickSwitch(ToggleDevice):
|
||||||
self.tellstick = tellstick
|
self.tellstick = tellstick
|
||||||
self.state_attr = {ATTR_FRIENDLY_NAME: tellstick.name}
|
self.state_attr = {ATTR_FRIENDLY_NAME: tellstick.name}
|
||||||
|
|
||||||
def get_name(self):
|
@property
|
||||||
|
def name(self):
|
||||||
""" Returns the name of the switch if any. """
|
""" Returns the name of the switch if any. """
|
||||||
return self.tellstick.name
|
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
|
# pylint: disable=unused-argument
|
||||||
def turn_on(self, **kwargs):
|
def turn_on(self, **kwargs):
|
||||||
""" Turns the switch on. """
|
""" Turns the switch on. """
|
||||||
|
@ -49,14 +63,3 @@ class TellstickSwitch(ToggleDevice):
|
||||||
def turn_off(self, **kwargs):
|
def turn_off(self, **kwargs):
|
||||||
""" Turns the switch off. """
|
""" Turns the switch off. """
|
||||||
self.tellstick.turn_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
|
|
||||||
|
|
|
@ -64,23 +64,13 @@ class WemoSwitch(ToggleDevice):
|
||||||
""" Returns the id of this WeMo switch """
|
""" Returns the id of this WeMo switch """
|
||||||
return "{}.{}".format(self.__class__, self.wemo.serialnumber)
|
return "{}.{}".format(self.__class__, self.wemo.serialnumber)
|
||||||
|
|
||||||
def get_name(self):
|
@property
|
||||||
|
def name(self):
|
||||||
""" Returns the name of the switch if any. """
|
""" Returns the name of the switch if any. """
|
||||||
return self.wemo.name
|
return self.wemo.name
|
||||||
|
|
||||||
def turn_on(self, **kwargs):
|
@property
|
||||||
""" Turns the switch on. """
|
def state_attributes(self):
|
||||||
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):
|
|
||||||
""" Returns optional state attributes. """
|
""" Returns optional state attributes. """
|
||||||
if self.wemo.model.startswith('Belkin Insight'):
|
if self.wemo.model.startswith('Belkin Insight'):
|
||||||
cur_info = self.wemo.insight_params
|
cur_info = self.wemo.insight_params
|
||||||
|
@ -92,3 +82,16 @@ class WemoSwitch(ToggleDevice):
|
||||||
}
|
}
|
||||||
else:
|
else:
|
||||||
return {ATTR_FRIENDLY_NAME: self.wemo.name}
|
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()
|
||||||
|
|
|
@ -2,6 +2,9 @@
|
||||||
# Can be used to specify a catch all when registering state or event listeners.
|
# Can be used to specify a catch all when registering state or event listeners.
|
||||||
MATCH_ALL = '*'
|
MATCH_ALL = '*'
|
||||||
|
|
||||||
|
# If no name is specified
|
||||||
|
DEVICE_DEFAULT_NAME = "Unnamed Device"
|
||||||
|
|
||||||
# #### CONFIG ####
|
# #### CONFIG ####
|
||||||
CONF_LATITUDE = "latitude"
|
CONF_LATITUDE = "latitude"
|
||||||
CONF_LONGITUDE = "longitude"
|
CONF_LONGITUDE = "longitude"
|
||||||
|
|
|
@ -7,7 +7,8 @@ from homeassistant import NoEntitySpecifiedError
|
||||||
|
|
||||||
from homeassistant.loader import get_component
|
from homeassistant.loader import get_component
|
||||||
from homeassistant.const import (
|
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
|
from homeassistant.util import ensure_unique_string, slugify
|
||||||
|
|
||||||
|
|
||||||
|
@ -147,16 +148,16 @@ def platform_devices_from_config(config, domain, hass,
|
||||||
devices.extend(p_devices)
|
devices.extend(p_devices)
|
||||||
|
|
||||||
# Setup entity IDs for each device
|
# Setup entity IDs for each device
|
||||||
no_name_count = 1
|
|
||||||
|
|
||||||
device_dict = {}
|
device_dict = {}
|
||||||
|
|
||||||
for device in devices:
|
no_name_count = 0
|
||||||
name = device.get_name()
|
|
||||||
|
|
||||||
if name is None:
|
for device in devices:
|
||||||
name = "{} #{}".format(domain, no_name_count)
|
name = device.name
|
||||||
|
|
||||||
|
if name == DEVICE_DEFAULT_NAME:
|
||||||
no_name_count += 1
|
no_name_count += 1
|
||||||
|
name = "{} #{}".format(domain, no_name_count)
|
||||||
|
|
||||||
entity_id = ensure_unique_string(
|
entity_id = ensure_unique_string(
|
||||||
entity_id_format.format(slugify(name)),
|
entity_id_format.format(slugify(name)),
|
||||||
|
@ -179,9 +180,29 @@ class Device(object):
|
||||||
""" Returns a unique id. """
|
""" Returns a unique id. """
|
||||||
return "{}.{}".format(self.__class__, id(self))
|
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):
|
def get_name(self):
|
||||||
""" Returns the name of the device if any. """
|
""" Returns the name of the device if any. """
|
||||||
return "No Name"
|
return DEVICE_DEFAULT_NAME
|
||||||
|
|
||||||
def get_state(self):
|
def get_state(self):
|
||||||
""" Returns state of the device. """
|
""" Returns state of the device. """
|
||||||
|
@ -202,13 +223,13 @@ class Device(object):
|
||||||
"""
|
"""
|
||||||
if self.entity_id is None:
|
if self.entity_id is None:
|
||||||
raise NoEntitySpecifiedError(
|
raise NoEntitySpecifiedError(
|
||||||
"No entity specified for device {}".format(self.get_name()))
|
"No entity specified for device {}".format(self.name))
|
||||||
|
|
||||||
if force_refresh:
|
if force_refresh:
|
||||||
self.update()
|
self.update()
|
||||||
|
|
||||||
return hass.states.set(self.entity_id, self.get_state(),
|
return hass.states.set(self.entity_id, self.state,
|
||||||
self.get_state_attributes())
|
self.state_attributes)
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
return (isinstance(other, Device) and
|
return (isinstance(other, Device) and
|
||||||
|
@ -219,9 +240,15 @@ class ToggleDevice(Device):
|
||||||
""" ABC for devices that can be turned on and off. """
|
""" ABC for devices that can be turned on and off. """
|
||||||
# pylint: disable=no-self-use
|
# pylint: disable=no-self-use
|
||||||
|
|
||||||
def get_state(self):
|
@property
|
||||||
|
def state(self):
|
||||||
""" Returns the state. """
|
""" 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):
|
def turn_on(self, **kwargs):
|
||||||
""" Turn the device on. """
|
""" Turn the device on. """
|
||||||
|
@ -230,7 +257,3 @@ class ToggleDevice(Device):
|
||||||
def turn_off(self, **kwargs):
|
def turn_off(self, **kwargs):
|
||||||
""" Turn the device off. """
|
""" Turn the device off. """
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def is_on(self):
|
|
||||||
""" True if device is on. """
|
|
||||||
return False
|
|
||||||
|
|
|
@ -8,7 +8,7 @@ import os
|
||||||
|
|
||||||
import homeassistant as ha
|
import homeassistant as ha
|
||||||
from homeassistant.helpers import ToggleDevice
|
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():
|
def get_test_home_assistant():
|
||||||
|
@ -45,29 +45,37 @@ class MockModule(object):
|
||||||
class MockToggleDevice(ToggleDevice):
|
class MockToggleDevice(ToggleDevice):
|
||||||
""" Provides a mock toggle device. """
|
""" Provides a mock toggle device. """
|
||||||
def __init__(self, name, state):
|
def __init__(self, name, state):
|
||||||
self.name = name
|
self._name = name or DEVICE_DEFAULT_NAME
|
||||||
self.state = state
|
self._state = state
|
||||||
self.calls = []
|
self.calls = []
|
||||||
|
|
||||||
def get_name(self):
|
@property
|
||||||
|
def name(self):
|
||||||
""" Returns the name of the device if any. """
|
""" Returns the name of the device if any. """
|
||||||
self.calls.append(('get_name', {}))
|
self.calls.append(('name', {}))
|
||||||
return self.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):
|
def turn_on(self, **kwargs):
|
||||||
""" Turn the device on. """
|
""" Turn the device on. """
|
||||||
self.calls.append(('turn_on', kwargs))
|
self.calls.append(('turn_on', kwargs))
|
||||||
self.state = STATE_ON
|
self._state = STATE_ON
|
||||||
|
|
||||||
def turn_off(self, **kwargs):
|
def turn_off(self, **kwargs):
|
||||||
""" Turn the device off. """
|
""" Turn the device off. """
|
||||||
self.calls.append(('turn_off', kwargs))
|
self.calls.append(('turn_off', kwargs))
|
||||||
self.state = STATE_OFF
|
self._state = STATE_OFF
|
||||||
|
|
||||||
def is_on(self):
|
|
||||||
""" True if device is on. """
|
|
||||||
self.calls.append(('is_on', {}))
|
|
||||||
return self.state == STATE_ON
|
|
||||||
|
|
||||||
def last_call(self, method=None):
|
def last_call(self, method=None):
|
||||||
if method is None:
|
if method is None:
|
||||||
|
|
Loading…
Reference in New Issue