2019-02-13 20:21:14 +00:00
|
|
|
"""Component to count within automations."""
|
2017-08-29 13:44:36 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2019-07-23 17:08:32 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_ICON, CONF_NAME, CONF_MAXIMUM, CONF_MINIMUM)
|
2019-04-18 10:02:01 +00:00
|
|
|
|
2018-06-01 17:41:20 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2019-07-23 17:08:32 +00:00
|
|
|
from homeassistant.helpers.config_validation import ENTITY_SERVICE_SCHEMA
|
2017-08-29 13:44:36 +00:00
|
|
|
from homeassistant.helpers.entity_component import EntityComponent
|
2018-11-28 12:16:43 +00:00
|
|
|
from homeassistant.helpers.restore_state import RestoreEntity
|
2017-08-29 13:44:36 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
ATTR_INITIAL = 'initial'
|
|
|
|
ATTR_STEP = 'step'
|
2019-04-18 10:02:01 +00:00
|
|
|
ATTR_MINIMUM = 'minimum'
|
|
|
|
ATTR_MAXIMUM = 'maximum'
|
2017-08-29 13:44:36 +00:00
|
|
|
|
|
|
|
CONF_INITIAL = 'initial'
|
2018-10-03 21:12:21 +00:00
|
|
|
CONF_RESTORE = 'restore'
|
2017-08-29 13:44:36 +00:00
|
|
|
CONF_STEP = 'step'
|
|
|
|
|
|
|
|
DEFAULT_INITIAL = 0
|
|
|
|
DEFAULT_STEP = 1
|
|
|
|
DOMAIN = 'counter'
|
|
|
|
|
|
|
|
ENTITY_ID_FORMAT = DOMAIN + '.{}'
|
|
|
|
|
|
|
|
SERVICE_DECREMENT = 'decrement'
|
|
|
|
SERVICE_INCREMENT = 'increment'
|
|
|
|
SERVICE_RESET = 'reset'
|
2019-04-18 10:02:01 +00:00
|
|
|
SERVICE_CONFIGURE = 'configure'
|
2017-08-29 13:44:36 +00:00
|
|
|
|
2019-07-23 17:08:32 +00:00
|
|
|
SERVICE_SCHEMA_CONFIGURE = ENTITY_SERVICE_SCHEMA.extend({
|
2019-04-18 10:02:01 +00:00
|
|
|
vol.Optional(ATTR_MINIMUM): vol.Any(None, vol.Coerce(int)),
|
|
|
|
vol.Optional(ATTR_MAXIMUM): vol.Any(None, vol.Coerce(int)),
|
|
|
|
vol.Optional(ATTR_STEP): cv.positive_int,
|
|
|
|
})
|
|
|
|
|
2017-08-29 13:44:36 +00:00
|
|
|
CONFIG_SCHEMA = vol.Schema({
|
2019-01-22 00:36:04 +00:00
|
|
|
DOMAIN: cv.schema_with_slug_keys(
|
|
|
|
vol.Any({
|
2017-08-29 13:44:36 +00:00
|
|
|
vol.Optional(CONF_ICON): cv.icon,
|
|
|
|
vol.Optional(CONF_INITIAL, default=DEFAULT_INITIAL):
|
|
|
|
cv.positive_int,
|
|
|
|
vol.Optional(CONF_NAME): cv.string,
|
2019-04-18 10:02:01 +00:00
|
|
|
vol.Optional(CONF_MAXIMUM, default=None):
|
|
|
|
vol.Any(None, vol.Coerce(int)),
|
|
|
|
vol.Optional(CONF_MINIMUM, default=None):
|
|
|
|
vol.Any(None, vol.Coerce(int)),
|
2018-10-03 21:12:21 +00:00
|
|
|
vol.Optional(CONF_RESTORE, default=True): cv.boolean,
|
2017-08-29 13:44:36 +00:00
|
|
|
vol.Optional(CONF_STEP, default=DEFAULT_STEP): cv.positive_int,
|
|
|
|
}, None)
|
2019-01-22 00:36:04 +00:00
|
|
|
)
|
2017-08-29 13:44:36 +00:00
|
|
|
}, extra=vol.ALLOW_EXTRA)
|
|
|
|
|
|
|
|
|
2018-06-01 17:41:20 +00:00
|
|
|
async def async_setup(hass, config):
|
|
|
|
"""Set up the counters."""
|
2017-08-29 13:44:36 +00:00
|
|
|
component = EntityComponent(_LOGGER, DOMAIN, hass)
|
|
|
|
|
|
|
|
entities = []
|
|
|
|
|
|
|
|
for object_id, cfg in config[DOMAIN].items():
|
|
|
|
if not cfg:
|
|
|
|
cfg = {}
|
|
|
|
|
|
|
|
name = cfg.get(CONF_NAME)
|
|
|
|
initial = cfg.get(CONF_INITIAL)
|
2018-10-03 21:12:21 +00:00
|
|
|
restore = cfg.get(CONF_RESTORE)
|
2017-08-29 13:44:36 +00:00
|
|
|
step = cfg.get(CONF_STEP)
|
|
|
|
icon = cfg.get(CONF_ICON)
|
2019-04-18 10:02:01 +00:00
|
|
|
minimum = cfg.get(CONF_MINIMUM)
|
|
|
|
maximum = cfg.get(CONF_MAXIMUM)
|
2017-08-29 13:44:36 +00:00
|
|
|
|
2019-04-18 10:02:01 +00:00
|
|
|
entities.append(Counter(object_id, name, initial, minimum, maximum,
|
|
|
|
restore, step, icon))
|
2017-08-29 13:44:36 +00:00
|
|
|
|
|
|
|
if not entities:
|
|
|
|
return False
|
|
|
|
|
2018-08-16 07:50:11 +00:00
|
|
|
component.async_register_entity_service(
|
2019-07-23 17:08:32 +00:00
|
|
|
SERVICE_INCREMENT, ENTITY_SERVICE_SCHEMA,
|
2018-08-16 07:50:11 +00:00
|
|
|
'async_increment')
|
|
|
|
component.async_register_entity_service(
|
2019-07-23 17:08:32 +00:00
|
|
|
SERVICE_DECREMENT, ENTITY_SERVICE_SCHEMA,
|
2018-08-16 07:50:11 +00:00
|
|
|
'async_decrement')
|
|
|
|
component.async_register_entity_service(
|
2019-07-23 17:08:32 +00:00
|
|
|
SERVICE_RESET, ENTITY_SERVICE_SCHEMA,
|
2018-08-16 07:50:11 +00:00
|
|
|
'async_reset')
|
2019-04-18 10:02:01 +00:00
|
|
|
component.async_register_entity_service(
|
|
|
|
SERVICE_CONFIGURE, SERVICE_SCHEMA_CONFIGURE,
|
|
|
|
'async_configure')
|
2017-08-29 13:44:36 +00:00
|
|
|
|
2018-06-01 17:41:20 +00:00
|
|
|
await component.async_add_entities(entities)
|
2017-08-29 13:44:36 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
2018-11-28 12:16:43 +00:00
|
|
|
class Counter(RestoreEntity):
|
2017-08-29 13:44:36 +00:00
|
|
|
"""Representation of a counter."""
|
|
|
|
|
2019-04-18 10:02:01 +00:00
|
|
|
def __init__(self, object_id, name, initial, minimum, maximum,
|
|
|
|
restore, step, icon):
|
2017-08-29 13:44:36 +00:00
|
|
|
"""Initialize a counter."""
|
|
|
|
self.entity_id = ENTITY_ID_FORMAT.format(object_id)
|
|
|
|
self._name = name
|
2018-10-03 21:12:21 +00:00
|
|
|
self._restore = restore
|
2017-08-29 13:44:36 +00:00
|
|
|
self._step = step
|
|
|
|
self._state = self._initial = initial
|
2019-04-18 10:02:01 +00:00
|
|
|
self._min = minimum
|
|
|
|
self._max = maximum
|
2017-08-29 13:44:36 +00:00
|
|
|
self._icon = icon
|
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
|
|
|
"""If entity should be polled."""
|
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return name of the counter."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Return the icon to be used for this entity."""
|
|
|
|
return self._icon
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the current value of the counter."""
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state_attributes(self):
|
|
|
|
"""Return the state attributes."""
|
2019-04-18 10:02:01 +00:00
|
|
|
ret = {
|
2017-08-29 13:44:36 +00:00
|
|
|
ATTR_INITIAL: self._initial,
|
|
|
|
ATTR_STEP: self._step,
|
|
|
|
}
|
2019-04-18 10:02:01 +00:00
|
|
|
if self._min is not None:
|
|
|
|
ret[CONF_MINIMUM] = self._min
|
|
|
|
if self._max is not None:
|
|
|
|
ret[CONF_MAXIMUM] = self._max
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def compute_next_state(self, state):
|
|
|
|
"""Keep the state within the range of min/max values."""
|
|
|
|
if self._min is not None:
|
|
|
|
state = max(self._min, state)
|
|
|
|
if self._max is not None:
|
|
|
|
state = min(self._max, state)
|
|
|
|
|
|
|
|
return state
|
2017-08-29 13:44:36 +00:00
|
|
|
|
2018-06-01 17:41:20 +00:00
|
|
|
async def async_added_to_hass(self):
|
2017-08-29 13:44:36 +00:00
|
|
|
"""Call when entity about to be added to Home Assistant."""
|
2018-11-28 12:16:43 +00:00
|
|
|
await super().async_added_to_hass()
|
2018-10-03 21:12:21 +00:00
|
|
|
# __init__ will set self._state to self._initial, only override
|
|
|
|
# if needed.
|
|
|
|
if self._restore:
|
2018-11-28 12:16:43 +00:00
|
|
|
state = await self.async_get_last_state()
|
2018-10-03 21:12:21 +00:00
|
|
|
if state is not None:
|
2019-04-18 10:02:01 +00:00
|
|
|
self._state = self.compute_next_state(int(state.state))
|
2017-08-29 13:44:36 +00:00
|
|
|
|
2018-06-01 17:41:20 +00:00
|
|
|
async def async_decrement(self):
|
2017-08-29 13:44:36 +00:00
|
|
|
"""Decrement the counter."""
|
2019-04-18 10:02:01 +00:00
|
|
|
self._state = self.compute_next_state(self._state - self._step)
|
2018-06-01 17:41:20 +00:00
|
|
|
await self.async_update_ha_state()
|
2017-08-29 13:44:36 +00:00
|
|
|
|
2018-06-01 17:41:20 +00:00
|
|
|
async def async_increment(self):
|
2017-08-29 13:44:36 +00:00
|
|
|
"""Increment a counter."""
|
2019-04-18 10:02:01 +00:00
|
|
|
self._state = self.compute_next_state(self._state + self._step)
|
2018-06-01 17:41:20 +00:00
|
|
|
await self.async_update_ha_state()
|
2017-08-29 13:44:36 +00:00
|
|
|
|
2018-06-01 17:41:20 +00:00
|
|
|
async def async_reset(self):
|
2017-08-29 13:44:36 +00:00
|
|
|
"""Reset a counter."""
|
2019-04-18 10:02:01 +00:00
|
|
|
self._state = self.compute_next_state(self._initial)
|
|
|
|
await self.async_update_ha_state()
|
|
|
|
|
|
|
|
async def async_configure(self, **kwargs):
|
|
|
|
"""Change the counter's settings with a service."""
|
|
|
|
if CONF_MINIMUM in kwargs:
|
|
|
|
self._min = kwargs[CONF_MINIMUM]
|
|
|
|
if CONF_MAXIMUM in kwargs:
|
|
|
|
self._max = kwargs[CONF_MAXIMUM]
|
|
|
|
if CONF_STEP in kwargs:
|
|
|
|
self._step = kwargs[CONF_STEP]
|
|
|
|
|
|
|
|
self._state = self.compute_next_state(self._state)
|
2018-06-01 17:41:20 +00:00
|
|
|
await self.async_update_ha_state()
|