Skip to content

Commit

Permalink
Scene bugfixes and UI improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
balloob committed Mar 17, 2015
1 parent b459a29 commit 1245af3
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 37 deletions.
5 changes: 5 additions & 0 deletions homeassistant/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,11 @@ def __init__(self, entity_id, state, attributes=None, last_changed=None):
self.last_changed = util.strip_microseconds(
last_changed or self.last_updated)

@property
def domain(self):
""" Returns domain of this state. """
return util.split_entity_id(self.entity_id)[0]

def copy(self):
""" Creates a copy of itself. """
return State(self.entity_id, self.state,
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/frontend/version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
""" DO NOT MODIFY. Auto-generated by build_frontend script """
VERSION = "1d8b14c387123a4b42fec6b8f9346675"
VERSION = "c47a52ae084d870c0c306eea8c27f507"
12 changes: 5 additions & 7 deletions homeassistant/components/frontend/www_static/frontend.html

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
<link rel="import" href="../bower_components/polymer/polymer.html">

<link rel="import" href="./state-card-display.html">
<link rel="import" href="../components/state-info.html">

<polymer-element name="state-card-configurator" attributes="stateObj" noscript>
<template>
<style>
.state {
margin-left: 16px;
text-transform: capitalize;
font-weight: 300;
font-size: 1.3rem;
text-align: right;
}
</style>

<div horizontal justified layout>
<state-info stateObj="{{stateObj}}"></state-info>
<div class='state'>{{stateObj.stateDisplay}}</div>
</div>
<state-card-display stateObj="{{stateObj}}"></state-card-display>

<!-- pre load the image so the dialog is rendered the proper size -->
<template if="{{stateObj.attributes.description_image}}">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<link rel="import" href="state-card-toggle.html">
<link rel="import" href="state-card-thermostat.html">
<link rel="import" href="state-card-configurator.html">
<link rel="import" href="state-card-scene.html">

<polymer-element name="state-card-content" attributes="stateObj">
<template>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<link rel="import" href="../bower_components/polymer/polymer.html">

<link rel="import" href="./state-card-display.html">
<link rel="import" href="./state-card-toggle.html">

<polymer-element name="state-card-scene" attributes="stateObj">
<template>
<template if={{allowToggle}}>
<state-card-toggle stateObj="{{stateObj}}"></state-card-toggle>
</template>
<template if={{!allowToggle}}>
<state-card-display stateObj="{{stateObj}}"></state-card-display>
</template>
</template>
<script>
(function() {
Polymer({
allowToggle: false,

stateObjChanged: function(oldVal, newVal) {
this.allowToggle = newVal.state === 'off' ||
newVal.attributes.active_requested;
},
});
})();
</script>
</polymer-element>
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ <h3>Hi there!</h3>
if (this.filter) {
var filter = this.filter;
states = stateStore.all.filter(function(state) {
console.log(state, state.domain, filter);
return state.domain === filter;
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<script>
(function() {
var DOMAINS_WITH_CARD = ['thermostat', 'configurator'];
var DOMAINS_WITH_CARD = ['thermostat', 'configurator', 'scene'];
var DOMAINS_WITH_MORE_INFO = [
'light', 'group', 'sun', 'configurator', 'thermostat', 'script'
];
Expand Down
24 changes: 16 additions & 8 deletions homeassistant/components/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ def handle_scene_service(service):
else:
scene.turn_off()

scene.update_ha_state(True)

hass.services.register(DOMAIN, SERVICE_TURN_OFF, handle_scene_service)
hass.services.register(DOMAIN, SERVICE_TURN_ON, handle_scene_service)

Expand Down Expand Up @@ -104,6 +102,7 @@ def __init__(self, hass, scene_config):

self.is_active = False
self.prev_states = None
self.ignore_updates = False

self.hass.states.track_change(
self.entity_ids, self.entity_state_changed)
Expand Down Expand Up @@ -140,23 +139,27 @@ def turn_on(self):
self.prev_states = tuple(self.hass.states.get(entity_id)
for entity_id in self.entity_ids)

reproduce_state(self.hass, self.scene_config.states.values())
self._reproduce_state(self.scene_config.states.values())

def turn_off(self):
""" Deactivates scene and restores old states. """
if self.prev_states:
reproduce_state(self.hass, self.prev_states)
self._reproduce_state(self.prev_states)
self.prev_states = None

def entity_state_changed(self, entity_id, old_state, new_state):
""" Called when an entity part of this scene changes state. """
if self.ignore_updates:
return

# If new state is not what we expect, it can never be active
if self._state_as_requested(new_state):
self.update()
else:
self.is_active = False
self.prev_states = None

self.update_ha_state(True)
self.update_ha_state()

def update(self):
"""
Expand All @@ -170,13 +173,18 @@ def update(self):
self._state_as_requested(self.hass.states.get(entity_id))
for entity_id in self.entity_ids)

if not self.is_active and self.prev_states:
self.prev_states = None

def _state_as_requested(self, cur_state):
""" Returns if given state is as requested. """
state = self.scene_config.states.get(cur_state and cur_state.entity_id)

return (cur_state is not None and state.state == cur_state.state and
all(value == cur_state.attributes.get(key)
for key, value in state.attributes.items()))

def _reproduce_state(self, states):
""" Wraps reproduce state with Scence specific logic. """
self.ignore_updates = True
reproduce_state(self.hass, states, True)
self.ignore_updates = False

self.update_ha_state(True)
16 changes: 11 additions & 5 deletions homeassistant/helpers/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
from datetime import datetime

from homeassistant import State
from homeassistant.const import STATE_ON, STATE_OFF
import homeassistant.components as core_components
from homeassistant.const import (
STATE_ON, STATE_OFF, SERVICE_TURN_ON, SERVICE_TURN_OFF, ATTR_ENTITY_ID)

_LOGGER = logging.getLogger(__name__)

Expand All @@ -33,7 +33,7 @@ def __exit__(self, exc_type, exc_value, traceback):
self.states.extend(self.hass.states.get_since(self.now))


def reproduce_state(hass, states):
def reproduce_state(hass, states, blocking=False):
""" Takes in a state and will try to have the entity reproduce it. """
if isinstance(states, State):
states = [states]
Expand All @@ -45,8 +45,14 @@ def reproduce_state(hass, states):
continue

if state.state == STATE_ON:
core_components.turn_on(hass, state.entity_id, **state.attributes)
service = SERVICE_TURN_ON
elif state.state == STATE_OFF:
core_components.turn_off(hass, state.entity_id, **state.attributes)
service = SERVICE_TURN_OFF
else:
_LOGGER.warning("Unable to reproduce state for %s", state)
continue

service_data = dict(state.attributes)
service_data[ATTR_ENTITY_ID] = state.entity_id

hass.services.call(state.domain, service, service_data, blocking)

0 comments on commit 1245af3

Please sign in to comment.