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

@ -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. """