Tweak visibility config
This commit is contained in:
parent
bbdb0320f1
commit
b855f422ef
4 changed files with 103 additions and 40 deletions
|
@ -20,11 +20,11 @@ import homeassistant
|
||||||
import homeassistant.loader as loader
|
import homeassistant.loader as loader
|
||||||
import homeassistant.components as core_components
|
import homeassistant.components as core_components
|
||||||
import homeassistant.components.group as group
|
import homeassistant.components.group as group
|
||||||
from homeassistant.helpers.entity import VisibilityABC
|
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, TEMP_CELCIUS,
|
CONF_TEMPERATURE_UNIT, CONF_NAME, CONF_TIME_ZONE, CONF_VISIBILITY,
|
||||||
TEMP_FAHRENHEIT)
|
TEMP_CELCIUS, TEMP_FAHRENHEIT)
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -208,7 +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])
|
||||||
|
|
||||||
VisibilityABC.visibility.update(config.get('visibility', {}))
|
for entity_id, hidden in config.get(CONF_VISIBILITY, {}).items():
|
||||||
|
Entity.overwrite_hidden(entity_id, hidden == 'hide')
|
||||||
|
|
||||||
if CONF_TEMPERATURE_UNIT in config:
|
if CONF_TEMPERATURE_UNIT in config:
|
||||||
unit = config[CONF_TEMPERATURE_UNIT]
|
unit = config[CONF_TEMPERATURE_UNIT]
|
||||||
|
|
|
@ -11,6 +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_PLATFORM = "platform"
|
CONF_PLATFORM = "platform"
|
||||||
CONF_HOST = "host"
|
CONF_HOST = "host"
|
||||||
|
|
|
@ -11,45 +11,16 @@ from homeassistant.const import (
|
||||||
ATTR_FRIENDLY_NAME, ATTR_UNIT_OF_MEASUREMENT, ATTR_HIDDEN, STATE_ON,
|
ATTR_FRIENDLY_NAME, ATTR_UNIT_OF_MEASUREMENT, ATTR_HIDDEN, STATE_ON,
|
||||||
STATE_OFF, DEVICE_DEFAULT_NAME, TEMP_CELCIUS, TEMP_FAHRENHEIT)
|
STATE_OFF, DEVICE_DEFAULT_NAME, TEMP_CELCIUS, TEMP_FAHRENHEIT)
|
||||||
|
|
||||||
|
# Dict mapping entity_id to a boolean that overwrites the hidden property
|
||||||
class VisibilityABC(object):
|
_OVERWRITE_HIDDEN = {}
|
||||||
"""
|
|
||||||
Abstract Class for including visibility logic. This class includes the
|
|
||||||
necessary methods and properties to consider a visibility suggestion form
|
|
||||||
the component and then determine visibility based on the options in the
|
|
||||||
configuration file. When using this abstract class, the value for the
|
|
||||||
hidden property must still be included in the attributes disctionary. The
|
|
||||||
Entity class takes care of this automatically.
|
|
||||||
"""
|
|
||||||
# pylint: disable=too-few-public-methods
|
|
||||||
|
|
||||||
entity_id = None
|
|
||||||
visibility = {}
|
|
||||||
_hidden = False
|
|
||||||
|
|
||||||
@property
|
|
||||||
def hidden(self):
|
|
||||||
"""
|
|
||||||
Returns the official decision of whether the entity should be hidden.
|
|
||||||
Any value set by the user in the configuration file will overwrite
|
|
||||||
whatever the component sets for visibility.
|
|
||||||
"""
|
|
||||||
if self.entity_id is not None and \
|
|
||||||
self.entity_id.lower() in self.visibility:
|
|
||||||
return self.visibility[self.entity_id.lower()] == 'hide'
|
|
||||||
else:
|
|
||||||
return self._hidden
|
|
||||||
|
|
||||||
@hidden.setter
|
|
||||||
def hidden(self, val):
|
|
||||||
""" Sets the suggestion for visibility. """
|
|
||||||
self._hidden = bool(val)
|
|
||||||
|
|
||||||
|
|
||||||
class Entity(VisibilityABC):
|
class Entity(object):
|
||||||
""" ABC for Home Assistant entities. """
|
""" ABC for Home Assistant entities. """
|
||||||
# pylint: disable=no-self-use
|
# pylint: disable=no-self-use
|
||||||
|
|
||||||
|
_hidden = False
|
||||||
|
|
||||||
# SAFE TO OVERWRITE
|
# SAFE TO OVERWRITE
|
||||||
# The properties and methods here are safe to overwrite when inherting this
|
# The properties and methods here are safe to overwrite when inherting this
|
||||||
# class. These may be used to customize the behavior of the entity.
|
# class. These may be used to customize the behavior of the entity.
|
||||||
|
@ -87,6 +58,16 @@ class Entity(VisibilityABC):
|
||||||
""" Unit of measurement of this entity, if any. """
|
""" Unit of measurement of this entity, if any. """
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
@property
|
||||||
|
def hidden(self):
|
||||||
|
""" Suggestion if the entity should be hidden from UIs. """
|
||||||
|
return self._hidden
|
||||||
|
|
||||||
|
@hidden.setter
|
||||||
|
def hidden(self, val):
|
||||||
|
""" Sets the suggestion for visibility. """
|
||||||
|
self._hidden = bool(val)
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
""" Retrieve latest state. """
|
""" Retrieve latest state. """
|
||||||
pass
|
pass
|
||||||
|
@ -140,8 +121,8 @@ class Entity(VisibilityABC):
|
||||||
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 ATTR_HIDDEN not in attr:
|
if _OVERWRITE_HIDDEN.get(self.entity_id, self.hidden):
|
||||||
attr[ATTR_HIDDEN] = bool(self.hidden)
|
attr[ATTR_HIDDEN] = True
|
||||||
|
|
||||||
# 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,
|
||||||
|
@ -161,6 +142,17 @@ class Entity(VisibilityABC):
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "<Entity {}: {}>".format(self.name, self.state)
|
return "<Entity {}: {}>".format(self.name, self.state)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def overwrite_hidden(entity_id, hidden):
|
||||||
|
"""
|
||||||
|
Overwrite the hidden property of an entity.
|
||||||
|
Set hidden 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
|
||||||
|
|
||||||
|
|
||||||
class ToggleEntity(Entity):
|
class ToggleEntity(Entity):
|
||||||
""" ABC for entities that can be turned on and off. """
|
""" ABC for entities that can be turned on and off. """
|
||||||
|
|
69
tests/test_helper_entity.py
Normal file
69
tests/test_helper_entity.py
Normal file
|
@ -0,0 +1,69 @@
|
||||||
|
"""
|
||||||
|
tests.test_helper_entity
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
Tests the entity helper.
|
||||||
|
"""
|
||||||
|
# pylint: disable=protected-access,too-many-public-methods
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
import homeassistant as ha
|
||||||
|
import homeassistant.helpers.entity as entity
|
||||||
|
from homeassistant.const import ATTR_HIDDEN
|
||||||
|
|
||||||
|
|
||||||
|
class TestHelpersEntity(unittest.TestCase):
|
||||||
|
""" Tests homeassistant.helpers.entity module. """
|
||||||
|
|
||||||
|
def setUp(self): # pylint: disable=invalid-name
|
||||||
|
""" Init needed objects. """
|
||||||
|
self.entity = entity.Entity()
|
||||||
|
self.entity.entity_id = 'test.overwrite_hidden_true'
|
||||||
|
self.hass = self.entity.hass = ha.HomeAssistant()
|
||||||
|
|
||||||
|
def tearDown(self): # pylint: disable=invalid-name
|
||||||
|
""" Stop down stuff we started. """
|
||||||
|
self.hass.stop()
|
||||||
|
|
||||||
|
def test_default_hidden_not_in_attributes(self):
|
||||||
|
""" Test that the default hidden property is set to False. """
|
||||||
|
self.entity.update_ha_state()
|
||||||
|
|
||||||
|
self.assertNotIn(
|
||||||
|
ATTR_HIDDEN,
|
||||||
|
self.hass.states.get(self.entity.entity_id).attributes)
|
||||||
|
|
||||||
|
def test_setting_hidden_to_true(self):
|
||||||
|
self.entity.hidden = True
|
||||||
|
self.entity.update_ha_state()
|
||||||
|
|
||||||
|
state = self.hass.states.get(self.entity.entity_id)
|
||||||
|
|
||||||
|
self.assertTrue(state.attributes.get(ATTR_HIDDEN))
|
||||||
|
|
||||||
|
self.entity.hidden = False
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
self.entity.update_ha_state()
|
||||||
|
|
||||||
|
state = self.hass.states.get(self.entity.entity_id)
|
||||||
|
|
||||||
|
self.assertTrue(state.attributes.get(ATTR_HIDDEN))
|
||||||
|
|
||||||
|
entity.Entity.overwrite_hidden(self.entity.entity_id, None)
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
self.entity.hidden = True
|
||||||
|
self.entity.update_ha_state()
|
||||||
|
|
||||||
|
self.assertNotIn(
|
||||||
|
ATTR_HIDDEN,
|
||||||
|
self.hass.states.get(self.entity.entity_id).attributes)
|
||||||
|
|
||||||
|
entity.Entity.overwrite_hidden(self.entity.entity_id, None)
|
Loading…
Add table
Add a link
Reference in a new issue