From a64726e321f39dfe3320726f7c119248fb5d1492 Mon Sep 17 00:00:00 2001 From: William Scanlon Date: Tue, 15 Mar 2016 08:32:33 -0400 Subject: [PATCH] Added assumed state to command_line switch --- .../components/switch/command_line.py | 6 ++++++ tests/components/switch/test_command_line.py | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/homeassistant/components/switch/command_line.py b/homeassistant/components/switch/command_line.py index c9b3bb06f52..12b66bb6b7e 100644 --- a/homeassistant/components/switch/command_line.py +++ b/homeassistant/components/switch/command_line.py @@ -33,6 +33,7 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None): add_devices_callback(devices) +# pylint: disable=too-many-instance-attributes class CommandSwitch(SwitchDevice): """Representation a switch that can be toggled using shell commands.""" @@ -92,6 +93,11 @@ class CommandSwitch(SwitchDevice): """Return true if device is on.""" return self._state + @property + def assumed_state(self): + """Return true if we do optimistic updates.""" + return self._command_state is False + def _query_state(self): """Query for state.""" if not self._command_state: diff --git a/tests/components/switch/test_command_line.py b/tests/components/switch/test_command_line.py index eef058aa50d..f71fb9c25aa 100644 --- a/tests/components/switch/test_command_line.py +++ b/tests/components/switch/test_command_line.py @@ -6,6 +6,7 @@ import unittest from homeassistant.const import STATE_ON, STATE_OFF import homeassistant.components.switch as switch +import homeassistant.components.switch.command_line as command_line from tests.common import get_test_home_assistant @@ -155,3 +156,21 @@ class TestCommandSwitch(unittest.TestCase): state = self.hass.states.get('switch.test') self.assertEqual(STATE_ON, state.state) + + def test_assumed_state_should_be_true_if_command_state_is_false(self): + """Test with state value.""" + self.hass = get_test_home_assistant() + + # Set state command to false + statecmd = False + + no_state_device = command_line.CommandSwitch(self.hass, "Test", "echo", + "echo", statecmd, None) + self.assertTrue(no_state_device.assumed_state) + + # Set state command + statecmd = 'cat {}' + + state_device = command_line.CommandSwitch(self.hass, "Test", "echo", + "echo", statecmd, None) + self.assertFalse(state_device.assumed_state)