diff --git a/homeassistant/__init__.py b/homeassistant/__init__.py index cf462a029e0..871d105a3f0 100644 --- a/homeassistant/__init__.py +++ b/homeassistant/__init__.py @@ -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, diff --git a/homeassistant/actors.py b/homeassistant/actors.py index 36f19b8c886..b9e06a3ede8 100644 --- a/homeassistant/actors.py +++ b/homeassistant/actors.py @@ -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 diff --git a/homeassistant/httpinterface.py b/homeassistant/httpinterface.py index d2a776d8df8..daaba72582f 100644 --- a/homeassistant/httpinterface.py +++ b/homeassistant/httpinterface.py @@ -110,7 +110,7 @@ class RequestHandler(BaseHTTPRequestHandler): write(("" "" - "")) + "")) for category in \ sorted(self.server.statemachine.categories, @@ -120,9 +120,17 @@ class RequestHandler(BaseHTTPRequestHandler): state = self.server.statemachine.get_state(category) - write("". - format(category, state.state, - util.datetime_to_str(state.last_changed))) + attributes = "
".join( + ["{}: {}".format(attr, state.attributes[attr]) + for attr in state.attributes]) + + write(("" + "" + ""). + format(category, + state.state, + util.datetime_to_str(state.last_changed), + attributes)) write("
NameStateLast Changed
Last ChangedAttributes
{}{}{}
{}{}{}{}
") diff --git a/homeassistant/observers.py b/homeassistant/observers.py index 5b7d92749f1..c1468633cd8 100644 --- a/homeassistant/observers.py +++ b/homeassistant/observers.py @@ -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,