Add assumed_state property to entity

pull/1247/head
Paulus Schoutsen 2016-02-13 23:42:11 -08:00
parent e170484f16
commit 8bea5c06de
3 changed files with 22 additions and 5 deletions

View File

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

View File

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

View File

@ -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, {}))