Skip to content

Commit

Permalink
Moved card visibility logic out of the Entity class and into a Visibi…
Browse files Browse the repository at this point in the history
…lityABC. Then made the Group class inherit the VisibilityABC. No duplication of code now. This is definitely better.
  • Loading branch information
rmkraus committed Apr 23, 2015
1 parent 8fcf814 commit ff3dace
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 48 deletions.
4 changes: 2 additions & 2 deletions homeassistant/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
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,
Expand Down Expand Up @@ -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]
Expand Down
26 changes: 2 additions & 24 deletions homeassistant/components/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -220,24 +216,6 @@ def _update_group_state(self, entity_id, old_state, new_state):
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
Expand Down
56 changes: 34 additions & 22 deletions homeassistant/helpers/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,47 @@
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

# SAFE TO OVERWRITE
# 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):
"""
Expand Down Expand Up @@ -83,7 +114,6 @@ def get_state_attributes(self):

hass = None
entity_id = None
visibility = {}

def update_ha_state(self, force_refresh=False):
"""
Expand Down Expand Up @@ -130,24 +160,6 @@ def __eq__(self, other):
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. """
Expand Down

0 comments on commit ff3dace

Please sign in to comment.