StateMachine now supports attributes for states
parent
c1819e19a4
commit
66d432d0a2
|
@ -26,7 +26,7 @@ TIMER_INTERVAL = 10 # seconds
|
|||
# every minute.
|
||||
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):
|
||||
""" Start home assistant. """
|
||||
|
@ -181,21 +181,29 @@ class StateMachine(object):
|
|||
""" List of categories which states are being tracked. """
|
||||
return self.states.keys()
|
||||
|
||||
def set_state(self, category, new_state):
|
||||
""" Set the state of a category, add category if it does not exist. """
|
||||
def set_state(self, category, new_state, attributes=None):
|
||||
""" 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()
|
||||
|
||||
# Add category if it does not exist
|
||||
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
|
||||
else:
|
||||
old_state = self.states[category]
|
||||
|
||||
if old_state.state != new_state:
|
||||
self.states[category] = State(new_state, datetime.now())
|
||||
if old_state.state != new_state or \
|
||||
old_state.attributes != attributes:
|
||||
|
||||
self.states[category] = State(new_state, datetime.now(),
|
||||
attributes)
|
||||
|
||||
self.eventbus.fire(EVENT_STATE_CHANGED,
|
||||
{'category':category,
|
||||
|
|
|
@ -19,7 +19,7 @@ import homeassistant.util as util
|
|||
from homeassistant.observers import (
|
||||
STATE_CATEGORY_SUN, SUN_STATE_BELOW_HORIZON, SUN_STATE_ABOVE_HORIZON,
|
||||
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)
|
||||
|
||||
|
@ -171,8 +171,10 @@ class LightTrigger(object):
|
|||
|
||||
def _next_sun_setting(self):
|
||||
""" Returns the datetime object representing the next sun setting. """
|
||||
state = self.statemachine.get_state(STATE_CATEGORY_SUN)
|
||||
|
||||
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):
|
||||
""" Helper method to calculate the point in time we have to start
|
||||
|
|
|
@ -110,7 +110,7 @@ class RequestHandler(BaseHTTPRequestHandler):
|
|||
|
||||
write(("<table><tr>"
|
||||
"<th>Name</th><th>State</th>"
|
||||
"<th>Last Changed</th></tr>"))
|
||||
"<th>Last Changed</th><th>Attributes</th></tr>"))
|
||||
|
||||
for category in \
|
||||
sorted(self.server.statemachine.categories,
|
||||
|
@ -120,9 +120,17 @@ class RequestHandler(BaseHTTPRequestHandler):
|
|||
|
||||
state = self.server.statemachine.get_state(category)
|
||||
|
||||
write("<tr><td>{}</td><td>{}</td><td>{}</td></tr>".
|
||||
format(category, state.state,
|
||||
util.datetime_to_str(state.last_changed)))
|
||||
attributes = "<br>".join(
|
||||
["{}: {}".format(attr, state.attributes[attr])
|
||||
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>")
|
||||
|
||||
|
|
|
@ -21,8 +21,8 @@ import homeassistant as ha
|
|||
import homeassistant.util as util
|
||||
|
||||
STATE_CATEGORY_SUN = "weather.sun"
|
||||
STATE_CATEGORY_NEXT_SUN_RISING = "weather.sun.next_rising"
|
||||
STATE_CATEGORY_NEXT_SUN_SETTING = "weather.sun.next_setting"
|
||||
STATE_ATTRIBUTE_NEXT_SUN_RISING = "next_rising"
|
||||
STATE_ATTRIBUTE_NEXT_SUN_SETTING = "next_setting"
|
||||
STATE_CATEGORY_ALL_DEVICES = 'all_devices'
|
||||
STATE_CATEGORY_DEVICE_FORMAT = '{}'
|
||||
|
||||
|
@ -76,11 +76,12 @@ def track_sun(eventbus, statemachine, latitude, longitude):
|
|||
logger.info("Sun:{}. Next change: {}".
|
||||
format(new_state, next_change.strftime("%H:%M")))
|
||||
|
||||
statemachine.set_state(STATE_CATEGORY_SUN, new_state)
|
||||
statemachine.set_state(STATE_CATEGORY_NEXT_SUN_RISING,
|
||||
util.datetime_to_str(next_rising))
|
||||
statemachine.set_state(STATE_CATEGORY_NEXT_SUN_SETTING,
|
||||
util.datetime_to_str(next_setting))
|
||||
state_attributes = {
|
||||
STATE_ATTRIBUTE_NEXT_SUN_RISING: util.datetime_to_str(next_rising),
|
||||
STATE_ATTRIBUTE_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
|
||||
ha.track_time_change(eventbus, update_sun_state,
|
||||
|
|
Loading…
Reference in New Issue