From 6809a881faf0cf2a4e2036805a1327ff39b48d29 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sat, 28 Nov 2015 01:02:35 -0800 Subject: [PATCH] Tweak MQTT Motor component --- homeassistant/components/motor/__init__.py | 20 ++++++--- homeassistant/components/motor/mqtt.py | 16 -------- tests/components/motor/test_mqtt.py | 48 +++++++++++++++++++++- 3 files changed, 60 insertions(+), 24 deletions(-) diff --git a/homeassistant/components/motor/__init__.py b/homeassistant/components/motor/__init__.py index feea836a0dd..806dd9e0eb1 100644 --- a/homeassistant/components/motor/__init__.py +++ b/homeassistant/components/motor/__init__.py @@ -17,7 +17,6 @@ from homeassistant.const import ( DOMAIN = 'motor' -DEPENDENCIES = [] SCAN_INTERVAL = 15 GROUP_NAME_ALL_MOTORS = 'all motors' @@ -30,6 +29,8 @@ DISCOVERY_PLATFORMS = {} _LOGGER = logging.getLogger(__name__) +ATTR_CURRENT_POSITION = 'current_position' + def is_open(hass, entity_id=None): """ Returns if the motor is open based on the statemachine. """ @@ -114,17 +115,24 @@ class MotorDevice(Entity): @property def state_attributes(self): - """ Returns optional state attributes. """ - return None + """ Return the state attributes. """ + current = self.current_position + + if current is None: + return None + + return { + ATTR_CURRENT_POSITION: current + } def open(self, **kwargs): - """ Open the device. """ + """ Open the motor. """ raise NotImplementedError() def close(self, **kwargs): - """ Close the device. """ + """ Close the motor. """ raise NotImplementedError() def stop(self, **kwargs): - """ Stop the device. """ + """ Stop the motor. """ raise NotImplementedError() diff --git a/homeassistant/components/motor/mqtt.py b/homeassistant/components/motor/mqtt.py index 2aac5e37c16..1075030fc37 100644 --- a/homeassistant/components/motor/mqtt.py +++ b/homeassistant/components/motor/mqtt.py @@ -18,10 +18,7 @@ DEFAULT_PAYLOAD_OPEN = "OPEN" DEFAULT_PAYLOAD_CLOSE = "CLOSE" DEFAULT_PAYLOAD_STOP = "STOP" -ATTR_CURRENT_POSITION = 'current_position' - -# pylint: disable=unused-argument def setup_platform(hass, config, add_devices_callback, discovery_info=None): """ Add MQTT Motor """ @@ -88,11 +85,6 @@ class MqttMotor(MotorDevice): None is unknown, 0 is closed, 100 is fully open. """ return self._state - @property - def is_open(self): - """ True if device is current position is not zero. """ - return self._state > 0 - def open(self, **kwargs): """ Open the device. """ mqtt.publish(self.hass, self._command_topic, self._payload_open, @@ -107,11 +99,3 @@ class MqttMotor(MotorDevice): """ Stop the device. """ mqtt.publish(self.hass, self._command_topic, self._payload_stop, self._qos) - - @property - def state_attributes(self): - """ Return the state attributes. """ - state_attr = {} - if self._state is not None: - state_attr[ATTR_CURRENT_POSITION] = self._state - return state_attr diff --git a/tests/components/motor/test_mqtt.py b/tests/components/motor/test_mqtt.py index 4d7af04ae6c..1638e830f2d 100644 --- a/tests/components/motor/test_mqtt.py +++ b/tests/components/motor/test_mqtt.py @@ -58,7 +58,7 @@ class TestMotorMQTT(unittest.TestCase): state = self.hass.states.get('motor.test') self.assertEqual(STATE_OPEN, state.state) - def test_sending_mqtt_commands(self): + def test_send_open_command(self): self.assertTrue(motor.setup(self.hass, { 'motor': { 'platform': 'mqtt', @@ -75,7 +75,51 @@ class TestMotorMQTT(unittest.TestCase): motor.call_open(self.hass, 'motor.test') self.hass.pool.block_till_done() - self.assertEqual(('command-topic', 'OPEN', 2), + self.assertEqual(('command-topic', 'OPEN', 2, False), + self.mock_publish.mock_calls[-1][1]) + state = self.hass.states.get('motor.test') + self.assertEqual(STATE_UNKNOWN, state.state) + + def test_send_close_command(self): + self.assertTrue(motor.setup(self.hass, { + 'motor': { + 'platform': 'mqtt', + 'name': 'test', + 'state_topic': 'state-topic', + 'command_topic': 'command-topic', + 'qos': 2 + } + })) + + state = self.hass.states.get('motor.test') + self.assertEqual(STATE_UNKNOWN, state.state) + + motor.call_close(self.hass, 'motor.test') + self.hass.pool.block_till_done() + + self.assertEqual(('command-topic', 'CLOSE', 2, False), + self.mock_publish.mock_calls[-1][1]) + state = self.hass.states.get('motor.test') + self.assertEqual(STATE_UNKNOWN, state.state) + + def test_send_stop_command(self): + self.assertTrue(motor.setup(self.hass, { + 'motor': { + 'platform': 'mqtt', + 'name': 'test', + 'state_topic': 'state-topic', + 'command_topic': 'command-topic', + 'qos': 2 + } + })) + + state = self.hass.states.get('motor.test') + self.assertEqual(STATE_UNKNOWN, state.state) + + motor.call_stop(self.hass, 'motor.test') + self.hass.pool.block_till_done() + + self.assertEqual(('command-topic', 'STOP', 2, False), self.mock_publish.mock_calls[-1][1]) state = self.hass.states.get('motor.test') self.assertEqual(STATE_UNKNOWN, state.state)