Fix bug in reproduce_state with complex state attributes

This commit is contained in:
Paulus Schoutsen 2016-01-03 11:27:30 -08:00
parent f8b2570cb3
commit 736183e6f5
2 changed files with 24 additions and 2 deletions

View File

@ -1,5 +1,6 @@
"""Helpers that help with state related things.""" """Helpers that help with state related things."""
from collections import defaultdict from collections import defaultdict
import json
import logging import logging
from homeassistant.core import State from homeassistant.core import State
@ -82,10 +83,12 @@ def reproduce_state(hass, states, blocking=False):
service_domain = state.domain service_domain = state.domain
# We group service calls for entities by service call # We group service calls for entities by service call
key = (service_domain, service, tuple(state.attributes.items())) # json used to create a hashable version of dict with maybe lists in it
key = (service_domain, service,
json.dumps(state.attributes, sort_keys=True))
to_call[key].append(state.entity_id) to_call[key].append(state.entity_id)
for (service_domain, service, service_data), entity_ids in to_call.items(): for (service_domain, service, service_data), entity_ids in to_call.items():
data = dict(service_data) data = json.loads(service_data)
data[ATTR_ENTITY_ID] = entity_ids data[ATTR_ENTITY_ID] = entity_ids
hass.services.call(service_domain, service, data, blocking) hass.services.call(service_domain, service, data, blocking)

View File

@ -91,6 +91,25 @@ class TestStateHelpers(unittest.TestCase):
self.assertEqual(SERVICE_TURN_ON, last_call.service) self.assertEqual(SERVICE_TURN_ON, last_call.service)
self.assertEqual(['light.test'], last_call.data.get('entity_id')) self.assertEqual(['light.test'], last_call.data.get('entity_id'))
def test_reproduce_state_with_complex_service_data(self):
calls = mock_service(self.hass, 'light', SERVICE_TURN_ON)
self.hass.states.set('light.test', 'off')
complex_data = ['hello', {'11': '22'}]
state.reproduce_state(self.hass, ha.State('light.test', 'on', {
'complex': complex_data
}))
self.hass.pool.block_till_done()
self.assertTrue(len(calls) > 0)
last_call = calls[-1]
self.assertEqual('light', last_call.domain)
self.assertEqual(SERVICE_TURN_ON, last_call.service)
self.assertEqual(complex_data, last_call.data.get('complex'))
def test_reproduce_state_with_group(self): def test_reproduce_state_with_group(self):
light_calls = mock_service(self.hass, 'light', SERVICE_TURN_ON) light_calls = mock_service(self.hass, 'light', SERVICE_TURN_ON)