2016-01-03 10:32:09 +00:00
|
|
|
"""Helpers that help with state related things."""
|
2016-01-03 19:27:30 +00:00
|
|
|
import json
|
2015-03-16 06:36:42 +00:00
|
|
|
import logging
|
2016-02-19 05:27:50 +00:00
|
|
|
from collections import defaultdict
|
2015-03-16 06:36:42 +00:00
|
|
|
|
2015-04-29 02:12:05 +00:00
|
|
|
import homeassistant.util.dt as dt_util
|
2016-02-19 05:27:50 +00:00
|
|
|
from homeassistant.components.media_player import SERVICE_PLAY_MEDIA
|
|
|
|
from homeassistant.components.sun import (
|
|
|
|
STATE_ABOVE_HORIZON, STATE_BELOW_HORIZON)
|
2015-03-17 06:32:18 +00:00
|
|
|
from homeassistant.const import (
|
2016-02-19 05:27:50 +00:00
|
|
|
ATTR_ENTITY_ID, SERVICE_MEDIA_PAUSE, SERVICE_MEDIA_PLAY, SERVICE_TURN_OFF,
|
|
|
|
SERVICE_TURN_ON, STATE_CLOSED, STATE_LOCKED, STATE_OFF, STATE_ON,
|
|
|
|
STATE_OPEN, STATE_PAUSED, STATE_PLAYING, STATE_UNKNOWN, STATE_UNLOCKED)
|
|
|
|
from homeassistant.core import State
|
2015-10-07 04:39:38 +00:00
|
|
|
|
2015-03-16 06:36:42 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable=too-few-public-methods, attribute-defined-outside-init
|
|
|
|
class TrackStates(object):
|
2016-03-07 22:39:52 +00:00
|
|
|
"""Record the time when the with-block is entered.
|
|
|
|
|
|
|
|
Will add all states that have changed since the start time to the return
|
|
|
|
list when with-block is exited.
|
2015-03-16 06:36:42 +00:00
|
|
|
"""
|
2016-01-03 10:32:09 +00:00
|
|
|
|
2015-03-16 06:36:42 +00:00
|
|
|
def __init__(self, hass):
|
2016-01-03 10:32:09 +00:00
|
|
|
"""Initialize a TrackStates block."""
|
2015-03-16 06:36:42 +00:00
|
|
|
self.hass = hass
|
|
|
|
self.states = []
|
|
|
|
|
|
|
|
def __enter__(self):
|
2016-01-03 10:32:09 +00:00
|
|
|
"""Record time from which to track changes."""
|
2015-04-29 02:12:05 +00:00
|
|
|
self.now = dt_util.utcnow()
|
2015-03-16 06:36:42 +00:00
|
|
|
return self.states
|
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_value, traceback):
|
2016-01-03 10:32:09 +00:00
|
|
|
"""Add changes states to changes list."""
|
2015-07-26 08:45:49 +00:00
|
|
|
self.states.extend(get_changed_since(self.hass.states.all(), self.now))
|
|
|
|
|
|
|
|
|
|
|
|
def get_changed_since(states, utc_point_in_time):
|
2016-01-03 10:32:09 +00:00
|
|
|
"""List of states that have been changed since utc_point_in_time."""
|
2015-07-26 08:45:49 +00:00
|
|
|
point_in_time = dt_util.strip_microseconds(utc_point_in_time)
|
|
|
|
|
|
|
|
return [state for state in states if state.last_updated >= point_in_time]
|
2015-03-16 06:36:42 +00:00
|
|
|
|
|
|
|
|
2015-03-17 06:32:18 +00:00
|
|
|
def reproduce_state(hass, states, blocking=False):
|
2016-01-03 10:32:09 +00:00
|
|
|
"""Reproduce given state."""
|
2015-03-16 06:36:42 +00:00
|
|
|
if isinstance(states, State):
|
|
|
|
states = [states]
|
|
|
|
|
2016-01-03 10:32:09 +00:00
|
|
|
to_call = defaultdict(list)
|
|
|
|
|
2015-03-16 06:36:42 +00:00
|
|
|
for state in states:
|
|
|
|
current_state = hass.states.get(state.entity_id)
|
|
|
|
|
|
|
|
if current_state is None:
|
2015-09-16 05:23:07 +00:00
|
|
|
_LOGGER.warning('reproduce_state: Unable to find entity %s',
|
|
|
|
state.entity_id)
|
2015-03-16 06:36:42 +00:00
|
|
|
continue
|
|
|
|
|
2015-10-09 00:37:59 +00:00
|
|
|
if state.domain == 'media_player' and state.attributes and \
|
2015-10-07 05:29:55 +00:00
|
|
|
'media_type' in state.attributes and \
|
2015-10-07 05:37:40 +00:00
|
|
|
'media_id' in state.attributes:
|
2015-10-07 04:39:38 +00:00
|
|
|
service = SERVICE_PLAY_MEDIA
|
2015-10-09 00:37:59 +00:00
|
|
|
elif state.domain == 'media_player' and state.state == STATE_PAUSED:
|
|
|
|
service = SERVICE_MEDIA_PAUSE
|
|
|
|
elif state.domain == 'media_player' and state.state == STATE_PLAYING:
|
|
|
|
service = SERVICE_MEDIA_PLAY
|
2015-09-24 05:35:08 +00:00
|
|
|
elif state.state == STATE_ON:
|
|
|
|
service = SERVICE_TURN_ON
|
|
|
|
elif state.state == STATE_OFF:
|
|
|
|
service = SERVICE_TURN_OFF
|
2015-09-24 04:20:17 +00:00
|
|
|
else:
|
2015-09-24 05:35:08 +00:00
|
|
|
_LOGGER.warning("reproduce_state: Unable to reproduce state %s",
|
|
|
|
state)
|
|
|
|
continue
|
2015-03-17 06:32:18 +00:00
|
|
|
|
2016-01-03 10:32:09 +00:00
|
|
|
if state.domain == 'group':
|
|
|
|
service_domain = 'homeassistant'
|
|
|
|
else:
|
|
|
|
service_domain = state.domain
|
|
|
|
|
|
|
|
# We group service calls for entities by service call
|
2016-01-03 19:27:30 +00:00
|
|
|
# json used to create a hashable version of dict with maybe lists in it
|
|
|
|
key = (service_domain, service,
|
2016-02-10 07:27:01 +00:00
|
|
|
json.dumps(dict(state.attributes), sort_keys=True))
|
2016-01-03 10:32:09 +00:00
|
|
|
to_call[key].append(state.entity_id)
|
2015-03-17 06:32:18 +00:00
|
|
|
|
2016-01-03 10:32:09 +00:00
|
|
|
for (service_domain, service, service_data), entity_ids in to_call.items():
|
2016-01-03 19:27:30 +00:00
|
|
|
data = json.loads(service_data)
|
2016-01-03 10:32:09 +00:00
|
|
|
data[ATTR_ENTITY_ID] = entity_ids
|
|
|
|
hass.services.call(service_domain, service, data, blocking)
|
2016-02-11 17:10:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
def state_as_number(state):
|
|
|
|
"""Try to coerce our state to a number.
|
|
|
|
|
|
|
|
Raises ValueError if this is not possible.
|
|
|
|
"""
|
|
|
|
if state.state in (STATE_ON, STATE_LOCKED, STATE_ABOVE_HORIZON,
|
|
|
|
STATE_OPEN):
|
|
|
|
return 1
|
|
|
|
elif state.state in (STATE_OFF, STATE_UNLOCKED, STATE_UNKNOWN,
|
|
|
|
STATE_BELOW_HORIZON, STATE_CLOSED):
|
|
|
|
return 0
|
2016-02-13 08:08:32 +00:00
|
|
|
|
|
|
|
return float(state.state)
|