Skip to content

Commit

Permalink
Fix MQTT cover not using tilt_command_template (#63080)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbouwh committed Jan 3, 2022
1 parent 1ce75f8 commit 0dab5e6
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 10 deletions.
26 changes: 24 additions & 2 deletions homeassistant/components/mqtt/cover.py
Expand Up @@ -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],
Expand All @@ -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],
Expand Down
60 changes: 52 additions & 8 deletions tests/components/mqtt/test_cover.py
Expand Up @@ -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",
Expand All @@ -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,
)


Expand Down

0 comments on commit 0dab5e6

Please sign in to comment.