StateMachine now supports attributes for states

pull/2/head
Paulus Schoutsen 2013-10-24 07:57:08 +01:00
parent c1819e19a4
commit 66d432d0a2
4 changed files with 38 additions and 19 deletions

View File

@ -26,7 +26,7 @@ TIMER_INTERVAL = 10 # seconds
# every minute. # every minute.
assert 60 % TIMER_INTERVAL == 0, "60 % TIMER_INTERVAL should be 0!" assert 60 % TIMER_INTERVAL == 0, "60 % TIMER_INTERVAL should be 0!"
State = namedtuple("State", ['state','last_changed']) State = namedtuple("State", ['state', 'last_changed', 'attributes'])
def start_home_assistant(eventbus): def start_home_assistant(eventbus):
""" Start home assistant. """ """ Start home assistant. """
@ -181,21 +181,29 @@ class StateMachine(object):
""" List of categories which states are being tracked. """ """ List of categories which states are being tracked. """
return self.states.keys() return self.states.keys()
def set_state(self, category, new_state): def set_state(self, category, new_state, attributes=None):
""" Set the state of a category, add category if it does not exist. """ """ Set the state of a category, add category if it does not exist.
Attributes is an optional dict to specify attributes of this state. """
attributes = attributes or {}
self.lock.acquire() self.lock.acquire()
# Add category if it does not exist # Add category if it does not exist
if category not in self.states: if category not in self.states:
self.states[category] = State(new_state, datetime.now()) self.states[category] = State(new_state, datetime.now(),
attributes)
# Change state and fire listeners # Change state and fire listeners
else: else:
old_state = self.states[category] old_state = self.states[category]
if old_state.state != new_state: if old_state.state != new_state or \
self.states[category] = State(new_state, datetime.now()) old_state.attributes != attributes:
self.states[category] = State(new_state, datetime.now(),
attributes)
self.eventbus.fire(EVENT_STATE_CHANGED, self.eventbus.fire(EVENT_STATE_CHANGED,
{'category':category, {'category':category,

View File

@ -19,7 +19,7 @@ import homeassistant.util as util
from homeassistant.observers import ( from homeassistant.observers import (
STATE_CATEGORY_SUN, SUN_STATE_BELOW_HORIZON, SUN_STATE_ABOVE_HORIZON, STATE_CATEGORY_SUN, SUN_STATE_BELOW_HORIZON, SUN_STATE_ABOVE_HORIZON,
STATE_CATEGORY_ALL_DEVICES, DEVICE_STATE_HOME, DEVICE_STATE_NOT_HOME, STATE_CATEGORY_ALL_DEVICES, DEVICE_STATE_HOME, DEVICE_STATE_NOT_HOME,
STATE_CATEGORY_NEXT_SUN_SETTING) STATE_ATTRIBUTE_NEXT_SUN_SETTING)
LIGHT_TRANSITION_TIME = timedelta(minutes=15) LIGHT_TRANSITION_TIME = timedelta(minutes=15)
@ -171,8 +171,10 @@ class LightTrigger(object):
def _next_sun_setting(self): def _next_sun_setting(self):
""" Returns the datetime object representing the next sun setting. """ """ Returns the datetime object representing the next sun setting. """
state = self.statemachine.get_state(STATE_CATEGORY_SUN)
return util.str_to_datetime( return util.str_to_datetime(
self.statemachine.get_state(STATE_CATEGORY_NEXT_SUN_SETTING).state) state.attributes[STATE_ATTRIBUTE_NEXT_SUN_SETTING])
def _time_for_light_before_sun_set(self): def _time_for_light_before_sun_set(self):
""" Helper method to calculate the point in time we have to start """ Helper method to calculate the point in time we have to start

View File

@ -110,7 +110,7 @@ class RequestHandler(BaseHTTPRequestHandler):
write(("<table><tr>" write(("<table><tr>"
"<th>Name</th><th>State</th>" "<th>Name</th><th>State</th>"
"<th>Last Changed</th></tr>")) "<th>Last Changed</th><th>Attributes</th></tr>"))
for category in \ for category in \
sorted(self.server.statemachine.categories, sorted(self.server.statemachine.categories,
@ -120,9 +120,17 @@ class RequestHandler(BaseHTTPRequestHandler):
state = self.server.statemachine.get_state(category) state = self.server.statemachine.get_state(category)
write("<tr><td>{}</td><td>{}</td><td>{}</td></tr>". attributes = "<br>".join(
format(category, state.state, ["{}: {}".format(attr, state.attributes[attr])
util.datetime_to_str(state.last_changed))) for attr in state.attributes])
write(("<tr>"
"<td>{}</td><td>{}</td><td>{}</td><td>{}</td>"
"</tr>").
format(category,
state.state,
util.datetime_to_str(state.last_changed),
attributes))
write("</table>") write("</table>")

View File

@ -21,8 +21,8 @@ import homeassistant as ha
import homeassistant.util as util import homeassistant.util as util
STATE_CATEGORY_SUN = "weather.sun" STATE_CATEGORY_SUN = "weather.sun"
STATE_CATEGORY_NEXT_SUN_RISING = "weather.sun.next_rising" STATE_ATTRIBUTE_NEXT_SUN_RISING = "next_rising"
STATE_CATEGORY_NEXT_SUN_SETTING = "weather.sun.next_setting" STATE_ATTRIBUTE_NEXT_SUN_SETTING = "next_setting"
STATE_CATEGORY_ALL_DEVICES = 'all_devices' STATE_CATEGORY_ALL_DEVICES = 'all_devices'
STATE_CATEGORY_DEVICE_FORMAT = '{}' STATE_CATEGORY_DEVICE_FORMAT = '{}'
@ -76,11 +76,12 @@ def track_sun(eventbus, statemachine, latitude, longitude):
logger.info("Sun:{}. Next change: {}". logger.info("Sun:{}. Next change: {}".
format(new_state, next_change.strftime("%H:%M"))) format(new_state, next_change.strftime("%H:%M")))
statemachine.set_state(STATE_CATEGORY_SUN, new_state) state_attributes = {
statemachine.set_state(STATE_CATEGORY_NEXT_SUN_RISING, STATE_ATTRIBUTE_NEXT_SUN_RISING: util.datetime_to_str(next_rising),
util.datetime_to_str(next_rising)) STATE_ATTRIBUTE_NEXT_SUN_SETTING: util.datetime_to_str(next_setting)
statemachine.set_state(STATE_CATEGORY_NEXT_SUN_SETTING, }
util.datetime_to_str(next_setting))
statemachine.set_state(STATE_CATEGORY_SUN, new_state, state_attributes)
# +10 seconds to be sure that the change has occured # +10 seconds to be sure that the change has occured
ha.track_time_change(eventbus, update_sun_state, ha.track_time_change(eventbus, update_sun_state,