Tweak MQTT Motor component

pull/656/merge
Paulus Schoutsen 2015-11-28 01:02:35 -08:00
parent 9f01d7abca
commit 6809a881fa
3 changed files with 60 additions and 24 deletions

View File

@ -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()

View File

@ -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

View File

@ -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)