mirror of
https://github.com/home-assistant/core.git
synced 2025-07-20 03:37:07 +00:00
Add icon support to entity
This commit is contained in:
parent
77f4fc8c22
commit
4d069323f4
@ -12,16 +12,17 @@ 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),
|
||||
DemoSwitch('AC', False)
|
||||
DemoSwitch('Decorative Lights', True, None),
|
||||
DemoSwitch('AC', False, 'mdi:air-conditioner')
|
||||
])
|
||||
|
||||
|
||||
class DemoSwitch(SwitchDevice):
|
||||
""" Provides a demo switch. """
|
||||
def __init__(self, name, state):
|
||||
def __init__(self, name, state, icon):
|
||||
self._name = name or DEVICE_DEFAULT_NAME
|
||||
self._state = state
|
||||
self._icon = icon
|
||||
|
||||
@property
|
||||
def should_poll(self):
|
||||
@ -33,6 +34,11 @@ class DemoSwitch(SwitchDevice):
|
||||
""" Returns the name of the device if any. """
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def icon(self):
|
||||
""" Returns the icon to use for device if any. """
|
||||
return self._icon
|
||||
|
||||
@property
|
||||
def current_power_mwh(self):
|
||||
""" Current power usage in mwh. """
|
||||
|
@ -9,7 +9,7 @@ https://home-assistant.io/components/zone.html
|
||||
import logging
|
||||
|
||||
from homeassistant.const import (
|
||||
ATTR_HIDDEN, ATTR_LATITUDE, ATTR_LONGITUDE, CONF_NAME)
|
||||
ATTR_HIDDEN, ATTR_ICON, ATTR_LATITUDE, ATTR_LONGITUDE, CONF_NAME)
|
||||
from homeassistant.helpers import extract_domain_configs, generate_entity_id
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.util.location import distance
|
||||
@ -25,8 +25,7 @@ DEFAULT_NAME = 'Unnamed zone'
|
||||
ATTR_RADIUS = 'radius'
|
||||
DEFAULT_RADIUS = 100
|
||||
|
||||
ATTR_ICON = 'icon'
|
||||
ICON_HOME = 'home'
|
||||
ICON_HOME = 'mdi:home'
|
||||
|
||||
|
||||
def active_zone(hass, latitude, longitude, radius=0):
|
||||
@ -110,7 +109,7 @@ class Zone(Entity):
|
||||
self.latitude = latitude
|
||||
self.longitude = longitude
|
||||
self.radius = radius
|
||||
self.icon = icon
|
||||
self._icon = icon
|
||||
|
||||
def should_poll(self):
|
||||
return False
|
||||
@ -124,14 +123,15 @@ class Zone(Entity):
|
||||
""" The state property really does nothing for a zone. """
|
||||
return STATE
|
||||
|
||||
@property
|
||||
def icon(self):
|
||||
return self._icon
|
||||
|
||||
@property
|
||||
def state_attributes(self):
|
||||
attr = {
|
||||
return {
|
||||
ATTR_HIDDEN: True,
|
||||
ATTR_LATITUDE: self.latitude,
|
||||
ATTR_LONGITUDE: self.longitude,
|
||||
ATTR_RADIUS: self.radius,
|
||||
}
|
||||
if self.icon:
|
||||
attr[ATTR_ICON] = self.icon
|
||||
return attr
|
||||
|
@ -74,6 +74,9 @@ ATTR_FRIENDLY_NAME = "friendly_name"
|
||||
# A picture to represent entity
|
||||
ATTR_ENTITY_PICTURE = "entity_picture"
|
||||
|
||||
# Icon to use in the frontend
|
||||
ATTR_ICON = "icon"
|
||||
|
||||
# The unit of measurement if applicable
|
||||
ATTR_UNIT_OF_MEASUREMENT = "unit_of_measurement"
|
||||
|
||||
|
@ -10,7 +10,7 @@ from collections import defaultdict
|
||||
from homeassistant.exceptions import NoEntitySpecifiedError
|
||||
|
||||
from homeassistant.const import (
|
||||
ATTR_FRIENDLY_NAME, ATTR_HIDDEN, ATTR_UNIT_OF_MEASUREMENT,
|
||||
ATTR_FRIENDLY_NAME, ATTR_HIDDEN, ATTR_UNIT_OF_MEASUREMENT, ATTR_ICON,
|
||||
DEVICE_DEFAULT_NAME, STATE_ON, STATE_OFF, STATE_UNKNOWN, TEMP_CELCIUS,
|
||||
TEMP_FAHRENHEIT)
|
||||
|
||||
@ -61,6 +61,11 @@ class Entity(object):
|
||||
""" Unit of measurement of this entity, if any. """
|
||||
return None
|
||||
|
||||
@property
|
||||
def icon(self):
|
||||
""" Icon to use in the frontend, if any. """
|
||||
return None
|
||||
|
||||
@property
|
||||
def hidden(self):
|
||||
""" Suggestion if the entity should be hidden from UIs. """
|
||||
@ -102,6 +107,9 @@ class Entity(object):
|
||||
if ATTR_UNIT_OF_MEASUREMENT not in attr and self.unit_of_measurement:
|
||||
attr[ATTR_UNIT_OF_MEASUREMENT] = self.unit_of_measurement
|
||||
|
||||
if ATTR_ICON not in attr and self.icon:
|
||||
attr[ATTR_ICON] = self.icon
|
||||
|
||||
if self.hidden:
|
||||
attr[ATTR_HIDDEN] = self.hidden
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user