From 3c8a067b62707869e9b713df3ec09463af64e9a4 Mon Sep 17 00:00:00 2001 From: Diogo Gomes Date: Sat, 28 Apr 2018 20:37:33 +0100 Subject: [PATCH 1/6] Add restore_state to optimistic switch --- homeassistant/components/switch/mqtt.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/homeassistant/components/switch/mqtt.py b/homeassistant/components/switch/mqtt.py index f3bd0bef012a06..b4ce25a3ffb138 100644 --- a/homeassistant/components/switch/mqtt.py +++ b/homeassistant/components/switch/mqtt.py @@ -20,6 +20,7 @@ CONF_PAYLOAD_ON, CONF_ICON) import homeassistant.components.mqtt as mqtt import homeassistant.helpers.config_validation as cv +from homeassistant.helpers.restore_state import async_get_last_state _LOGGER = logging.getLogger(__name__) @@ -114,6 +115,13 @@ def state_message_received(topic, payload, qos): self.hass, self._state_topic, state_message_received, self._qos) + if self._optimistic: + last_state = yield from async_get_last_state(self.hass, + self.entity_id) + if last_state: + self._state = last_state.state + self.async_schedule_update_ha_state() + @property def should_poll(self): """Return the polling state.""" From f9489d64634f2c86bd1bea79b2b06e12b70f06b6 Mon Sep 17 00:00:00 2001 From: Diogo Gomes Date: Tue, 1 May 2018 23:49:45 +0100 Subject: [PATCH 2/6] no need to schedule update --- homeassistant/components/switch/mqtt.py | 1 - 1 file changed, 1 deletion(-) diff --git a/homeassistant/components/switch/mqtt.py b/homeassistant/components/switch/mqtt.py index b4ce25a3ffb138..c6336aa58f48b2 100644 --- a/homeassistant/components/switch/mqtt.py +++ b/homeassistant/components/switch/mqtt.py @@ -120,7 +120,6 @@ def state_message_received(topic, payload, qos): self.entity_id) if last_state: self._state = last_state.state - self.async_schedule_update_ha_state() @property def should_poll(self): From abdafd136bae8bc7c7509d389fafcfe553e1ecf8 Mon Sep 17 00:00:00 2001 From: Diogo Gomes Date: Wed, 2 May 2018 21:27:38 +0100 Subject: [PATCH 3/6] test added --- homeassistant/components/switch/mqtt.py | 4 ++-- tests/components/switch/test_mqtt.py | 30 +++++++++++++++---------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/homeassistant/components/switch/mqtt.py b/homeassistant/components/switch/mqtt.py index c6336aa58f48b2..8b2de1518d3f76 100644 --- a/homeassistant/components/switch/mqtt.py +++ b/homeassistant/components/switch/mqtt.py @@ -17,7 +17,7 @@ from homeassistant.components.switch import SwitchDevice from homeassistant.const import ( CONF_NAME, CONF_OPTIMISTIC, CONF_VALUE_TEMPLATE, CONF_PAYLOAD_OFF, - CONF_PAYLOAD_ON, CONF_ICON) + CONF_PAYLOAD_ON, CONF_ICON, STATE_ON, STATE_OFF) import homeassistant.components.mqtt as mqtt import homeassistant.helpers.config_validation as cv from homeassistant.helpers.restore_state import async_get_last_state @@ -119,7 +119,7 @@ def state_message_received(topic, payload, qos): last_state = yield from async_get_last_state(self.hass, self.entity_id) if last_state: - self._state = last_state.state + self._state = last_state.state == STATE_ON @property def should_poll(self): diff --git a/tests/components/switch/test_mqtt.py b/tests/components/switch/test_mqtt.py index f79d070632107b..b5e2a0b0395497 100644 --- a/tests/components/switch/test_mqtt.py +++ b/tests/components/switch/test_mqtt.py @@ -1,12 +1,14 @@ """The tests for the MQTT switch platform.""" import unittest +from unittest.mock import patch from homeassistant.setup import setup_component from homeassistant.const import STATE_ON, STATE_OFF, STATE_UNAVAILABLE,\ ATTR_ASSUMED_STATE +import homeassistant.core as ha import homeassistant.components.switch as switch from tests.common import ( - mock_mqtt_component, fire_mqtt_message, get_test_home_assistant) + mock_mqtt_component, fire_mqtt_message, get_test_home_assistant, mock_coro) class TestSwitchMQTT(unittest.TestCase): @@ -52,19 +54,23 @@ def test_controlling_state_via_topic(self): def test_sending_mqtt_commands_and_optimistic(self): """Test the sending MQTT commands in optimistic mode.""" - assert setup_component(self.hass, switch.DOMAIN, { - switch.DOMAIN: { - 'platform': 'mqtt', - 'name': 'test', - 'command_topic': 'command-topic', - 'payload_on': 'beer on', - 'payload_off': 'beer off', - 'qos': '2' - } - }) + fake_state = ha.State('switch.test', 'on') + + with patch('homeassistant.components.switch.mqtt.async_get_last_state', + return_value=mock_coro(fake_state)): + assert setup_component(self.hass, switch.DOMAIN, { + switch.DOMAIN: { + 'platform': 'mqtt', + 'name': 'test', + 'command_topic': 'command-topic', + 'payload_on': 'beer on', + 'payload_off': 'beer off', + 'qos': '2' + } + }) state = self.hass.states.get('switch.test') - self.assertEqual(STATE_OFF, state.state) + self.assertEqual(STATE_ON, state.state) self.assertTrue(state.attributes.get(ATTR_ASSUMED_STATE)) switch.turn_on(self.hass, 'switch.test') From f6559e164bf3f91c7849dfcbf1e227a7c766445a Mon Sep 17 00:00:00 2001 From: Diogo Gomes Date: Wed, 2 May 2018 21:29:34 +0100 Subject: [PATCH 4/6] lint --- homeassistant/components/switch/mqtt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/switch/mqtt.py b/homeassistant/components/switch/mqtt.py index 8b2de1518d3f76..91118e42b2a100 100644 --- a/homeassistant/components/switch/mqtt.py +++ b/homeassistant/components/switch/mqtt.py @@ -17,7 +17,7 @@ from homeassistant.components.switch import SwitchDevice from homeassistant.const import ( CONF_NAME, CONF_OPTIMISTIC, CONF_VALUE_TEMPLATE, CONF_PAYLOAD_OFF, - CONF_PAYLOAD_ON, CONF_ICON, STATE_ON, STATE_OFF) + CONF_PAYLOAD_ON, CONF_ICON, STATE_ON) import homeassistant.components.mqtt as mqtt import homeassistant.helpers.config_validation as cv from homeassistant.helpers.restore_state import async_get_last_state From 39f0c436ebb29f4bda5edf69e3761ff568dcb237 Mon Sep 17 00:00:00 2001 From: Diogo Gomes Date: Wed, 2 May 2018 22:05:10 +0100 Subject: [PATCH 5/6] new async syntax --- homeassistant/components/switch/mqtt.py | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/homeassistant/components/switch/mqtt.py b/homeassistant/components/switch/mqtt.py index 91118e42b2a100..3a31f232c32f3b 100644 --- a/homeassistant/components/switch/mqtt.py +++ b/homeassistant/components/switch/mqtt.py @@ -4,7 +4,6 @@ For more details about this platform, please refer to the documentation at https://home-assistant.io/components/switch.mqtt/ """ -import asyncio import logging import voluptuous as vol @@ -40,8 +39,7 @@ }).extend(mqtt.MQTT_AVAILABILITY_SCHEMA.schema) -@asyncio.coroutine -def async_setup_platform(hass, config, async_add_devices, discovery_info=None): +async def async_setup_platform(hass, config, async_add_devices, discovery_info=None): """Set up the MQTT switch.""" if discovery_info is not None: config = PLATFORM_SCHEMA(discovery_info) @@ -89,10 +87,9 @@ def __init__(self, name, icon, self._optimistic = optimistic self._template = value_template - @asyncio.coroutine - def async_added_to_hass(self): + async def async_added_to_hass(self): """Subscribe to MQTT events.""" - yield from super().async_added_to_hass() + await super().async_added_to_hass() @callback def state_message_received(topic, payload, qos): @@ -111,12 +108,12 @@ def state_message_received(topic, payload, qos): # Force into optimistic mode. self._optimistic = True else: - yield from mqtt.async_subscribe( + await mqtt.async_subscribe( self.hass, self._state_topic, state_message_received, self._qos) if self._optimistic: - last_state = yield from async_get_last_state(self.hass, + last_state = await async_get_last_state(self.hass, self.entity_id) if last_state: self._state = last_state.state == STATE_ON @@ -146,8 +143,7 @@ def icon(self): """Return the icon.""" return self._icon - @asyncio.coroutine - def async_turn_on(self, **kwargs): + async def async_turn_on(self, **kwargs): """Turn the device on. This method is a coroutine. @@ -160,8 +156,7 @@ def async_turn_on(self, **kwargs): self._state = True self.async_schedule_update_ha_state() - @asyncio.coroutine - def async_turn_off(self, **kwargs): + async def async_turn_off(self, **kwargs): """Turn the device off. This method is a coroutine. From dea0bc6220793febc7b8bb2b137a5e32ee40ae3b Mon Sep 17 00:00:00 2001 From: Diogo Gomes Date: Wed, 2 May 2018 22:09:05 +0100 Subject: [PATCH 6/6] lint --- homeassistant/components/switch/mqtt.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/switch/mqtt.py b/homeassistant/components/switch/mqtt.py index 3a31f232c32f3b..69f12536c5f9a0 100644 --- a/homeassistant/components/switch/mqtt.py +++ b/homeassistant/components/switch/mqtt.py @@ -39,7 +39,8 @@ }).extend(mqtt.MQTT_AVAILABILITY_SCHEMA.schema) -async def async_setup_platform(hass, config, async_add_devices, discovery_info=None): +async def async_setup_platform(hass, config, async_add_devices, + discovery_info=None): """Set up the MQTT switch.""" if discovery_info is not None: config = PLATFORM_SCHEMA(discovery_info) @@ -114,7 +115,7 @@ def state_message_received(topic, payload, qos): if self._optimistic: last_state = await async_get_last_state(self.hass, - self.entity_id) + self.entity_id) if last_state: self._state = last_state.state == STATE_ON