From 0dab5e69dbb2ce0e511f54e7de91dde8f6f1e842 Mon Sep 17 00:00:00 2001 From: Jan Bouwhuis Date: Mon, 3 Jan 2022 15:13:30 +0100 Subject: [PATCH] Fix MQTT cover not using tilt_command_template (#63080) --- homeassistant/components/mqtt/cover.py | 26 ++++++++++- tests/components/mqtt/test_cover.py | 60 ++++++++++++++++++++++---- 2 files changed, 76 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/mqtt/cover.py b/homeassistant/components/mqtt/cover.py index 2ba06567e850a0..014c21d384eba8 100644 --- a/homeassistant/components/mqtt/cover.py +++ b/homeassistant/components/mqtt/cover.py @@ -603,10 +603,20 @@ async def async_stop_cover(self, **kwargs): async def async_open_cover_tilt(self, **kwargs): """Tilt the cover open.""" + tilt_open_position = self._config[CONF_TILT_OPEN_POSITION] + variables = { + "tilt_position": tilt_open_position, + "entity_id": self.entity_id, + "position_open": self._config.get(CONF_POSITION_OPEN), + "position_closed": self._config.get(CONF_POSITION_CLOSED), + "tilt_min": self._config.get(CONF_TILT_MIN), + "tilt_max": self._config.get(CONF_TILT_MAX), + } + tilt_payload = self._set_tilt_template(tilt_open_position, variables=variables) await mqtt.async_publish( self.hass, self._config.get(CONF_TILT_COMMAND_TOPIC), - self._config[CONF_TILT_OPEN_POSITION], + tilt_payload, self._config[CONF_QOS], self._config[CONF_RETAIN], self._config[CONF_ENCODING], @@ -619,10 +629,22 @@ async def async_open_cover_tilt(self, **kwargs): async def async_close_cover_tilt(self, **kwargs): """Tilt the cover closed.""" + tilt_closed_position = self._config[CONF_TILT_CLOSED_POSITION] + variables = { + "tilt_position": tilt_closed_position, + "entity_id": self.entity_id, + "position_open": self._config.get(CONF_POSITION_OPEN), + "position_closed": self._config.get(CONF_POSITION_CLOSED), + "tilt_min": self._config.get(CONF_TILT_MIN), + "tilt_max": self._config.get(CONF_TILT_MAX), + } + tilt_payload = self._set_tilt_template( + tilt_closed_position, variables=variables + ) await mqtt.async_publish( self.hass, self._config.get(CONF_TILT_COMMAND_TOPIC), - self._config[CONF_TILT_CLOSED_POSITION], + tilt_payload, self._config[CONF_QOS], self._config[CONF_RETAIN], self._config[CONF_ENCODING], diff --git a/tests/components/mqtt/test_cover.py b/tests/components/mqtt/test_cover.py index 6fcda6717936e7..5505c1ae5da04e 100644 --- a/tests/components/mqtt/test_cover.py +++ b/tests/components/mqtt/test_cover.py @@ -934,12 +934,11 @@ async def test_set_tilt_templated_and_attributes(hass, mqtt_mock): "position_closed": 0, "set_position_topic": "set-position-topic", "set_position_template": "{{position-1}}", - "tilt_command_template": '\ - {% if state_attr(entity_id, "friendly_name") != "test" %}\ - {{ 5 }}\ - {% else %}\ - {{ 23 }}\ - {% endif %}', + "tilt_command_template": "{" + '"enitity_id": "{{ entity_id }}",' + '"value": {{ value }},' + '"tilt_position": {{ tilt_position }}' + "}", "payload_open": "OPEN", "payload_close": "CLOSE", "payload_stop": "STOP", @@ -951,12 +950,57 @@ async def test_set_tilt_templated_and_attributes(hass, mqtt_mock): await hass.services.async_call( cover.DOMAIN, SERVICE_SET_COVER_TILT_POSITION, - {ATTR_ENTITY_ID: "cover.test", ATTR_TILT_POSITION: 99}, + {ATTR_ENTITY_ID: "cover.test", ATTR_TILT_POSITION: 45}, + blocking=True, + ) + + mqtt_mock.async_publish.assert_called_once_with( + "tilt-command-topic", + '{"enitity_id": "cover.test","value": 45,"tilt_position": 45}', + 0, + False, + ) + mqtt_mock.async_publish.reset_mock() + + await hass.services.async_call( + cover.DOMAIN, + SERVICE_OPEN_COVER_TILT, + {ATTR_ENTITY_ID: "cover.test"}, + blocking=True, + ) + mqtt_mock.async_publish.assert_called_once_with( + "tilt-command-topic", + '{"enitity_id": "cover.test","value": 100,"tilt_position": 100}', + 0, + False, + ) + mqtt_mock.async_publish.reset_mock() + + await hass.services.async_call( + cover.DOMAIN, + SERVICE_CLOSE_COVER_TILT, + {ATTR_ENTITY_ID: "cover.test"}, blocking=True, ) + mqtt_mock.async_publish.assert_called_once_with( + "tilt-command-topic", + '{"enitity_id": "cover.test","value": 0,"tilt_position": 0}', + 0, + False, + ) + mqtt_mock.async_publish.reset_mock() + await hass.services.async_call( + cover.DOMAIN, + SERVICE_TOGGLE_COVER_TILT, + {ATTR_ENTITY_ID: "cover.test"}, + blocking=True, + ) mqtt_mock.async_publish.assert_called_once_with( - "tilt-command-topic", "23", 0, False + "tilt-command-topic", + '{"enitity_id": "cover.test","value": 100,"tilt_position": 100}', + 0, + False, )