Events and States are no longer dicts but objects.

This commit is contained in:
Paulus Schoutsen committed 2014-01-19 19:10:40 -08:00
1 parent ae2058de70
commit 3c3e7e5825
9 files changed
+212 -129

No files matched your search

+3 -3
View File
@@ -36,10 +36,10 @@ def turn_off(statemachine, cc_id=None):
state = statemachine.get_state(cat)
if state and \
state['state'] != STATE_NO_APP or \
state['state'] != pychromecast.APP_ID_HOME:
state.state != STATE_NO_APP or \
state.state != pychromecast.APP_ID_HOME:
pychromecast.quit_app(state['attributes'][ATTR_HOST])
pychromecast.quit_app(state.attributes[ATTR_HOST])
def setup(bus, statemachine, host):
@@ -92,7 +92,7 @@ def setup(bus, statemachine, light_group=None):
# Specific device came home ?
if (category != device_tracker.STATE_CATEGORY_ALL_DEVICES and
new_state['state'] == ha.STATE_HOME):
new_state.state == ha.STATE_HOME):
# These variables are needed for the elif check
now = datetime.now()
@@ -128,7 +128,7 @@ def setup(bus, statemachine, light_group=None):
# Did all devices leave the house?
elif (category == device_tracker.STATE_CATEGORY_ALL_DEVICES and
new_state['state'] == ha.STATE_NOT_HOME and lights_are_on):
new_state.state == ha.STATE_NOT_HOME and lights_are_on):
logger.info(
"Everyone has left but there are devices on. Turning them off")
+12 -13
View File
@@ -35,12 +35,11 @@ def is_on(statemachine, group):
state = statemachine.get_state(group)
if state:
group_type = _get_group_type(state['state'])
group_type = _get_group_type(state.state)
if group_type:
group_on = _GROUP_TYPES[group_type][0]
return state['state'] == group_on
# We found group_type, compare to ON-state
return state.state == _GROUP_TYPES[group_type][0]
else:
return False
else:
@@ -51,7 +50,7 @@ def get_categories(statemachine, group):
""" Get the categories that make up this group. """
state = statemachine.get_state(group)
return state['attributes'][STATE_ATTR_CATEGORIES] if state else []
return state.attributes[STATE_ATTR_CATEGORIES] if state else []
# pylint: disable=too-many-branches
@@ -73,7 +72,7 @@ def setup(bus, statemachine, name, categories):
# Try to determine group type if we didn't yet
if not group_type and state:
group_type = _get_group_type(state['state'])
group_type = _get_group_type(state.state)
if group_type:
group_on, group_off = _GROUP_TYPES[group_type]
@@ -82,7 +81,7 @@ def setup(bus, statemachine, name, categories):
else:
# We did not find a matching group_type
errors.append("Found unexpected state '{}'".format(
name, state['state']))
name, state.state))
break
@@ -91,13 +90,13 @@ def setup(bus, statemachine, name, categories):
errors.append("Category {} does not exist".format(cat))
# Check if category is valid state
elif state['state'] != group_off and state['state'] != group_on:
elif state.state != group_off and state.state != group_on:
errors.append("State of {} is {} (expected: {}, {})".format(
cat, state['state'], group_off, group_on))
cat, state.state, group_off, group_on))
# Keep track of the group state to init later on
elif group_state == group_off and state['state'] == group_on:
elif group_state == group_off and state.state == group_on:
group_state = group_on
if errors:
@@ -114,17 +113,17 @@ def setup(bus, statemachine, name, categories):
""" Updates the group state based on a state change by a tracked
category. """
cur_group_state = statemachine.get_state(group_cat)['state']
cur_group_state = statemachine.get_state(group_cat).state
# if cur_group_state = OFF and new_state = ON: set ON
# if cur_group_state = ON and new_state = OFF: research
# else: ignore
if cur_group_state == group_off and new_state['state'] == group_on:
if cur_group_state == group_off and new_state.state == group_on:
statemachine.set_state(group_cat, group_on, state_attr)
elif cur_group_state == group_on and new_state['state'] == group_off:
elif cur_group_state == group_on and new_state.state == group_off:
# Check if any of the other states is still on
if not any([statemachine.is_state(cat, group_on)
@@ -341,16 +341,16 @@ class RequestHandler(BaseHTTPRequestHandler):
state = self.server.statemachine.get_state(category)
attributes = "<br>".join(
["{}: {}".format(attr, state['attributes'][attr])
for attr in state['attributes']])
["{}: {}".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'],
state.state,
attributes,
state['last_changed']))
state.last_changed))
# Change state form
write(("<tr><td><input name='category' class='form-control' "
@@ -518,9 +518,8 @@ class RequestHandler(BaseHTTPRequestHandler):
if self.use_json:
state = self.server.statemachine.get_state(category)
state['category'] = category
self._write_json(state, status_code=HTTP_CREATED,
self._write_json(state.to_json_dict(category),
status_code=HTTP_CREATED,
location=
URL_API_STATES_CATEGORY.format(category))
else:
@@ -619,10 +618,7 @@ class RequestHandler(BaseHTTPRequestHandler):
state = self.server.statemachine.get_state(category)
if state:
state['category'] = category
self._write_json(state)
self._write_json(state.to_json_dict(category))
else:
# If category does not exist
self._message("State does not exist.", HTTP_UNPROCESSABLE_ENTITY)
+7 -6
View File
@@ -8,6 +8,7 @@ import logging
from datetime import timedelta
import homeassistant as ha
import homeassistant.util as util
STATE_CATEGORY = "weather.sun"
@@ -27,16 +28,16 @@ def next_setting(statemachine):
""" Returns the datetime object representing the next sun setting. """
state = statemachine.get_state(STATE_CATEGORY)
return None if not state else ha.str_to_datetime(
state['attributes'][STATE_ATTR_NEXT_SETTING])
return None if not state else util.str_to_datetime(
state.attributes[STATE_ATTR_NEXT_SETTING])
def next_rising(statemachine):
""" Returns the datetime object representing the next sun setting. """
state = statemachine.get_state(STATE_CATEGORY)
return None if not state else ha.str_to_datetime(
state['attributes'][STATE_ATTR_NEXT_RISING])
return None if not state else util.str_to_datetime(
state.attributes[STATE_ATTR_NEXT_RISING])
def setup(bus, statemachine, latitude, longitude):
@@ -74,8 +75,8 @@ def setup(bus, statemachine, latitude, longitude):
next_change.strftime("%H:%M")))
state_attributes = {
STATE_ATTR_NEXT_RISING: ha.datetime_to_str(next_rising_dt),
STATE_ATTR_NEXT_SETTING: ha.datetime_to_str(next_setting_dt)
STATE_ATTR_NEXT_RISING: util.datetime_to_str(next_rising_dt),
STATE_ATTR_NEXT_SETTING: util.datetime_to_str(next_setting_dt)
}
statemachine.set_state(STATE_CATEGORY, new_state, state_attributes)