Tweak MQTT Motor component
parent
9f01d7abca
commit
6809a881fa
|
@ -17,7 +17,6 @@ from homeassistant.const import (
|
||||||
|
|
||||||
|
|
||||||
DOMAIN = 'motor'
|
DOMAIN = 'motor'
|
||||||
DEPENDENCIES = []
|
|
||||||
SCAN_INTERVAL = 15
|
SCAN_INTERVAL = 15
|
||||||
|
|
||||||
GROUP_NAME_ALL_MOTORS = 'all motors'
|
GROUP_NAME_ALL_MOTORS = 'all motors'
|
||||||
|
@ -30,6 +29,8 @@ DISCOVERY_PLATFORMS = {}
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
ATTR_CURRENT_POSITION = 'current_position'
|
||||||
|
|
||||||
|
|
||||||
def is_open(hass, entity_id=None):
|
def is_open(hass, entity_id=None):
|
||||||
""" Returns if the motor is open based on the statemachine. """
|
""" Returns if the motor is open based on the statemachine. """
|
||||||
|
@ -114,17 +115,24 @@ class MotorDevice(Entity):
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state_attributes(self):
|
def state_attributes(self):
|
||||||
""" Returns optional state attributes. """
|
""" Return the state attributes. """
|
||||||
return None
|
current = self.current_position
|
||||||
|
|
||||||
|
if current is None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
return {
|
||||||
|
ATTR_CURRENT_POSITION: current
|
||||||
|
}
|
||||||
|
|
||||||
def open(self, **kwargs):
|
def open(self, **kwargs):
|
||||||
""" Open the device. """
|
""" Open the motor. """
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
def close(self, **kwargs):
|
def close(self, **kwargs):
|
||||||
""" Close the device. """
|
""" Close the motor. """
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
def stop(self, **kwargs):
|
def stop(self, **kwargs):
|
||||||
""" Stop the device. """
|
""" Stop the motor. """
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
|
@ -18,10 +18,7 @@ DEFAULT_PAYLOAD_OPEN = "OPEN"
|
||||||
DEFAULT_PAYLOAD_CLOSE = "CLOSE"
|
DEFAULT_PAYLOAD_CLOSE = "CLOSE"
|
||||||
DEFAULT_PAYLOAD_STOP = "STOP"
|
DEFAULT_PAYLOAD_STOP = "STOP"
|
||||||
|
|
||||||
ATTR_CURRENT_POSITION = 'current_position'
|
|
||||||
|
|
||||||
|
|
||||||
# pylint: disable=unused-argument
|
|
||||||
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
||||||
""" Add MQTT Motor """
|
""" Add MQTT Motor """
|
||||||
|
|
||||||
|
@ -88,11 +85,6 @@ class MqttMotor(MotorDevice):
|
||||||
None is unknown, 0 is closed, 100 is fully open. """
|
None is unknown, 0 is closed, 100 is fully open. """
|
||||||
return self._state
|
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):
|
def open(self, **kwargs):
|
||||||
""" Open the device. """
|
""" Open the device. """
|
||||||
mqtt.publish(self.hass, self._command_topic, self._payload_open,
|
mqtt.publish(self.hass, self._command_topic, self._payload_open,
|
||||||
|
@ -107,11 +99,3 @@ class MqttMotor(MotorDevice):
|
||||||
""" Stop the device. """
|
""" Stop the device. """
|
||||||
mqtt.publish(self.hass, self._command_topic, self._payload_stop,
|
mqtt.publish(self.hass, self._command_topic, self._payload_stop,
|
||||||
self._qos)
|
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
|
|
||||||
|
|
|
@ -58,7 +58,7 @@ class TestMotorMQTT(unittest.TestCase):
|
||||||
state = self.hass.states.get('motor.test')
|
state = self.hass.states.get('motor.test')
|
||||||
self.assertEqual(STATE_OPEN, state.state)
|
self.assertEqual(STATE_OPEN, state.state)
|
||||||
|
|
||||||
def test_sending_mqtt_commands(self):
|
def test_send_open_command(self):
|
||||||
self.assertTrue(motor.setup(self.hass, {
|
self.assertTrue(motor.setup(self.hass, {
|
||||||
'motor': {
|
'motor': {
|
||||||
'platform': 'mqtt',
|
'platform': 'mqtt',
|
||||||
|
@ -75,7 +75,51 @@ class TestMotorMQTT(unittest.TestCase):
|
||||||
motor.call_open(self.hass, 'motor.test')
|
motor.call_open(self.hass, 'motor.test')
|
||||||
self.hass.pool.block_till_done()
|
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])
|
self.mock_publish.mock_calls[-1][1])
|
||||||
state = self.hass.states.get('motor.test')
|
state = self.hass.states.get('motor.test')
|
||||||
self.assertEqual(STATE_UNKNOWN, state.state)
|
self.assertEqual(STATE_UNKNOWN, state.state)
|
||||||
|
|
Loading…
Reference in New Issue