diff --git a/homeassistant/components/mqtt/cover.py b/homeassistant/components/mqtt/cover.py index 54ef6cfb539..7b7f983b1e4 100644 --- a/homeassistant/components/mqtt/cover.py +++ b/homeassistant/components/mqtt/cover.py @@ -129,14 +129,14 @@ def validate_options(value): ): _LOGGER.warning( "using 'value_template' for 'position_topic' is deprecated " - "and will be removed from Home Assistant in version 2021.6" + "and will be removed from Home Assistant in version 2021.6, " "please replace it with 'position_template'" ) if CONF_TILT_INVERT_STATE in value: _LOGGER.warning( "'tilt_invert_state' is deprecated " - "and will be removed from Home Assistant in version 2021.6" + "and will be removed from Home Assistant in version 2021.6, " "please invert tilt using 'tilt_min' & 'tilt_max'" ) @@ -172,9 +172,7 @@ PLATFORM_SCHEMA = vol.All( CONF_TILT_CLOSED_POSITION, default=DEFAULT_TILT_CLOSED_POSITION ): int, vol.Optional(CONF_TILT_COMMAND_TOPIC): mqtt.valid_publish_topic, - vol.Optional( - CONF_TILT_INVERT_STATE, default=DEFAULT_TILT_INVERT_STATE - ): cv.boolean, + vol.Optional(CONF_TILT_INVERT_STATE): cv.boolean, vol.Optional(CONF_TILT_MAX, default=DEFAULT_TILT_MAX): int, vol.Optional(CONF_TILT_MIN, default=DEFAULT_TILT_MIN): int, vol.Optional( @@ -247,15 +245,22 @@ class MqttCover(MqttEntity, CoverEntity): ) self._tilt_optimistic = config[CONF_TILT_STATE_OPTIMISTIC] - template = self._config.get(CONF_VALUE_TEMPLATE) - if template is not None: - template.hass = self.hass + value_template = self._config.get(CONF_VALUE_TEMPLATE) + if value_template is not None: + value_template.hass = self.hass + set_position_template = self._config.get(CONF_SET_POSITION_TEMPLATE) if set_position_template is not None: set_position_template.hass = self.hass + + get_position_template = self._config.get(CONF_GET_POSITION_TEMPLATE) + if get_position_template is not None: + get_position_template.hass = self.hass + set_tilt_template = self._config.get(CONF_TILT_COMMAND_TEMPLATE) if set_tilt_template is not None: set_tilt_template.hass = self.hass + tilt_status_template = self._config.get(CONF_TILT_STATUS_TEMPLATE) if tilt_status_template is not None: tilt_status_template.hass = self.hass @@ -290,24 +295,21 @@ class MqttCover(MqttEntity, CoverEntity): def state_message_received(msg): """Handle new MQTT state messages.""" payload = msg.payload - template = self._config.get(CONF_VALUE_TEMPLATE) - if template is not None: - payload = template.async_render_with_possible_json_value(payload) + value_template = self._config.get(CONF_VALUE_TEMPLATE) + if value_template is not None: + payload = value_template.async_render_with_possible_json_value(payload) if payload == self._config[CONF_STATE_STOPPED]: - if ( - self._optimistic - or self._config.get(CONF_GET_POSITION_TOPIC) is None - ): - self._state = ( - STATE_CLOSED if self._state == STATE_CLOSING else STATE_OPEN - ) - else: + if self._config.get(CONF_GET_POSITION_TOPIC) is not None: self._state = ( STATE_CLOSED if self._position == DEFAULT_POSITION_CLOSED else STATE_OPEN ) + else: + self._state = ( + STATE_CLOSED if self._state == STATE_CLOSING else STATE_OPEN + ) elif payload == self._config[CONF_STATE_OPENING]: self._state = STATE_OPENING elif payload == self._config[CONF_STATE_CLOSING]: @@ -616,7 +618,7 @@ class MqttCover(MqttEntity, CoverEntity): max_percent = 100 min_percent = 0 position_percentage = min(max(position_percentage, min_percent), max_percent) - if range_type == TILT_PAYLOAD and self._config[CONF_TILT_INVERT_STATE]: + if range_type == TILT_PAYLOAD and self._config.get(CONF_TILT_INVERT_STATE): return 100 - position_percentage return position_percentage @@ -640,6 +642,6 @@ class MqttCover(MqttEntity, CoverEntity): position = round(current_range * (percentage / 100.0)) position += offset - if range_type == TILT_PAYLOAD and self._config[CONF_TILT_INVERT_STATE]: + if range_type == TILT_PAYLOAD and self._config.get(CONF_TILT_INVERT_STATE): position = max_range - position + offset return position diff --git a/tests/components/mqtt/test_cover.py b/tests/components/mqtt/test_cover.py index 87b016e2d59..44144642f40 100644 --- a/tests/components/mqtt/test_cover.py +++ b/tests/components/mqtt/test_cover.py @@ -2032,10 +2032,10 @@ async def test_entity_debug_info_message(hass, mqtt_mock): ) -async def test_deprecated_value_template_for_position_topic_warnning( +async def test_deprecated_value_template_for_position_topic_warning( hass, caplog, mqtt_mock ): - """Test warnning when value_template is used for position_topic.""" + """Test warning when value_template is used for position_topic.""" assert await async_setup_component( hass, cover.DOMAIN, @@ -2054,13 +2054,13 @@ async def test_deprecated_value_template_for_position_topic_warnning( assert ( "using 'value_template' for 'position_topic' is deprecated " - "and will be removed from Home Assistant in version 2021.6" + "and will be removed from Home Assistant in version 2021.6, " "please replace it with 'position_template'" ) in caplog.text -async def test_deprecated_tilt_invert_state_warnning(hass, caplog, mqtt_mock): - """Test warnning when tilt_invert_state is used.""" +async def test_deprecated_tilt_invert_state_warning(hass, caplog, mqtt_mock): + """Test warning when tilt_invert_state is used.""" assert await async_setup_component( hass, cover.DOMAIN, @@ -2077,11 +2077,33 @@ async def test_deprecated_tilt_invert_state_warnning(hass, caplog, mqtt_mock): assert ( "'tilt_invert_state' is deprecated " - "and will be removed from Home Assistant in version 2021.6" + "and will be removed from Home Assistant in version 2021.6, " "please invert tilt using 'tilt_min' & 'tilt_max'" ) in caplog.text +async def test_no_deprecated_tilt_invert_state_warning(hass, caplog, mqtt_mock): + """Test warning when tilt_invert_state is used.""" + assert await async_setup_component( + hass, + cover.DOMAIN, + { + cover.DOMAIN: { + "platform": "mqtt", + "name": "test", + "command_topic": "command-topic", + } + }, + ) + await hass.async_block_till_done() + + assert ( + "'tilt_invert_state' is deprecated " + "and will be removed from Home Assistant in version 2021.6, " + "please invert tilt using 'tilt_min' & 'tilt_max'" + ) not in caplog.text + + async def test_no_deprecated_warning_for_position_topic_using_position_template( hass, caplog, mqtt_mock ): @@ -2104,7 +2126,7 @@ async def test_no_deprecated_warning_for_position_topic_using_position_template( assert ( "using 'value_template' for 'position_topic' is deprecated " - "and will be removed from Home Assistant in version 2021.6" + "and will be removed from Home Assistant in version 2021.6, " "please replace it with 'position_template'" ) not in caplog.text @@ -2221,8 +2243,8 @@ async def test_set_state_via_position_using_stopped_state(hass, mqtt_mock): assert state.state == STATE_OPEN -async def test_set_state_via_stopped_state_optimistic(hass, mqtt_mock): - """Test the controlling state via stopped state in optimistic mode.""" +async def test_position_via_position_topic_template(hass, mqtt_mock): + """Test position by updating status via position template.""" assert await async_setup_component( hass, cover.DOMAIN, @@ -2231,51 +2253,28 @@ async def test_set_state_via_stopped_state_optimistic(hass, mqtt_mock): "platform": "mqtt", "name": "test", "state_topic": "state-topic", - "position_topic": "get-position-topic", - "position_open": 100, - "position_closed": 0, - "state_open": "OPEN", - "state_closed": "CLOSE", - "state_stopped": "STOPPED", - "state_opening": "OPENING", - "state_closing": "CLOSING", "command_topic": "command-topic", - "qos": 0, - "optimistic": True, + "set_position_topic": "set-position-topic", + "position_topic": "get-position-topic", + "position_template": "{{ (value | multiply(0.01)) | int }}", } }, ) await hass.async_block_till_done() - async_fire_mqtt_message(hass, "state-topic", "OPEN") + async_fire_mqtt_message(hass, "get-position-topic", "99") - state = hass.states.get("cover.test") - assert state.state == STATE_OPEN + current_cover_position_position = hass.states.get("cover.test").attributes[ + ATTR_CURRENT_POSITION + ] + assert current_cover_position_position == 0 - async_fire_mqtt_message(hass, "get-position-topic", "50") + async_fire_mqtt_message(hass, "get-position-topic", "5000") - state = hass.states.get("cover.test") - assert state.state == STATE_OPEN - - async_fire_mqtt_message(hass, "state-topic", "OPENING") - - state = hass.states.get("cover.test") - assert state.state == STATE_OPENING - - async_fire_mqtt_message(hass, "state-topic", "STOPPED") - - state = hass.states.get("cover.test") - assert state.state == STATE_OPEN - - async_fire_mqtt_message(hass, "state-topic", "CLOSING") - - state = hass.states.get("cover.test") - assert state.state == STATE_CLOSING - - async_fire_mqtt_message(hass, "state-topic", "STOPPED") - - state = hass.states.get("cover.test") - assert state.state == STATE_CLOSED + current_cover_position_position = hass.states.get("cover.test").attributes[ + ATTR_CURRENT_POSITION + ] + assert current_cover_position_position == 50 async def test_set_state_via_stopped_state_no_position_topic(hass, mqtt_mock):