From 51dd7182823575cb1fc741c0b453f6643e588718 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sun, 13 Sep 2015 08:08:46 -0700 Subject: [PATCH] Fix broken thermostat demo and prevent happening again --- homeassistant/components/demo.py | 2 +- .../components/thermostat/__init__.py | 40 +++++++++---------- tests/components/test_demo.py | 15 ++++++- 3 files changed, 32 insertions(+), 25 deletions(-) diff --git a/homeassistant/components/demo.py b/homeassistant/components/demo.py index ac80fed4ad0..beb7a63b47c 100644 --- a/homeassistant/components/demo.py +++ b/homeassistant/components/demo.py @@ -17,7 +17,7 @@ DOMAIN = "demo" DEPENDENCIES = ['introduction', 'conversation'] COMPONENTS_WITH_DEMO_PLATFORM = [ - 'switch', 'light', 'sensor', 'media_player', 'notify'] + 'switch', 'light', 'sensor', 'thermostat', 'media_player', 'notify'] def setup(hass, config): diff --git a/homeassistant/components/thermostat/__init__.py b/homeassistant/components/thermostat/__init__.py index 940c656ad94..e9d3c50451b 100644 --- a/homeassistant/components/thermostat/__init__.py +++ b/homeassistant/components/thermostat/__init__.py @@ -133,29 +133,25 @@ class ThermostatDevice(Entity): user_unit = self.hass.config.temperature_unit data = { - ATTR_CURRENT_TEMPERATURE: round(convert(self.current_temperature, - thermostat_unit, - user_unit), 1), - ATTR_MIN_TEMP: round(convert(self.min_temp, - thermostat_unit, - user_unit), 0), - ATTR_MAX_TEMP: round(convert(self.max_temp, - thermostat_unit, - user_unit), 0), - ATTR_TEMPERATURE: round(convert(self.target_temperature, - thermostat_unit, - user_unit), 0), - ATTR_TEMPERATURE_LOW: round(convert(self.target_temperature_low, - thermostat_unit, - user_unit), 0), - ATTR_TEMPERATURE_HIGH: round(convert(self.target_temperature_high, - thermostat_unit, - user_unit), 0), - ATTR_OPERATION: self.operation + ATTR_CURRENT_TEMPERATURE: round(convert( + self.current_temperature, thermostat_unit, user_unit), 1), + ATTR_MIN_TEMP: round(convert( + self.min_temp, thermostat_unit, user_unit), 0), + ATTR_MAX_TEMP: round(convert( + self.max_temp, thermostat_unit, user_unit), 0), + ATTR_TEMPERATURE: round(convert( + self.target_temperature, thermostat_unit, user_unit), 0), + ATTR_TEMPERATURE_LOW: round(convert( + self.target_temperature_low, thermostat_unit, user_unit), 0), + ATTR_TEMPERATURE_HIGH: round(convert( + self.target_temperature_high, thermostat_unit, user_unit), 0), } - is_away = self.is_away_mode_on + operation = self.operation + if operation is not None: + data[ATTR_OPERATION] = operation + is_away = self.is_away_mode_on if is_away is not None: data[ATTR_AWAY_MODE] = STATE_ON if is_away else STATE_OFF @@ -169,7 +165,7 @@ class ThermostatDevice(Entity): @property def unit_of_measurement(self): """ Unit of measurement this thermostat expresses itself in. """ - return NotImplementedError + raise NotImplementedError @property def current_temperature(self): @@ -179,7 +175,7 @@ class ThermostatDevice(Entity): @property def operation(self): """ Returns current operation ie. heat, cool, idle """ - return NotImplementedError + return None @property def target_temperature(self): diff --git a/tests/components/test_demo.py b/tests/components/test_demo.py index 970f51f3bb4..13cc55ed7dc 100644 --- a/tests/components/test_demo.py +++ b/tests/components/test_demo.py @@ -4,15 +4,18 @@ tests.test_component_demo Tests demo component. """ +import json import unittest from unittest.mock import patch import homeassistant.core as ha import homeassistant.components.demo as demo +from homeassistant.remote import JSONEncoder from tests.common import mock_http_component +@patch('homeassistant.components.sun.setup') class TestDemo(unittest.TestCase): """ Test the demo module. """ @@ -24,16 +27,24 @@ class TestDemo(unittest.TestCase): """ Stop down stuff we started. """ self.hass.stop() - @patch('homeassistant.components.sun.setup') def test_if_demo_state_shows_by_default(self, mock_sun_setup): """ Test if demo state shows if we give no configuration. """ demo.setup(self.hass, {demo.DOMAIN: {}}) self.assertIsNotNone(self.hass.states.get('a.Demo_Mode')) - @patch('homeassistant.components.sun.setup') def test_hiding_demo_state(self, mock_sun_setup): """ Test if you can hide the demo card. """ demo.setup(self.hass, {demo.DOMAIN: {'hide_demo_state': 1}}) self.assertIsNone(self.hass.states.get('a.Demo_Mode')) + + def test_all_entities_can_be_loaded_over_json(self, mock_sun_setup): + """ Test if you can hide the demo card. """ + demo.setup(self.hass, {demo.DOMAIN: {'hide_demo_state': 1}}) + + try: + json.dumps(self.hass.states.all(), cls=JSONEncoder) + except Exception: + self.fail('Unable to convert all demo entities to JSON. ' + 'Wrong data in state machine!')