Merge pull request #101 from automicus/master
State decorating, ISY component update
This commit is contained in:
commit
c2db17df9a
5 changed files with 45 additions and 20 deletions
|
@ -23,7 +23,7 @@ import homeassistant.components.group as group
|
|||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.const import (
|
||||
EVENT_COMPONENT_LOADED, CONF_LATITUDE, CONF_LONGITUDE,
|
||||
CONF_TEMPERATURE_UNIT, CONF_NAME, CONF_TIME_ZONE, CONF_VISIBILITY,
|
||||
CONF_TEMPERATURE_UNIT, CONF_NAME, CONF_TIME_ZONE, CONF_CUSTOMIZE,
|
||||
TEMP_CELCIUS, TEMP_FAHRENHEIT)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
@ -208,8 +208,8 @@ def process_ha_core_config(hass, config):
|
|||
if key in config:
|
||||
setattr(hass.config, attr, config[key])
|
||||
|
||||
for entity_id, hidden in config.get(CONF_VISIBILITY, {}).items():
|
||||
Entity.overwrite_hidden(entity_id, hidden == 'hide')
|
||||
for entity_id, attrs in config.get(CONF_CUSTOMIZE, {}).items():
|
||||
Entity.overwrite_attribute(entity_id, attrs.keys(), attrs.values())
|
||||
|
||||
if CONF_TEMPERATURE_UNIT in config:
|
||||
unit = config[CONF_TEMPERATURE_UNIT]
|
||||
|
|
|
@ -13,7 +13,8 @@ from homeassistant.helpers import validate_config
|
|||
from homeassistant.helpers.entity import ToggleEntity
|
||||
from homeassistant.const import (
|
||||
CONF_HOST, CONF_USERNAME, CONF_PASSWORD, EVENT_PLATFORM_DISCOVERED,
|
||||
ATTR_SERVICE, ATTR_DISCOVERED, ATTR_FRIENDLY_NAME)
|
||||
EVENT_HOMEASSISTANT_STOP, ATTR_SERVICE, ATTR_DISCOVERED,
|
||||
ATTR_FRIENDLY_NAME)
|
||||
|
||||
# homeassistant constants
|
||||
DOMAIN = "isy994"
|
||||
|
@ -77,6 +78,9 @@ def setup(hass, config):
|
|||
if not ISY.connected:
|
||||
return False
|
||||
|
||||
# listen for HA stop to disconnect
|
||||
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop)
|
||||
|
||||
# Load components for the devices in the ISY controller that we support
|
||||
for comp_name, discovery in ((('sensor', DISCOVER_SENSORS),
|
||||
('light', DISCOVER_LIGHTS),
|
||||
|
@ -91,6 +95,11 @@ def setup(hass, config):
|
|||
return True
|
||||
|
||||
|
||||
def stop(event):
|
||||
""" Cleanup the ISY subscription. """
|
||||
ISY.auto_update = False
|
||||
|
||||
|
||||
class ISYDeviceABC(ToggleEntity):
|
||||
""" Abstract Class for an ISY device within home assistant. """
|
||||
|
||||
|
|
|
@ -11,7 +11,7 @@ CONF_LONGITUDE = "longitude"
|
|||
CONF_TEMPERATURE_UNIT = "temperature_unit"
|
||||
CONF_NAME = "name"
|
||||
CONF_TIME_ZONE = "time_zone"
|
||||
CONF_VISIBILITY = "visibility"
|
||||
CONF_CUSTOMIZE = "customize"
|
||||
|
||||
CONF_PLATFORM = "platform"
|
||||
CONF_HOST = "host"
|
||||
|
|
|
@ -5,14 +5,17 @@ homeassistant.helpers.entity
|
|||
Provides ABC for entities in HA.
|
||||
"""
|
||||
|
||||
from collections import defaultdict
|
||||
|
||||
from homeassistant import NoEntitySpecifiedError
|
||||
|
||||
from homeassistant.const import (
|
||||
ATTR_FRIENDLY_NAME, ATTR_UNIT_OF_MEASUREMENT, ATTR_HIDDEN, STATE_ON,
|
||||
STATE_OFF, DEVICE_DEFAULT_NAME, TEMP_CELCIUS, TEMP_FAHRENHEIT)
|
||||
ATTR_FRIENDLY_NAME, ATTR_UNIT_OF_MEASUREMENT, ATTR_HIDDEN,
|
||||
STATE_ON, STATE_OFF, DEVICE_DEFAULT_NAME, TEMP_CELCIUS,
|
||||
TEMP_FAHRENHEIT)
|
||||
|
||||
# Dict mapping entity_id to a boolean that overwrites the hidden property
|
||||
_OVERWRITE_HIDDEN = {}
|
||||
_OVERWRITE = defaultdict(dict)
|
||||
|
||||
|
||||
class Entity(object):
|
||||
|
@ -121,8 +124,15 @@ class Entity(object):
|
|||
if ATTR_UNIT_OF_MEASUREMENT not in attr and self.unit_of_measurement:
|
||||
attr[ATTR_UNIT_OF_MEASUREMENT] = self.unit_of_measurement
|
||||
|
||||
if _OVERWRITE_HIDDEN.get(self.entity_id, self.hidden):
|
||||
attr[ATTR_HIDDEN] = True
|
||||
if self.hidden:
|
||||
attr[ATTR_HIDDEN] = self.hidden
|
||||
|
||||
# overwrite properties that have been set in the config file
|
||||
attr.update(_OVERWRITE.get(self.entity_id, {}))
|
||||
|
||||
# remove hidden property if false so it won't show up
|
||||
if not attr.get(ATTR_HIDDEN, True):
|
||||
attr.pop(ATTR_HIDDEN)
|
||||
|
||||
# Convert temperature if we detect one
|
||||
if attr.get(ATTR_UNIT_OF_MEASUREMENT) in (TEMP_CELCIUS,
|
||||
|
@ -143,15 +153,18 @@ class Entity(object):
|
|||
return "<Entity {}: {}>".format(self.name, self.state)
|
||||
|
||||
@staticmethod
|
||||
def overwrite_hidden(entity_id, hidden):
|
||||
def overwrite_attribute(entity_id, attrs, vals):
|
||||
"""
|
||||
Overwrite the hidden property of an entity.
|
||||
Set hidden to None to remove any overwritten value in place.
|
||||
Overwrite any attribute of an entity.
|
||||
This function should receive a list of attributes and a
|
||||
list of values. Set attribute to None to remove any overwritten
|
||||
value in place.
|
||||
"""
|
||||
if hidden is None:
|
||||
_OVERWRITE_HIDDEN.pop(entity_id, None)
|
||||
else:
|
||||
_OVERWRITE_HIDDEN[entity_id.lower()] = hidden
|
||||
for attr, val in zip(attrs, vals):
|
||||
if val is None:
|
||||
_OVERWRITE[entity_id.lower()].pop(attr, None)
|
||||
else:
|
||||
_OVERWRITE[entity_id.lower()][attr] = val
|
||||
|
||||
|
||||
class ToggleEntity(Entity):
|
||||
|
|
|
@ -25,7 +25,8 @@ class TestHelpersEntity(unittest.TestCase):
|
|||
def tearDown(self): # pylint: disable=invalid-name
|
||||
""" Stop down stuff we started. """
|
||||
self.hass.stop()
|
||||
entity.Entity.overwrite_hidden(self.entity.entity_id, None)
|
||||
entity.Entity.overwrite_attribute(self.entity.entity_id,
|
||||
[ATTR_HIDDEN], [None])
|
||||
|
||||
def test_default_hidden_not_in_attributes(self):
|
||||
""" Test that the default hidden property is set to False. """
|
||||
|
@ -43,7 +44,8 @@ class TestHelpersEntity(unittest.TestCase):
|
|||
|
||||
def test_overwriting_hidden_property_to_true(self):
|
||||
""" Test we can overwrite hidden property to True. """
|
||||
entity.Entity.overwrite_hidden(self.entity.entity_id, True)
|
||||
entity.Entity.overwrite_attribute(self.entity.entity_id,
|
||||
[ATTR_HIDDEN], [True])
|
||||
self.entity.update_ha_state()
|
||||
|
||||
state = self.hass.states.get(self.entity.entity_id)
|
||||
|
@ -51,7 +53,8 @@ class TestHelpersEntity(unittest.TestCase):
|
|||
|
||||
def test_overwriting_hidden_property_to_false(self):
|
||||
""" Test we can overwrite hidden property to True. """
|
||||
entity.Entity.overwrite_hidden(self.entity.entity_id, False)
|
||||
entity.Entity.overwrite_attribute(self.entity.entity_id,
|
||||
[ATTR_HIDDEN], [False])
|
||||
self.entity.hidden = True
|
||||
self.entity.update_ha_state()
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue