Moved card visibility logic out of the Entity class and into a VisibilityABC. Then made the Group class inherit the VisibilityABC. No duplication of code now. This is definitely better.

This commit is contained in:
Ryan Kraus 2015-04-22 21:21:50 -04:00
parent 8fcf814eb6
commit ff3dacedc0
3 changed files with 38 additions and 48 deletions

View file

@ -20,7 +20,7 @@ import homeassistant
import homeassistant.loader as loader
import homeassistant.components as core_components
import homeassistant.components.group as group
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.entity import VisibilityABC
from homeassistant.const import (
EVENT_COMPONENT_LOADED, CONF_LATITUDE, CONF_LONGITUDE,
CONF_TEMPERATURE_UNIT, CONF_NAME, CONF_TIME_ZONE, TEMP_CELCIUS,
@ -208,7 +208,7 @@ def process_ha_core_config(hass, config):
if key in config:
setattr(hass.config, attr, config[key])
Entity.visibility.update(config.get('visibility', {}))
VisibilityABC.visibility.update(config.get('visibility', {}))
if CONF_TEMPERATURE_UNIT in config:
unit = config[CONF_TEMPERATURE_UNIT]

View file

@ -7,7 +7,7 @@ Provides functionality to group devices that can be turned on or off.
import homeassistant as ha
from homeassistant.helpers import generate_entity_id
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.entity import VisibilityABC
import homeassistant.util as util
from homeassistant.const import (
ATTR_ENTITY_ID, ATTR_FRIENDLY_NAME, STATE_ON, STATE_OFF,
@ -111,12 +111,8 @@ def setup(hass, config):
return True
class Group(object):
class Group(VisibilityABC):
""" Tracks a group of entity ids. """
# pylint: disable=too-many-instance-attributes
visibility = Entity.visibility
_hidden = False
def __init__(self, hass, name, entity_ids=None, user_defined=True):
self.hass = hass
@ -220,24 +216,6 @@ class Group(object):
self.hass.states.set(
self.entity_id, group_off, self.state_attr)
@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)
def setup_group(hass, name, entity_ids, user_defined=True):
""" Sets up a group state that is the combined state of

View file

@ -12,7 +12,40 @@ from homeassistant.const import (
STATE_OFF, DEVICE_DEFAULT_NAME, TEMP_CELCIUS, TEMP_FAHRENHEIT)
class Entity(object):
class VisibilityABC(object):
"""
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.
"""
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):
""" ABC for Home Assistant entities. """
# pylint: disable=no-self-use
@ -20,8 +53,6 @@ class Entity(object):
# The properties and methods here are safe to overwrite when inherting this
# class. These may be used to customize the behavior of the entity.
_hidden = False # suggestion as to whether the entity should be hidden
@property
def should_poll(self):
"""
@ -83,7 +114,6 @@ class Entity(object):
hass = None
entity_id = None
visibility = {}
def update_ha_state(self, force_refresh=False):
"""
@ -130,24 +160,6 @@ class Entity(object):
def __repr__(self):
return "<Entity {}: {}>".format(self.name, self.state)
@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 ToggleEntity(Entity):
""" ABC for entities that can be turned on and off. """