2015-03-16 06:36:42 +00:00
|
|
|
"""
|
|
|
|
homeassistant.helpers.state
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Helpers that help with state related things.
|
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
|
2015-08-17 03:44:46 +00:00
|
|
|
from homeassistant.core import State
|
2015-04-29 02:12:05 +00:00
|
|
|
import homeassistant.util.dt as dt_util
|
2015-03-17 06:32:18 +00:00
|
|
|
from homeassistant.const import (
|
2015-09-24 04:06:05 +00:00
|
|
|
STATE_ON, STATE_OFF, SERVICE_TURN_ON, SERVICE_TURN_OFF,
|
2015-09-24 04:20:05 +00:00
|
|
|
SERVICE_MEDIA_PLAY, SERVICE_MEDIA_PAUSE,
|
2015-09-24 04:06:05 +00:00
|
|
|
STATE_PLAYING, STATE_PAUSED, ATTR_ENTITY_ID)
|
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):
|
|
|
|
"""
|
|
|
|
Records 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.
|
|
|
|
"""
|
|
|
|
def __init__(self, hass):
|
|
|
|
self.hass = hass
|
|
|
|
self.states = []
|
|
|
|
|
|
|
|
def __enter__(self):
|
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):
|
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):
|
|
|
|
"""
|
|
|
|
Returns all states that have been changed since utc_point_in_time.
|
|
|
|
"""
|
|
|
|
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):
|
2015-03-16 06:36:42 +00:00
|
|
|
""" Takes in a state and will try to have the entity reproduce it. """
|
|
|
|
if isinstance(states, State):
|
|
|
|
states = [states]
|
|
|
|
|
|
|
|
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-09-24 04:06:05 +00:00
|
|
|
if state.domain == 'media_player' and state == STATE_PAUSED:
|
2015-09-24 04:20:05 +00:00
|
|
|
service = SERVICE_MEDIA_PAUSE
|
2015-09-24 04:06:05 +00:00
|
|
|
elif state.domain == 'media_player' and state == STATE_PLAYING:
|
2015-09-24 04:20:05 +00:00
|
|
|
service = SERVICE_MEDIA_PLAY
|
2015-09-24 04:20:17 +00:00
|
|
|
else:
|
2015-09-24 04:06:05 +00:00
|
|
|
if state.state == STATE_ON:
|
|
|
|
service = SERVICE_TURN_ON
|
|
|
|
elif state.state == STATE_OFF:
|
|
|
|
service = SERVICE_TURN_OFF
|
|
|
|
else:
|
|
|
|
_LOGGER.warning("reproduce_state: Unable to reproduce state %s",
|
|
|
|
state)
|
|
|
|
continue
|
2015-03-17 06:32:18 +00:00
|
|
|
|
|
|
|
service_data = dict(state.attributes)
|
|
|
|
service_data[ATTR_ENTITY_ID] = state.entity_id
|
|
|
|
|
|
|
|
hass.services.call(state.domain, service, service_data, blocking)
|