diff --git a/homeassistant/components/cover/mqtt.py b/homeassistant/components/cover/mqtt.py index 575dc4cb345..2115434232c 100644 --- a/homeassistant/components/cover/mqtt.py +++ b/homeassistant/components/cover/mqtt.py @@ -11,23 +11,35 @@ import voluptuous as vol from homeassistant.core import callback import homeassistant.components.mqtt as mqtt -from homeassistant.components.cover import CoverDevice +from homeassistant.components.cover import ( + CoverDevice, ATTR_TILT_POSITION, SUPPORT_OPEN_TILT, + SUPPORT_CLOSE_TILT, SUPPORT_STOP_TILT, SUPPORT_SET_TILT_POSITION, + SUPPORT_OPEN, SUPPORT_CLOSE, SUPPORT_STOP, SUPPORT_SET_POSITION) from homeassistant.const import ( CONF_NAME, CONF_VALUE_TEMPLATE, CONF_OPTIMISTIC, STATE_OPEN, - STATE_CLOSED) + STATE_CLOSED, STATE_UNKNOWN) from homeassistant.components.mqtt import ( - CONF_STATE_TOPIC, CONF_COMMAND_TOPIC, CONF_QOS, CONF_RETAIN) + CONF_STATE_TOPIC, CONF_COMMAND_TOPIC, CONF_QOS, CONF_RETAIN, + valid_publish_topic, valid_subscribe_topic) import homeassistant.helpers.config_validation as cv _LOGGER = logging.getLogger(__name__) DEPENDENCIES = ['mqtt'] +CONF_TILT_COMMAND_TOPIC = 'tilt_command_topic' +CONF_TILT_STATUS_TOPIC = 'tilt_status_topic' + CONF_PAYLOAD_OPEN = 'payload_open' CONF_PAYLOAD_CLOSE = 'payload_close' CONF_PAYLOAD_STOP = 'payload_stop' CONF_STATE_OPEN = 'state_open' CONF_STATE_CLOSED = 'state_closed' +CONF_TILT_CLOSED_POSITION = 'tilt_closed_value' +CONF_TILT_OPEN_POSITION = 'tilt_opened_value' +CONF_TILT_MIN = 'tilt_min' +CONF_TILT_MAX = 'tilt_max' +CONF_TILT_STATE_OPTIMISTIC = 'tilt_optimistic' DEFAULT_NAME = 'MQTT Cover' DEFAULT_PAYLOAD_OPEN = 'OPEN' @@ -35,6 +47,14 @@ DEFAULT_PAYLOAD_CLOSE = 'CLOSE' DEFAULT_PAYLOAD_STOP = 'STOP' DEFAULT_OPTIMISTIC = False DEFAULT_RETAIN = False +DEFAULT_TILT_CLOSED_POSITION = 0 +DEFAULT_TILT_OPEN_POSITION = 100 +DEFAULT_TILT_MIN = 0 +DEFAULT_TILT_MAX = 100 +DEFAULT_TILT_OPTIMISTIC = False + +TILT_FEATURES = (SUPPORT_OPEN_TILT | SUPPORT_CLOSE_TILT | SUPPORT_STOP_TILT | + SUPPORT_SET_TILT_POSITION) PLATFORM_SCHEMA = mqtt.MQTT_RW_PLATFORM_SCHEMA.extend({ vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, @@ -44,6 +64,18 @@ PLATFORM_SCHEMA = mqtt.MQTT_RW_PLATFORM_SCHEMA.extend({ vol.Optional(CONF_STATE_OPEN, default=STATE_OPEN): cv.string, vol.Optional(CONF_STATE_CLOSED, default=STATE_CLOSED): cv.string, vol.Optional(CONF_OPTIMISTIC, default=DEFAULT_OPTIMISTIC): cv.boolean, + vol.Optional(CONF_TILT_COMMAND_TOPIC, default=None): valid_publish_topic, + vol.Optional(CONF_TILT_STATUS_TOPIC, default=None): valid_subscribe_topic, + vol.Optional(CONF_TILT_CLOSED_POSITION, + default=DEFAULT_TILT_CLOSED_POSITION): int, + vol.Optional(CONF_TILT_OPEN_POSITION, + default=DEFAULT_TILT_OPEN_POSITION): int, + vol.Optional(CONF_TILT_MIN, + default=DEFAULT_TILT_MIN): int, + vol.Optional(CONF_TILT_MAX, + default=DEFAULT_TILT_MAX): int, + vol.Optional(CONF_TILT_STATE_OPTIMISTIC, + default=DEFAULT_TILT_OPTIMISTIC): cv.boolean, }) @@ -58,6 +90,8 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None): config.get(CONF_NAME), config.get(CONF_STATE_TOPIC), config.get(CONF_COMMAND_TOPIC), + config.get(CONF_TILT_COMMAND_TOPIC), + config.get(CONF_TILT_STATUS_TOPIC), config.get(CONF_QOS), config.get(CONF_RETAIN), config.get(CONF_STATE_OPEN), @@ -67,21 +101,30 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None): config.get(CONF_PAYLOAD_STOP), config.get(CONF_OPTIMISTIC), value_template, + config.get(CONF_TILT_OPEN_POSITION), + config.get(CONF_TILT_CLOSED_POSITION), + config.get(CONF_TILT_MIN), + config.get(CONF_TILT_MAX), + config.get(CONF_TILT_STATE_OPTIMISTIC), )]) class MqttCover(CoverDevice): """Representation of a cover that can be controlled using MQTT.""" - def __init__(self, name, state_topic, command_topic, qos, retain, - state_open, state_closed, payload_open, payload_close, - payload_stop, optimistic, value_template): + def __init__(self, name, state_topic, command_topic, tilt_command_topic, + tilt_status_topic, qos, retain, state_open, state_closed, + payload_open, payload_close, payload_stop, + optimistic, value_template, tilt_open_position, + tilt_closed_position, tilt_min, tilt_max, tilt_optimistic): """Initialize the cover.""" self._position = None self._state = None self._name = name self._state_topic = state_topic self._command_topic = command_topic + self._tilt_command_topic = tilt_command_topic + self._tilt_status_topic = tilt_status_topic self._qos = qos self._payload_open = payload_open self._payload_close = payload_close @@ -89,8 +132,14 @@ class MqttCover(CoverDevice): self._state_open = state_open self._state_closed = state_closed self._retain = retain + self._tilt_open_position = tilt_open_position + self._tilt_closed_position = tilt_closed_position self._optimistic = optimistic or state_topic is None self._template = value_template + self._tilt_value = None + self._tilt_min = tilt_min + self._tilt_max = tilt_max + self._tilt_optimistic = tilt_optimistic @asyncio.coroutine def async_added_to_hass(self): @@ -98,6 +147,16 @@ class MqttCover(CoverDevice): This method is a coroutine. """ + @callback + def tilt_updated(topic, payload, qos): + """The tilt was updated.""" + if (payload.isnumeric() and + self._tilt_min <= int(payload) <= self._tilt_max): + tilt_range = self._tilt_max - self._tilt_min + level = round(float(payload) / tilt_range * 100.0) + self._tilt_value = level + self.hass.async_add_job(self.async_update_ha_state()) + @callback def message_received(topic, payload, qos): """Handle new MQTT message.""" @@ -130,6 +189,14 @@ class MqttCover(CoverDevice): yield from mqtt.async_subscribe( self.hass, self._state_topic, message_received, self._qos) + if self._tilt_status_topic is None: + self._tilt_optimistic = True + else: + self._tilt_optimistic = False + self._tilt_value = STATE_UNKNOWN + yield from mqtt.async_subscribe( + self.hass, self._tilt_status_topic, tilt_updated, self._qos) + @property def should_poll(self): """No polling needed.""" @@ -153,6 +220,24 @@ class MqttCover(CoverDevice): """ return self._position + @property + def current_cover_tilt_position(self): + """Return current position of cover tilt.""" + return self._tilt_value + + @property + def supported_features(self): + """Flag supported features.""" + supported_features = SUPPORT_OPEN | SUPPORT_CLOSE | SUPPORT_STOP + + if self.current_cover_position is not None: + supported_features |= SUPPORT_SET_POSITION + + if self._tilt_command_topic is not None: + supported_features |= TILT_FEATURES + + return supported_features + @asyncio.coroutine def async_open_cover(self, **kwargs): """Move the cover up. @@ -190,3 +275,37 @@ class MqttCover(CoverDevice): mqtt.async_publish( self.hass, self._command_topic, self._payload_stop, self._qos, self._retain) + + @asyncio.coroutine + def async_open_cover_tilt(self, **kwargs): + """Tilt the cover open.""" + mqtt.async_publish(self.hass, self._tilt_command_topic, + self._tilt_open_position, self._qos, self._retain) + if self._tilt_optimistic: + self._tilt_value = self._tilt_open_position + self.hass.async_add_job(self.async_update_ha_state()) + + @asyncio.coroutine + def async_close_cover_tilt(self, **kwargs): + """Tilt the cover closed.""" + mqtt.async_publish(self.hass, self._tilt_command_topic, + self._tilt_closed_position, self._qos, self._retain) + if self._tilt_optimistic: + self._tilt_value = self._tilt_closed_position + self.hass.async_add_job(self.async_update_ha_state()) + + @asyncio.coroutine + def async_set_cover_tilt_position(self, **kwargs): + """Move the cover tilt to a specific position.""" + if ATTR_TILT_POSITION not in kwargs: + return + + position = float(kwargs[ATTR_TILT_POSITION]) + + # The position needs to be between min and max + tilt_range = self._tilt_max - self._tilt_min + percentage = position / 100.0 + level = round(tilt_range * percentage) + + mqtt.async_publish(self.hass, self._tilt_command_topic, + level, self._qos, self._retain) diff --git a/tests/components/cover/test_mqtt.py b/tests/components/cover/test_mqtt.py index 5cd79fdb74c..b2dcf8e175d 100644 --- a/tests/components/cover/test_mqtt.py +++ b/tests/components/cover/test_mqtt.py @@ -213,6 +213,7 @@ class TestCoverMQTT(unittest.TestCase): state_attributes_dict = self.hass.states.get( 'cover.test').attributes self.assertFalse('current_position' in state_attributes_dict) + self.assertFalse('current_tilt_position' in state_attributes_dict) fire_mqtt_message(self.hass, 'state-topic', '0') self.hass.block_till_done() @@ -237,3 +238,215 @@ class TestCoverMQTT(unittest.TestCase): current_cover_position = self.hass.states.get( 'cover.test').attributes['current_position'] self.assertEqual(50, current_cover_position) + + def test_tilt_defaults(self): + """Test the defaults.""" + self.assertTrue(setup_component(self.hass, cover.DOMAIN, { + cover.DOMAIN: { + 'platform': 'mqtt', + 'name': 'test', + 'state_topic': 'state-topic', + 'command_topic': 'command-topic', + 'qos': 0, + 'payload_open': 'OPEN', + 'payload_close': 'CLOSE', + 'payload_stop': 'STOP', + 'tilt_command_topic': 'tilt-command', + 'tilt_status_topic': 'tilt-status' + } + })) + + state_attributes_dict = self.hass.states.get( + 'cover.test').attributes + self.assertTrue('current_tilt_position' in state_attributes_dict) + + current_cover_position = self.hass.states.get( + 'cover.test').attributes['current_tilt_position'] + self.assertEqual(STATE_UNKNOWN, current_cover_position) + + def test_tilt_via_invocation_defaults(self): + """Test tilt defaults on close/open.""" + self.assertTrue(setup_component(self.hass, cover.DOMAIN, { + cover.DOMAIN: { + 'platform': 'mqtt', + 'name': 'test', + 'state_topic': 'state-topic', + 'command_topic': 'command-topic', + 'qos': 0, + 'payload_open': 'OPEN', + 'payload_close': 'CLOSE', + 'payload_stop': 'STOP', + 'tilt_command_topic': 'tilt-command-topic', + 'tilt_status_topic': 'tilt-status-topic' + } + })) + + cover.open_cover_tilt(self.hass, 'cover.test') + self.hass.block_till_done() + + self.assertEqual(('tilt-command-topic', 100, 0, False), + self.mock_publish.mock_calls[-2][1]) + + cover.close_cover_tilt(self.hass, 'cover.test') + self.hass.block_till_done() + + self.assertEqual(('tilt-command-topic', 0, 0, False), + self.mock_publish.mock_calls[-2][1]) + + def test_tilt_given_value(self): + """Test tilting to a given value.""" + self.assertTrue(setup_component(self.hass, cover.DOMAIN, { + cover.DOMAIN: { + 'platform': 'mqtt', + 'name': 'test', + 'state_topic': 'state-topic', + 'command_topic': 'command-topic', + 'qos': 0, + 'payload_open': 'OPEN', + 'payload_close': 'CLOSE', + 'payload_stop': 'STOP', + 'tilt_command_topic': 'tilt-command-topic', + 'tilt_status_topic': 'tilt-status-topic', + 'tilt_opened_value': 400, + 'tilt_closed_value': 125 + } + })) + + cover.open_cover_tilt(self.hass, 'cover.test') + self.hass.block_till_done() + + self.assertEqual(('tilt-command-topic', 400, 0, False), + self.mock_publish.mock_calls[-2][1]) + + cover.close_cover_tilt(self.hass, 'cover.test') + self.hass.block_till_done() + + self.assertEqual(('tilt-command-topic', 125, 0, False), + self.mock_publish.mock_calls[-2][1]) + + def test_tilt_via_topic(self): + """Test tilt by updating status via MQTT.""" + self.assertTrue(setup_component(self.hass, cover.DOMAIN, { + cover.DOMAIN: { + 'platform': 'mqtt', + 'name': 'test', + 'state_topic': 'state-topic', + 'command_topic': 'command-topic', + 'qos': 0, + 'payload_open': 'OPEN', + 'payload_close': 'CLOSE', + 'payload_stop': 'STOP', + 'tilt_command_topic': 'tilt-command-topic', + 'tilt_status_topic': 'tilt-status-topic', + 'tilt_opened_value': 400, + 'tilt_closed_value': 125 + } + })) + + fire_mqtt_message(self.hass, 'tilt-status-topic', '0') + self.hass.block_till_done() + + current_cover_tilt_position = self.hass.states.get( + 'cover.test').attributes['current_tilt_position'] + self.assertEqual(0, current_cover_tilt_position) + + fire_mqtt_message(self.hass, 'tilt-status-topic', '50') + self.hass.block_till_done() + + current_cover_tilt_position = self.hass.states.get( + 'cover.test').attributes['current_tilt_position'] + self.assertEqual(50, current_cover_tilt_position) + + def test_tilt_via_topic_altered_range(self): + """Test tilt status via MQTT with altered tilt range.""" + self.assertTrue(setup_component(self.hass, cover.DOMAIN, { + cover.DOMAIN: { + 'platform': 'mqtt', + 'name': 'test', + 'state_topic': 'state-topic', + 'command_topic': 'command-topic', + 'qos': 0, + 'payload_open': 'OPEN', + 'payload_close': 'CLOSE', + 'payload_stop': 'STOP', + 'tilt_command_topic': 'tilt-command-topic', + 'tilt_status_topic': 'tilt-status-topic', + 'tilt_opened_value': 400, + 'tilt_closed_value': 125, + 'tilt_min': 0, + 'tilt_max': 50 + } + })) + + fire_mqtt_message(self.hass, 'tilt-status-topic', '0') + self.hass.block_till_done() + + current_cover_tilt_position = self.hass.states.get( + 'cover.test').attributes['current_tilt_position'] + self.assertEqual(0, current_cover_tilt_position) + + fire_mqtt_message(self.hass, 'tilt-status-topic', '50') + self.hass.block_till_done() + + current_cover_tilt_position = self.hass.states.get( + 'cover.test').attributes['current_tilt_position'] + self.assertEqual(100, current_cover_tilt_position) + + fire_mqtt_message(self.hass, 'tilt-status-topic', '25') + self.hass.block_till_done() + + current_cover_tilt_position = self.hass.states.get( + 'cover.test').attributes['current_tilt_position'] + self.assertEqual(50, current_cover_tilt_position) + + def test_tilt_position(self): + """Test tilt via method invocation.""" + self.assertTrue(setup_component(self.hass, cover.DOMAIN, { + cover.DOMAIN: { + 'platform': 'mqtt', + 'name': 'test', + 'state_topic': 'state-topic', + 'command_topic': 'command-topic', + 'qos': 0, + 'payload_open': 'OPEN', + 'payload_close': 'CLOSE', + 'payload_stop': 'STOP', + 'tilt_command_topic': 'tilt-command-topic', + 'tilt_status_topic': 'tilt-status-topic', + 'tilt_opened_value': 400, + 'tilt_closed_value': 125 + } + })) + + cover.set_cover_tilt_position(self.hass, 50, 'cover.test') + self.hass.block_till_done() + + self.assertEqual(('tilt-command-topic', 50, 0, False), + self.mock_publish.mock_calls[-2][1]) + + def test_tilt_position_altered_range(self): + """Test tilt via method invocation with altered range.""" + self.assertTrue(setup_component(self.hass, cover.DOMAIN, { + cover.DOMAIN: { + 'platform': 'mqtt', + 'name': 'test', + 'state_topic': 'state-topic', + 'command_topic': 'command-topic', + 'qos': 0, + 'payload_open': 'OPEN', + 'payload_close': 'CLOSE', + 'payload_stop': 'STOP', + 'tilt_command_topic': 'tilt-command-topic', + 'tilt_status_topic': 'tilt-status-topic', + 'tilt_opened_value': 400, + 'tilt_closed_value': 125, + 'tilt_min': 0, + 'tilt_max': 50 + } + })) + + cover.set_cover_tilt_position(self.hass, 50, 'cover.test') + self.hass.block_till_done() + + self.assertEqual(('tilt-command-topic', 25, 0, False), + self.mock_publish.mock_calls[-2][1])