From 8bea5c06de0b9259594b1fc03abd8438ce7aac23 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sat, 13 Feb 2016 23:42:11 -0800 Subject: [PATCH] Add assumed_state property to entity --- homeassistant/components/switch/demo.py | 12 +++++++++--- homeassistant/const.py | 3 +++ homeassistant/helpers/entity.py | 12 ++++++++++-- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/switch/demo.py b/homeassistant/components/switch/demo.py index 16676cb239e..0f6529dff52 100644 --- a/homeassistant/components/switch/demo.py +++ b/homeassistant/components/switch/demo.py @@ -12,17 +12,18 @@ from homeassistant.const import DEVICE_DEFAULT_NAME def setup_platform(hass, config, add_devices_callback, discovery_info=None): """ Find and return demo switches. """ add_devices_callback([ - DemoSwitch('Decorative Lights', True, None), - DemoSwitch('AC', False, 'mdi:air-conditioner') + DemoSwitch('Decorative Lights', True, None, True), + DemoSwitch('AC', False, 'mdi:air-conditioner', False) ]) class DemoSwitch(SwitchDevice): """ Provides a demo switch. """ - def __init__(self, name, state, icon): + def __init__(self, name, state, icon, assumed): self._name = name or DEVICE_DEFAULT_NAME self._state = state self._icon = icon + self._assumed = assumed @property def should_poll(self): @@ -39,6 +40,11 @@ class DemoSwitch(SwitchDevice): """ Returns the icon to use for device if any. """ return self._icon + @property + def assumed_state(self): + """Return if the state is based on assumptions.""" + return self._assumed + @property def current_power_mwh(self): """ Current power usage in mwh. """ diff --git a/homeassistant/const.py b/homeassistant/const.py index 084f269e036..45fe2e0bd91 100644 --- a/homeassistant/const.py +++ b/homeassistant/const.py @@ -124,6 +124,9 @@ ATTR_LONGITUDE = "longitude" # Accuracy of location in meters ATTR_GPS_ACCURACY = 'gps_accuracy' +# If state is assumed +ATTR_ASSUMED_STATE = 'assumed_state' + # #### SERVICES #### SERVICE_HOMEASSISTANT_STOP = "stop" SERVICE_HOMEASSISTANT_RESTART = "restart" diff --git a/homeassistant/helpers/entity.py b/homeassistant/helpers/entity.py index 742b877946a..624029dc7fd 100644 --- a/homeassistant/helpers/entity.py +++ b/homeassistant/helpers/entity.py @@ -13,7 +13,7 @@ from homeassistant.util import ensure_unique_string, slugify from homeassistant.const import ( ATTR_FRIENDLY_NAME, ATTR_HIDDEN, ATTR_UNIT_OF_MEASUREMENT, ATTR_ICON, DEVICE_DEFAULT_NAME, STATE_ON, STATE_OFF, STATE_UNKNOWN, STATE_UNAVAILABLE, - TEMP_CELCIUS, TEMP_FAHRENHEIT) + TEMP_CELCIUS, TEMP_FAHRENHEIT, ATTR_ASSUMED_STATE) # Dict mapping entity_id to a boolean that overwrites the hidden property _OVERWRITE = defaultdict(dict) @@ -116,6 +116,11 @@ class Entity(object): """Return True if entity is available.""" return True + @property + def assumed_state(self): + """Return True if unable to access real state of entity.""" + return False + def update(self): """Retrieve latest state.""" pass @@ -164,12 +169,15 @@ class Entity(object): if ATTR_FRIENDLY_NAME not in attr and self.name is not None: attr[ATTR_FRIENDLY_NAME] = str(self.name) - if ATTR_ICON not in attr and self.icon is not None: + if self.icon is not None: attr[ATTR_ICON] = str(self.icon) if self.hidden: attr[ATTR_HIDDEN] = bool(self.hidden) + if self.assumed_state: + attr[ATTR_ASSUMED_STATE] = bool(self.assumed_state) + # overwrite properties that have been set in the config file attr.update(_OVERWRITE.get(self.entity_id, {}))