diff --git a/homeassistant/components/light/demo.py b/homeassistant/components/light/demo.py index edd8d680800..a02677e1876 100644 --- a/homeassistant/components/light/demo.py +++ b/homeassistant/components/light/demo.py @@ -2,8 +2,7 @@ import random from homeassistant.helpers import ToggleDevice -from homeassistant.const import ( - STATE_ON, STATE_OFF, DEVICE_DEFAULT_NAME, ATTR_FRIENDLY_NAME) +from homeassistant.const import STATE_ON, STATE_OFF, DEVICE_DEFAULT_NAME from homeassistant.components.light import ATTR_BRIGHTNESS, ATTR_XY_COLOR @@ -44,15 +43,11 @@ class DemoLight(ToggleDevice): @property def state_attributes(self): """ Returns optional state attributes. """ - attr = { - ATTR_FRIENDLY_NAME: self._name, - } - if self.is_on: - attr[ATTR_BRIGHTNESS] = self._brightness - attr[ATTR_XY_COLOR] = self._xy - - return attr + return { + ATTR_BRIGHTNESS: self._brightness, + ATTR_XY_COLOR: self._xy, + } @property def is_on(self): diff --git a/homeassistant/components/light/hue.py b/homeassistant/components/light/hue.py index e53fd1fa25d..2e68956d359 100644 --- a/homeassistant/components/light/hue.py +++ b/homeassistant/components/light/hue.py @@ -7,7 +7,7 @@ from urllib.parse import urlparse from homeassistant.loader import get_component import homeassistant.util as util from homeassistant.helpers import ToggleDevice -from homeassistant.const import ATTR_FRIENDLY_NAME, CONF_HOST +from homeassistant.const import CONF_HOST from homeassistant.components.light import ( ATTR_BRIGHTNESS, ATTR_XY_COLOR, ATTR_TRANSITION, ATTR_FLASH, FLASH_LONG, FLASH_SHORT) @@ -153,9 +153,7 @@ class HueLight(ToggleDevice): @property def state_attributes(self): """ Returns optional state attributes. """ - attr = { - ATTR_FRIENDLY_NAME: self.name - } + attr = {} if self.is_on: attr[ATTR_BRIGHTNESS] = self.info['state']['bri'] diff --git a/homeassistant/components/switch/demo.py b/homeassistant/components/switch/demo.py index efa049e7194..17233e0c46d 100644 --- a/homeassistant/components/switch/demo.py +++ b/homeassistant/components/switch/demo.py @@ -1,7 +1,6 @@ """ Demo platform that has two fake switchces. """ from homeassistant.helpers import ToggleDevice -from homeassistant.const import ( - STATE_ON, STATE_OFF, DEVICE_DEFAULT_NAME, ATTR_FRIENDLY_NAME) +from homeassistant.const import STATE_ON, STATE_OFF, DEVICE_DEFAULT_NAME def get_devices(hass, config): @@ -38,11 +37,6 @@ class DemoSwitch(ToggleDevice): """ Returns the name of the device if any. """ return self._state - @property - def state_attributes(self): - """ Returns optional state attributes. """ - return {ATTR_FRIENDLY_NAME: self._name} - @property def is_on(self): """ True if device is on. """ diff --git a/homeassistant/components/switch/wemo.py b/homeassistant/components/switch/wemo.py index 8e5808ee36e..ffec5eaf8c7 100644 --- a/homeassistant/components/switch/wemo.py +++ b/homeassistant/components/switch/wemo.py @@ -2,7 +2,6 @@ import logging from homeassistant.helpers import ToggleDevice -from homeassistant.const import ATTR_FRIENDLY_NAME from homeassistant.components.switch import ( ATTR_TODAY_MWH, ATTR_CURRENT_POWER_MWH) @@ -75,12 +74,9 @@ class WemoSwitch(ToggleDevice): cur_info = self.wemo.insight_params return { - ATTR_FRIENDLY_NAME: self.wemo.name, ATTR_CURRENT_POWER_MWH: cur_info['currentpower'], ATTR_TODAY_MWH: cur_info['todaymw'] } - else: - return {ATTR_FRIENDLY_NAME: self.wemo.name} @property def is_on(self): diff --git a/homeassistant/helpers.py b/homeassistant/helpers.py index 11b86dc798f..e076b084e30 100644 --- a/homeassistant/helpers.py +++ b/homeassistant/helpers.py @@ -7,8 +7,8 @@ from homeassistant import NoEntitySpecifiedError from homeassistant.loader import get_component from homeassistant.const import ( - ATTR_ENTITY_ID, STATE_ON, STATE_OFF, CONF_PLATFORM, CONF_TYPE, - DEVICE_DEFAULT_NAME) + ATTR_ENTITY_ID, ATTR_FRIENDLY_NAME, STATE_ON, STATE_OFF, CONF_PLATFORM, + CONF_TYPE, DEVICE_DEFAULT_NAME) from homeassistant.util import ensure_unique_string, slugify @@ -216,7 +216,7 @@ class Device(object): def get_state_attributes(self): """ Returns optional state attributes. """ - return {} + return None def update(self): """ Retrieve latest state from the real device. """ @@ -234,8 +234,12 @@ class Device(object): if force_refresh: self.update() - return hass.states.set(self.entity_id, self.state, - self.state_attributes) + attr = self.state_attributes or {} + + if ATTR_FRIENDLY_NAME not in attr and self.name: + attr[ATTR_FRIENDLY_NAME] = self.name + + return hass.states.set(self.entity_id, self.state, attr) def __eq__(self, other): return (isinstance(other, Device) and