Auto set friendly name in state attributes

This commit is contained in:
Paulus Schoutsen 2015-02-28 20:10:39 -08:00
parent 80ffe74af6
commit b38146bdef
5 changed files with 17 additions and 30 deletions

View file

@ -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):

View file

@ -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']

View file

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

View file

@ -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):

View file

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