From 736183e6f5c09f08a5ae202832cb03a25a54198d Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sun, 3 Jan 2016 11:27:30 -0800 Subject: [PATCH] Fix bug in reproduce_state with complex state attributes --- homeassistant/helpers/state.py | 7 +++++-- tests/helpers/test_state.py | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/homeassistant/helpers/state.py b/homeassistant/helpers/state.py index 019e7ce6ce9..c8f6f05661a 100644 --- a/homeassistant/helpers/state.py +++ b/homeassistant/helpers/state.py @@ -1,5 +1,6 @@ """Helpers that help with state related things.""" from collections import defaultdict +import json import logging from homeassistant.core import State @@ -82,10 +83,12 @@ def reproduce_state(hass, states, blocking=False): service_domain = state.domain # 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) 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 hass.services.call(service_domain, service, data, blocking) diff --git a/tests/helpers/test_state.py b/tests/helpers/test_state.py index 32924b1d6d5..f4e28330f7a 100644 --- a/tests/helpers/test_state.py +++ b/tests/helpers/test_state.py @@ -91,6 +91,25 @@ class TestStateHelpers(unittest.TestCase): self.assertEqual(SERVICE_TURN_ON, last_call.service) 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): light_calls = mock_service(self.hass, 'light', SERVICE_TURN_ON)