From 443b39bccdbda059bda71b0f17d8abd555f8dc48 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sat, 20 Feb 2016 17:17:30 -0800 Subject: [PATCH] MQTT Switch to expose assumed_state if optimistic --- homeassistant/components/switch/mqtt.py | 5 +++++ tests/components/switch/test_mqtt.py | 4 +++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/switch/mqtt.py b/homeassistant/components/switch/mqtt.py index 01360b02bec..6b8089daed3 100644 --- a/homeassistant/components/switch/mqtt.py +++ b/homeassistant/components/switch/mqtt.py @@ -97,6 +97,11 @@ class MqttSwitch(SwitchDevice): """ True if device is on. """ return self._state + @property + def assumed_state(self): + """Return True if we do optimistic updates.""" + return self._optimistic + def turn_on(self, **kwargs): """ Turn the device on. """ mqtt.publish(self.hass, self._command_topic, self._payload_on, diff --git a/tests/components/switch/test_mqtt.py b/tests/components/switch/test_mqtt.py index d412b5293a0..17bc65c7e5a 100644 --- a/tests/components/switch/test_mqtt.py +++ b/tests/components/switch/test_mqtt.py @@ -6,7 +6,7 @@ Tests MQTT switch. """ import unittest -from homeassistant.const import STATE_ON, STATE_OFF +from homeassistant.const import STATE_ON, STATE_OFF, ATTR_ASSUMED_STATE import homeassistant.components.switch as switch from tests.common import ( mock_mqtt_component, fire_mqtt_message, get_test_home_assistant) @@ -37,6 +37,7 @@ class TestSensorMQTT(unittest.TestCase): state = self.hass.states.get('switch.test') self.assertEqual(STATE_OFF, state.state) + self.assertIsNone(state.attributes.get(ATTR_ASSUMED_STATE)) fire_mqtt_message(self.hass, 'state-topic', 'beer on') self.hass.pool.block_till_done() @@ -64,6 +65,7 @@ class TestSensorMQTT(unittest.TestCase): state = self.hass.states.get('switch.test') self.assertEqual(STATE_OFF, state.state) + self.assertTrue(state.attributes.get(ATTR_ASSUMED_STATE)) switch.turn_on(self.hass, 'switch.test') self.hass.pool.block_till_done()