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 import random
from homeassistant.helpers import ToggleDevice from homeassistant.helpers import ToggleDevice
from homeassistant.const import ( from homeassistant.const import STATE_ON, STATE_OFF, DEVICE_DEFAULT_NAME
STATE_ON, STATE_OFF, DEVICE_DEFAULT_NAME, ATTR_FRIENDLY_NAME)
from homeassistant.components.light import ATTR_BRIGHTNESS, ATTR_XY_COLOR from homeassistant.components.light import ATTR_BRIGHTNESS, ATTR_XY_COLOR
@ -44,15 +43,11 @@ class DemoLight(ToggleDevice):
@property @property
def state_attributes(self): def state_attributes(self):
""" Returns optional state attributes. """ """ Returns optional state attributes. """
attr = {
ATTR_FRIENDLY_NAME: self._name,
}
if self.is_on: if self.is_on:
attr[ATTR_BRIGHTNESS] = self._brightness return {
attr[ATTR_XY_COLOR] = self._xy ATTR_BRIGHTNESS: self._brightness,
ATTR_XY_COLOR: self._xy,
return attr }
@property @property
def is_on(self): def is_on(self):

View file

@ -7,7 +7,7 @@ from urllib.parse import urlparse
from homeassistant.loader import get_component from homeassistant.loader import get_component
import homeassistant.util as util import homeassistant.util as util
from homeassistant.helpers import ToggleDevice 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 ( from homeassistant.components.light import (
ATTR_BRIGHTNESS, ATTR_XY_COLOR, ATTR_TRANSITION, ATTR_BRIGHTNESS, ATTR_XY_COLOR, ATTR_TRANSITION,
ATTR_FLASH, FLASH_LONG, FLASH_SHORT) ATTR_FLASH, FLASH_LONG, FLASH_SHORT)
@ -153,9 +153,7 @@ class HueLight(ToggleDevice):
@property @property
def state_attributes(self): def state_attributes(self):
""" Returns optional state attributes. """ """ Returns optional state attributes. """
attr = { attr = {}
ATTR_FRIENDLY_NAME: self.name
}
if self.is_on: if self.is_on:
attr[ATTR_BRIGHTNESS] = self.info['state']['bri'] attr[ATTR_BRIGHTNESS] = self.info['state']['bri']

View file

@ -1,7 +1,6 @@
""" Demo platform that has two fake switchces. """ """ Demo platform that has two fake switchces. """
from homeassistant.helpers import ToggleDevice from homeassistant.helpers import ToggleDevice
from homeassistant.const import ( from homeassistant.const import STATE_ON, STATE_OFF, DEVICE_DEFAULT_NAME
STATE_ON, STATE_OFF, DEVICE_DEFAULT_NAME, ATTR_FRIENDLY_NAME)
def get_devices(hass, config): def get_devices(hass, config):
@ -38,11 +37,6 @@ class DemoSwitch(ToggleDevice):
""" Returns the name of the device if any. """ """ Returns the name of the device if any. """
return self._state return self._state
@property
def state_attributes(self):
""" Returns optional state attributes. """
return {ATTR_FRIENDLY_NAME: self._name}
@property @property
def is_on(self): def is_on(self):
""" True if device is on. """ """ True if device is on. """

View file

@ -2,7 +2,6 @@
import logging import logging
from homeassistant.helpers import ToggleDevice from homeassistant.helpers import ToggleDevice
from homeassistant.const import ATTR_FRIENDLY_NAME
from homeassistant.components.switch import ( from homeassistant.components.switch import (
ATTR_TODAY_MWH, ATTR_CURRENT_POWER_MWH) ATTR_TODAY_MWH, ATTR_CURRENT_POWER_MWH)
@ -75,12 +74,9 @@ class WemoSwitch(ToggleDevice):
cur_info = self.wemo.insight_params cur_info = self.wemo.insight_params
return { return {
ATTR_FRIENDLY_NAME: self.wemo.name,
ATTR_CURRENT_POWER_MWH: cur_info['currentpower'], ATTR_CURRENT_POWER_MWH: cur_info['currentpower'],
ATTR_TODAY_MWH: cur_info['todaymw'] ATTR_TODAY_MWH: cur_info['todaymw']
} }
else:
return {ATTR_FRIENDLY_NAME: self.wemo.name}
@property @property
def is_on(self): def is_on(self):

View file

@ -7,8 +7,8 @@ from homeassistant import NoEntitySpecifiedError
from homeassistant.loader import get_component from homeassistant.loader import get_component
from homeassistant.const import ( from homeassistant.const import (
ATTR_ENTITY_ID, STATE_ON, STATE_OFF, CONF_PLATFORM, CONF_TYPE, ATTR_ENTITY_ID, ATTR_FRIENDLY_NAME, STATE_ON, STATE_OFF, CONF_PLATFORM,
DEVICE_DEFAULT_NAME) CONF_TYPE, DEVICE_DEFAULT_NAME)
from homeassistant.util import ensure_unique_string, slugify from homeassistant.util import ensure_unique_string, slugify
@ -216,7 +216,7 @@ class Device(object):
def get_state_attributes(self): def get_state_attributes(self):
""" Returns optional state attributes. """ """ Returns optional state attributes. """
return {} return None
def update(self): def update(self):
""" Retrieve latest state from the real device. """ """ Retrieve latest state from the real device. """
@ -234,8 +234,12 @@ class Device(object):
if force_refresh: if force_refresh:
self.update() self.update()
return hass.states.set(self.entity_id, self.state, attr = self.state_attributes or {}
self.state_attributes)
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): def __eq__(self, other):
return (isinstance(other, Device) and return (isinstance(other, Device) and