2015-03-22 01:49:30 +00:00
|
|
|
"""
|
2016-01-31 02:03:51 +00:00
|
|
|
homeassistant.helpers.entity.
|
2015-03-22 01:49:30 +00:00
|
|
|
|
|
|
|
Provides ABC for entities in HA.
|
|
|
|
"""
|
|
|
|
|
2015-04-25 04:39:35 +00:00
|
|
|
from collections import defaultdict
|
2016-01-24 06:37:15 +00:00
|
|
|
import re
|
2015-04-25 04:39:35 +00:00
|
|
|
|
2015-08-30 02:34:35 +00:00
|
|
|
from homeassistant.exceptions import NoEntitySpecifiedError
|
2016-01-24 07:00:46 +00:00
|
|
|
from homeassistant.util import ensure_unique_string, slugify
|
2015-03-22 01:49:30 +00:00
|
|
|
|
|
|
|
from homeassistant.const import (
|
2015-11-03 08:20:48 +00:00
|
|
|
ATTR_FRIENDLY_NAME, ATTR_HIDDEN, ATTR_UNIT_OF_MEASUREMENT, ATTR_ICON,
|
2016-01-31 02:03:51 +00:00
|
|
|
DEVICE_DEFAULT_NAME, STATE_ON, STATE_OFF, STATE_UNKNOWN, STATE_UNAVAILABLE,
|
|
|
|
TEMP_CELCIUS, TEMP_FAHRENHEIT)
|
2015-03-22 01:49:30 +00:00
|
|
|
|
2015-04-23 13:41:41 +00:00
|
|
|
# Dict mapping entity_id to a boolean that overwrites the hidden property
|
2015-04-25 04:39:35 +00:00
|
|
|
_OVERWRITE = defaultdict(dict)
|
2015-03-22 01:49:30 +00:00
|
|
|
|
2016-01-24 06:37:15 +00:00
|
|
|
# Pattern for validating entity IDs (format: <domain>.<entity>)
|
|
|
|
ENTITY_ID_PATTERN = re.compile(r"^(\w+)\.(\w+)$")
|
|
|
|
|
|
|
|
|
2016-01-24 07:00:46 +00:00
|
|
|
def generate_entity_id(entity_id_format, name, current_ids=None, hass=None):
|
2016-01-31 02:03:51 +00:00
|
|
|
"""Generate a unique entity ID based on given entity IDs or used ids."""
|
2016-01-24 07:00:46 +00:00
|
|
|
name = name.lower() or DEVICE_DEFAULT_NAME.lower()
|
|
|
|
if current_ids is None:
|
|
|
|
if hass is None:
|
|
|
|
raise RuntimeError("Missing required parameter currentids or hass")
|
|
|
|
|
|
|
|
current_ids = hass.states.entity_ids()
|
|
|
|
|
|
|
|
return ensure_unique_string(
|
|
|
|
entity_id_format.format(slugify(name.lower())), current_ids)
|
|
|
|
|
|
|
|
|
2016-01-24 06:49:49 +00:00
|
|
|
def split_entity_id(entity_id):
|
2016-01-31 02:03:51 +00:00
|
|
|
"""Split a state entity_id into domain, object_id."""
|
2016-01-24 06:49:49 +00:00
|
|
|
return entity_id.split(".", 1)
|
|
|
|
|
|
|
|
|
2016-01-24 06:37:15 +00:00
|
|
|
def valid_entity_id(entity_id):
|
|
|
|
"""Test if an entity ID is a valid format."""
|
|
|
|
return ENTITY_ID_PATTERN.match(entity_id) is not None
|
|
|
|
|
2015-04-23 01:21:50 +00:00
|
|
|
|
2015-04-23 13:41:41 +00:00
|
|
|
class Entity(object):
|
2016-01-31 02:03:51 +00:00
|
|
|
"""ABC for Home Assistant entities."""
|
|
|
|
|
2015-03-22 01:49:30 +00:00
|
|
|
# pylint: disable=no-self-use
|
|
|
|
|
2015-04-15 02:57:32 +00:00
|
|
|
# SAFE TO OVERWRITE
|
|
|
|
# The properties and methods here are safe to overwrite when inherting this
|
|
|
|
# class. These may be used to customize the behavior of the entity.
|
|
|
|
|
2015-03-22 01:49:30 +00:00
|
|
|
@property
|
|
|
|
def should_poll(self):
|
|
|
|
"""
|
|
|
|
Return True if entity has to be polled for state.
|
2016-01-31 02:03:51 +00:00
|
|
|
|
2015-03-22 01:49:30 +00:00
|
|
|
False if entity pushes its state to HA.
|
|
|
|
"""
|
|
|
|
return True
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
2016-01-31 02:03:51 +00:00
|
|
|
"""Return a unique id."""
|
2015-03-22 01:49:30 +00:00
|
|
|
return "{}.{}".format(self.__class__, id(self))
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-01-31 02:03:51 +00:00
|
|
|
"""Return the name of the entity."""
|
2015-09-10 06:37:15 +00:00
|
|
|
return DEVICE_DEFAULT_NAME
|
2015-03-22 01:49:30 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
2016-01-31 02:03:51 +00:00
|
|
|
"""Return the state of the entity."""
|
2015-09-10 06:37:15 +00:00
|
|
|
return STATE_UNKNOWN
|
2015-03-22 01:49:30 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state_attributes(self):
|
2016-01-31 02:03:51 +00:00
|
|
|
"""Return the state attributes."""
|
2015-09-10 06:37:15 +00:00
|
|
|
return None
|
2015-03-22 01:49:30 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
2016-01-31 02:03:51 +00:00
|
|
|
"""Return the unit of measurement of this entity, if any."""
|
2015-03-22 01:49:30 +00:00
|
|
|
return None
|
|
|
|
|
2015-11-03 08:20:48 +00:00
|
|
|
@property
|
|
|
|
def icon(self):
|
2016-01-31 02:03:51 +00:00
|
|
|
"""Return the icon to use in the frontend, if any."""
|
2015-11-03 08:20:48 +00:00
|
|
|
return None
|
|
|
|
|
2015-04-23 13:41:41 +00:00
|
|
|
@property
|
|
|
|
def hidden(self):
|
2016-01-31 02:03:51 +00:00
|
|
|
"""Return True if the entity should be hidden from UIs."""
|
2015-09-10 06:37:15 +00:00
|
|
|
return False
|
2015-04-23 13:41:41 +00:00
|
|
|
|
2016-01-31 02:03:51 +00:00
|
|
|
@property
|
|
|
|
def available(self):
|
|
|
|
"""Return True if entity is available."""
|
|
|
|
return True
|
|
|
|
|
2015-04-15 02:57:32 +00:00
|
|
|
def update(self):
|
2016-01-31 02:03:51 +00:00
|
|
|
"""Retrieve latest state."""
|
2015-04-15 02:57:32 +00:00
|
|
|
pass
|
|
|
|
|
2016-01-31 02:55:52 +00:00
|
|
|
entity_id = None
|
|
|
|
|
2015-04-15 02:57:32 +00:00
|
|
|
# DO NOT OVERWRITE
|
|
|
|
# These properties and methods are either managed by Home Assistant or they
|
|
|
|
# are used to perform a very specific function. Overwriting these may
|
|
|
|
# produce undesirable effects in the entity's operation.
|
|
|
|
|
|
|
|
hass = None
|
2015-03-22 01:49:30 +00:00
|
|
|
|
|
|
|
def update_ha_state(self, force_refresh=False):
|
|
|
|
"""
|
2016-01-31 02:03:51 +00:00
|
|
|
Update Home Assistant with current state of entity.
|
|
|
|
|
2015-03-22 01:49:30 +00:00
|
|
|
If force_refresh == True will update entity before setting state.
|
|
|
|
"""
|
|
|
|
if self.hass is None:
|
|
|
|
raise RuntimeError("Attribute hass is None for {}".format(self))
|
|
|
|
|
|
|
|
if self.entity_id is None:
|
|
|
|
raise NoEntitySpecifiedError(
|
|
|
|
"No entity id specified for entity {}".format(self.name))
|
|
|
|
|
|
|
|
if force_refresh:
|
|
|
|
self.update()
|
|
|
|
|
|
|
|
state = str(self.state)
|
|
|
|
attr = self.state_attributes or {}
|
|
|
|
|
2016-02-01 01:11:16 +00:00
|
|
|
if ATTR_UNIT_OF_MEASUREMENT not in attr and \
|
|
|
|
self.unit_of_measurement is not None:
|
|
|
|
attr[ATTR_UNIT_OF_MEASUREMENT] = str(self.unit_of_measurement)
|
|
|
|
|
2016-01-31 02:03:51 +00:00
|
|
|
if not self.available:
|
|
|
|
state = STATE_UNAVAILABLE
|
2016-02-01 01:11:16 +00:00
|
|
|
attr = {}
|
2016-01-31 02:03:51 +00:00
|
|
|
|
2016-01-18 01:50:20 +00:00
|
|
|
if ATTR_FRIENDLY_NAME not in attr and self.name is not None:
|
|
|
|
attr[ATTR_FRIENDLY_NAME] = str(self.name)
|
2015-03-22 01:49:30 +00:00
|
|
|
|
2016-01-18 01:50:20 +00:00
|
|
|
if ATTR_ICON not in attr and self.icon is not None:
|
|
|
|
attr[ATTR_ICON] = str(self.icon)
|
2015-11-03 08:20:48 +00:00
|
|
|
|
2015-04-25 18:59:27 +00:00
|
|
|
if self.hidden:
|
2016-01-18 01:50:20 +00:00
|
|
|
attr[ATTR_HIDDEN] = bool(self.hidden)
|
2015-04-25 18:59:27 +00:00
|
|
|
|
|
|
|
# overwrite properties that have been set in the config file
|
2015-04-25 22:29:37 +00:00
|
|
|
attr.update(_OVERWRITE.get(self.entity_id, {}))
|
2015-04-15 02:57:32 +00:00
|
|
|
|
2015-04-25 18:47:15 +00:00
|
|
|
# remove hidden property if false so it won't show up
|
|
|
|
if not attr.get(ATTR_HIDDEN, True):
|
|
|
|
attr.pop(ATTR_HIDDEN)
|
2015-04-25 04:39:35 +00:00
|
|
|
|
2015-03-22 01:49:30 +00:00
|
|
|
# Convert temperature if we detect one
|
|
|
|
if attr.get(ATTR_UNIT_OF_MEASUREMENT) in (TEMP_CELCIUS,
|
|
|
|
TEMP_FAHRENHEIT):
|
|
|
|
|
|
|
|
state, attr[ATTR_UNIT_OF_MEASUREMENT] = \
|
|
|
|
self.hass.config.temperature(
|
|
|
|
state, attr[ATTR_UNIT_OF_MEASUREMENT])
|
|
|
|
state = str(state)
|
|
|
|
|
|
|
|
return self.hass.states.set(self.entity_id, state, attr)
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
return (isinstance(other, Entity) and
|
|
|
|
other.unique_id == self.unique_id)
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "<Entity {}: {}>".format(self.name, self.state)
|
|
|
|
|
2015-04-23 13:41:41 +00:00
|
|
|
@staticmethod
|
2015-04-25 18:47:15 +00:00
|
|
|
def overwrite_attribute(entity_id, attrs, vals):
|
2015-04-23 13:41:41 +00:00
|
|
|
"""
|
2015-04-25 05:29:42 +00:00
|
|
|
Overwrite any attribute of an entity.
|
2016-01-31 02:03:51 +00:00
|
|
|
|
2015-04-25 18:47:15 +00:00
|
|
|
This function should receive a list of attributes and a
|
|
|
|
list of values. Set attribute to None to remove any overwritten
|
|
|
|
value in place.
|
2015-04-23 13:41:41 +00:00
|
|
|
"""
|
2015-04-25 18:47:15 +00:00
|
|
|
for attr, val in zip(attrs, vals):
|
|
|
|
if val is None:
|
|
|
|
_OVERWRITE[entity_id.lower()].pop(attr, None)
|
|
|
|
else:
|
|
|
|
_OVERWRITE[entity_id.lower()][attr] = val
|
2015-04-23 13:41:41 +00:00
|
|
|
|
2015-03-22 01:49:30 +00:00
|
|
|
|
|
|
|
class ToggleEntity(Entity):
|
2016-01-31 02:03:51 +00:00
|
|
|
"""ABC for entities that can be turned on and off."""
|
|
|
|
|
2015-03-22 01:49:30 +00:00
|
|
|
# pylint: disable=no-self-use
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
2016-01-31 02:03:51 +00:00
|
|
|
"""Return the state."""
|
2015-03-22 01:49:30 +00:00
|
|
|
return STATE_ON if self.is_on else STATE_OFF
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
2016-01-31 02:03:51 +00:00
|
|
|
"""True if entity is on."""
|
2015-03-22 01:49:30 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
def turn_on(self, **kwargs):
|
2016-01-31 02:03:51 +00:00
|
|
|
"""Turn the entity on."""
|
2015-03-22 01:49:30 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
2016-01-31 02:03:51 +00:00
|
|
|
"""Turn the entity off."""
|
2015-03-22 01:49:30 +00:00
|
|
|
pass
|
2016-01-16 15:45:05 +00:00
|
|
|
|
|
|
|
def toggle(self, **kwargs):
|
2016-01-31 02:03:51 +00:00
|
|
|
"""Toggle the entity off."""
|
2016-01-17 21:59:22 +00:00
|
|
|
if self.is_on:
|
2016-01-16 15:45:05 +00:00
|
|
|
self.turn_off(**kwargs)
|
|
|
|
else:
|
|
|
|
self.turn_on(**kwargs)
|