Rearranged visibility control and image control in the configuration file. Now a single generic clause can be used to customize any attribute.

This commit is contained in:
Ryan Kraus 2015-04-25 14:47:15 -04:00
parent 04a98f99b7
commit 8255164eda
4 changed files with 24 additions and 26 deletions

View File

@ -23,9 +23,8 @@ import homeassistant.components.group as group
from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity import Entity
from homeassistant.const import ( from homeassistant.const import (
EVENT_COMPONENT_LOADED, CONF_LATITUDE, CONF_LONGITUDE, 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,
CONF_DECORATE, TEMP_CELCIUS, TEMP_FAHRENHEIT, ATTR_ENTITY_PICTURE, TEMP_CELCIUS, TEMP_FAHRENHEIT)
ATTR_HIDDEN)
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -209,11 +208,8 @@ def process_ha_core_config(hass, config):
if key in config: if key in config:
setattr(hass.config, attr, config[key]) setattr(hass.config, attr, config[key])
for entity_id, hidden in config.get(CONF_VISIBILITY, {}).items(): for entity_id, attrs in config.get(CONF_CUSTOMIZE, {}).items():
Entity.overwrite_attribute(entity_id, ATTR_HIDDEN, hidden == 'hide') Entity.overwrite_attribute(entity_id, attrs.keys(), attrs.values())
for entity_id, image in config.get(CONF_DECORATE, {}).items():
Entity.overwrite_attribute(entity_id, ATTR_ENTITY_PICTURE, image)
if CONF_TEMPERATURE_UNIT in config: if CONF_TEMPERATURE_UNIT in config:
unit = config[CONF_TEMPERATURE_UNIT] unit = config[CONF_TEMPERATURE_UNIT]

View File

@ -11,8 +11,7 @@ CONF_LONGITUDE = "longitude"
CONF_TEMPERATURE_UNIT = "temperature_unit" CONF_TEMPERATURE_UNIT = "temperature_unit"
CONF_NAME = "name" CONF_NAME = "name"
CONF_TIME_ZONE = "time_zone" CONF_TIME_ZONE = "time_zone"
CONF_VISIBILITY = "visibility" CONF_CUSTOMIZE = "customize"
CONF_DECORATE = "decorate"
CONF_PLATFORM = "platform" CONF_PLATFORM = "platform"
CONF_HOST = "host" CONF_HOST = "host"

View File

@ -11,8 +11,8 @@ from homeassistant import NoEntitySpecifiedError
from homeassistant.const import ( from homeassistant.const import (
ATTR_FRIENDLY_NAME, ATTR_UNIT_OF_MEASUREMENT, ATTR_HIDDEN, ATTR_FRIENDLY_NAME, ATTR_UNIT_OF_MEASUREMENT, ATTR_HIDDEN,
ATTR_ENTITY_PICTURE, STATE_ON, STATE_OFF, DEVICE_DEFAULT_NAME, STATE_ON, STATE_OFF, DEVICE_DEFAULT_NAME, TEMP_CELCIUS,
TEMP_CELCIUS, TEMP_FAHRENHEIT) TEMP_FAHRENHEIT)
# Dict mapping entity_id to a boolean that overwrites the hidden property # Dict mapping entity_id to a boolean that overwrites the hidden property
_OVERWRITE = defaultdict(dict) _OVERWRITE = defaultdict(dict)
@ -124,12 +124,12 @@ class Entity(object):
if ATTR_UNIT_OF_MEASUREMENT not in attr and self.unit_of_measurement: if ATTR_UNIT_OF_MEASUREMENT not in attr and self.unit_of_measurement:
attr[ATTR_UNIT_OF_MEASUREMENT] = self.unit_of_measurement attr[ATTR_UNIT_OF_MEASUREMENT] = self.unit_of_measurement
if _OVERWRITE[ATTR_HIDDEN].get(self.entity_id, self.hidden): for attr_name, val in _OVERWRITE[self.entity_id].items():
attr[ATTR_HIDDEN] = True attr[attr_name] = val
if _OVERWRITE[ATTR_ENTITY_PICTURE].get(self.entity_id, False): # remove hidden property if false so it won't show up
attr[ATTR_ENTITY_PICTURE] = \ if not attr.get(ATTR_HIDDEN, True):
_OVERWRITE[ATTR_ENTITY_PICTURE][self.entity_id] attr.pop(ATTR_HIDDEN)
# Convert temperature if we detect one # Convert temperature if we detect one
if attr.get(ATTR_UNIT_OF_MEASUREMENT) in (TEMP_CELCIUS, if attr.get(ATTR_UNIT_OF_MEASUREMENT) in (TEMP_CELCIUS,
@ -150,15 +150,18 @@ class Entity(object):
return "<Entity {}: {}>".format(self.name, self.state) return "<Entity {}: {}>".format(self.name, self.state)
@staticmethod @staticmethod
def overwrite_attribute(entity_id, attr, val): def overwrite_attribute(entity_id, attrs, vals):
""" """
Overwrite any attribute of an entity. Overwrite any attribute of an entity.
Set attribute to None to remove any overwritten value in place. This function should receive a list of attributes and a
list of values. Set attribute to None to remove any overwritten
value in place.
""" """
for attr, val in zip(attrs, vals):
if val is None: if val is None:
_OVERWRITE[attr].pop(entity_id, None) _OVERWRITE[entity_id.lower()].pop(attr, None)
else: else:
_OVERWRITE[attr][entity_id.lower()] = val _OVERWRITE[entity_id.lower()][attr] = val
class ToggleEntity(Entity): class ToggleEntity(Entity):

View File

@ -26,7 +26,7 @@ class TestHelpersEntity(unittest.TestCase):
""" Stop down stuff we started. """ """ Stop down stuff we started. """
self.hass.stop() self.hass.stop()
entity.Entity.overwrite_attribute(self.entity.entity_id, entity.Entity.overwrite_attribute(self.entity.entity_id,
ATTR_HIDDEN, None) [ATTR_HIDDEN], [None])
def test_default_hidden_not_in_attributes(self): def test_default_hidden_not_in_attributes(self):
""" Test that the default hidden property is set to False. """ """ Test that the default hidden property is set to False. """
@ -45,7 +45,7 @@ class TestHelpersEntity(unittest.TestCase):
def test_overwriting_hidden_property_to_true(self): def test_overwriting_hidden_property_to_true(self):
""" Test we can overwrite hidden property to True. """ """ Test we can overwrite hidden property to True. """
entity.Entity.overwrite_attribute(self.entity.entity_id, entity.Entity.overwrite_attribute(self.entity.entity_id,
ATTR_HIDDEN, True) [ATTR_HIDDEN], [True])
self.entity.update_ha_state() self.entity.update_ha_state()
state = self.hass.states.get(self.entity.entity_id) state = self.hass.states.get(self.entity.entity_id)
@ -54,7 +54,7 @@ class TestHelpersEntity(unittest.TestCase):
def test_overwriting_hidden_property_to_false(self): def test_overwriting_hidden_property_to_false(self):
""" Test we can overwrite hidden property to True. """ """ Test we can overwrite hidden property to True. """
entity.Entity.overwrite_attribute(self.entity.entity_id, entity.Entity.overwrite_attribute(self.entity.entity_id,
ATTR_HIDDEN, False) [ATTR_HIDDEN], [False])
self.entity.hidden = True self.entity.hidden = True
self.entity.update_ha_state() self.entity.update_ha_state()