From 109b08bb577a47a6dc4f2bfa39b5c497b4d104b2 Mon Sep 17 00:00:00 2001 From: Shay Levy Date: Wed, 19 May 2021 15:34:20 +0300 Subject: [PATCH] Remove MQTT cover deprecated options (#50263) * Remove MQTT cover deprecated options * Fix pylint --- homeassistant/components/mqtt/cover.py | 53 ++--- tests/components/mqtt/test_cover.py | 313 +++++++++++++------------ 2 files changed, 185 insertions(+), 181 deletions(-) diff --git a/homeassistant/components/mqtt/cover.py b/homeassistant/components/mqtt/cover.py index eb7c5a79da9..a8de06ff1ca 100644 --- a/homeassistant/components/mqtt/cover.py +++ b/homeassistant/components/mqtt/cover.py @@ -71,7 +71,6 @@ CONF_STATE_OPEN = "state_open" CONF_STATE_OPENING = "state_opening" CONF_STATE_STOPPED = "state_stopped" CONF_TILT_CLOSED_POSITION = "tilt_closed_value" -CONF_TILT_INVERT_STATE = "tilt_invert_state" CONF_TILT_MAX = "tilt_max" CONF_TILT_MIN = "tilt_min" CONF_TILT_OPEN_POSITION = "tilt_opened_value" @@ -90,7 +89,6 @@ DEFAULT_POSITION_OPEN = 100 DEFAULT_RETAIN = False DEFAULT_STATE_STOPPED = "stopped" DEFAULT_TILT_CLOSED_POSITION = 0 -DEFAULT_TILT_INVERT_STATE = False DEFAULT_TILT_MAX = 100 DEFAULT_TILT_MIN = 0 DEFAULT_TILT_OPEN_POSITION = 100 @@ -112,25 +110,34 @@ def validate_options(value): """ if CONF_SET_POSITION_TOPIC in value and CONF_GET_POSITION_TOPIC not in value: raise vol.Invalid( - "'set_position_topic' must be set together with 'position_topic'." + f"'{CONF_SET_POSITION_TOPIC}' must be set together with '{CONF_GET_POSITION_TOPIC}'." ) - if ( - CONF_GET_POSITION_TOPIC in value - and CONF_STATE_TOPIC not in value - and CONF_VALUE_TEMPLATE in value - ): - _LOGGER.warning( - "Using 'value_template' for 'position_topic' is deprecated " - "and will be removed from Home Assistant in version 2021.6, " - "please replace it with 'position_template'" + # if templates are set make sure the topic for the template is also set + + if CONF_VALUE_TEMPLATE in value and CONF_STATE_TOPIC not in value: + raise vol.Invalid( + f"'{CONF_VALUE_TEMPLATE}' must be set together with '{CONF_STATE_TOPIC}'." ) - 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, " - "please invert tilt using 'tilt_min' & 'tilt_max'" + if CONF_GET_POSITION_TEMPLATE in value and CONF_GET_POSITION_TOPIC not in value: + raise vol.Invalid( + f"'{CONF_GET_POSITION_TEMPLATE}' must be set together with '{CONF_GET_POSITION_TOPIC}'." + ) + + if CONF_SET_POSITION_TEMPLATE in value and CONF_SET_POSITION_TOPIC not in value: + raise vol.Invalid( + f"'{CONF_SET_POSITION_TEMPLATE}' must be set together with '{CONF_SET_POSITION_TOPIC}'." + ) + + if CONF_TILT_COMMAND_TEMPLATE in value and CONF_TILT_COMMAND_TOPIC not in value: + raise vol.Invalid( + f"'{CONF_TILT_COMMAND_TEMPLATE}' must be set together with '{CONF_TILT_COMMAND_TOPIC}'." + ) + + if CONF_TILT_STATUS_TEMPLATE in value and CONF_TILT_STATUS_TOPIC not in value: + raise vol.Invalid( + f"'{CONF_TILT_STATUS_TEMPLATE}' must be set together with '{CONF_TILT_STATUS_TOPIC}'." ) return value @@ -164,7 +171,6 @@ 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): cv.boolean, vol.Optional(CONF_TILT_MAX, default=DEFAULT_TILT_MAX): int, vol.Optional(CONF_TILT_MIN, default=DEFAULT_TILT_MIN): int, vol.Optional( @@ -332,12 +338,6 @@ class MqttCover(MqttEntity, CoverEntity): payload = msg.payload template = self._config.get(CONF_GET_POSITION_TEMPLATE) - - # To be removed in 2021.6: - # allow using `value_template` as position template if no `state_topic` - if template is None and self._config.get(CONF_STATE_TOPIC) is None: - template = self._config.get(CONF_VALUE_TEMPLATE) - if template is not None: variables = { "entity_id": self.entity_id, @@ -665,8 +665,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.get(CONF_TILT_INVERT_STATE): - return 100 - position_percentage + return position_percentage def find_in_range_from_percent(self, percentage, range_type=TILT_PAYLOAD): @@ -689,8 +688,6 @@ class MqttCover(MqttEntity, CoverEntity): position = round(current_range * (percentage / 100.0)) position += offset - if range_type == TILT_PAYLOAD and self._config.get(CONF_TILT_INVERT_STATE): - position = max_range - position + offset return position def tilt_payload_received(self, _payload): diff --git a/tests/components/mqtt/test_cover.py b/tests/components/mqtt/test_cover.py index d0665fba318..c9dd30038ab 100644 --- a/tests/components/mqtt/test_cover.py +++ b/tests/components/mqtt/test_cover.py @@ -10,10 +10,22 @@ from homeassistant.components.cover import ( ATTR_POSITION, ATTR_TILT_POSITION, ) -from homeassistant.components.mqtt.cover import MqttCover +from homeassistant.components.mqtt.const import CONF_STATE_TOPIC +from homeassistant.components.mqtt.cover import ( + CONF_GET_POSITION_TEMPLATE, + CONF_GET_POSITION_TOPIC, + CONF_SET_POSITION_TEMPLATE, + CONF_SET_POSITION_TOPIC, + CONF_TILT_COMMAND_TEMPLATE, + CONF_TILT_COMMAND_TOPIC, + CONF_TILT_STATUS_TEMPLATE, + CONF_TILT_STATUS_TOPIC, + MqttCover, +) from homeassistant.const import ( ATTR_ASSUMED_STATE, ATTR_ENTITY_ID, + CONF_VALUE_TEMPLATE, SERVICE_CLOSE_COVER, SERVICE_CLOSE_COVER_TILT, SERVICE_OPEN_COVER, @@ -338,43 +350,6 @@ async def test_state_via_template_with_json_value(hass, mqtt_mock, caplog): ) in caplog.text -async def test_position_via_template(hass, mqtt_mock): - """Test the controlling state via topic.""" - assert await async_setup_component( - hass, - cover.DOMAIN, - { - cover.DOMAIN: { - "platform": "mqtt", - "name": "test", - "position_topic": "get-position-topic", - "command_topic": "command-topic", - "qos": 0, - "value_template": "{{ (value | multiply(0.01)) | int }}", - } - }, - ) - await hass.async_block_till_done() - - state = hass.states.get("cover.test") - assert state.state == STATE_UNKNOWN - - async_fire_mqtt_message(hass, "get-position-topic", "10000") - - state = hass.states.get("cover.test") - assert state.state == STATE_OPEN - - 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, "get-position-topic", "99") - - state = hass.states.get("cover.test") - assert state.state == STATE_CLOSED - - async def test_position_via_template_and_entity_id(hass, mqtt_mock): """Test the controlling state via topic.""" assert await async_setup_component( @@ -1899,7 +1874,6 @@ async def test_find_percentage_in_range_defaults(hass, mqtt_mock): "tilt_min": 0, "tilt_max": 100, "tilt_optimistic": False, - "tilt_invert_state": False, "set_position_topic": None, "set_position_template": None, "unique_id": None, @@ -1943,7 +1917,6 @@ async def test_find_percentage_in_range_altered(hass, mqtt_mock): "tilt_min": 80, "tilt_max": 180, "tilt_optimistic": False, - "tilt_invert_state": False, "set_position_topic": None, "set_position_template": None, "unique_id": None, @@ -1984,10 +1957,9 @@ async def test_find_percentage_in_range_defaults_inverted(hass, mqtt_mock): "value_template": None, "tilt_open_position": 100, "tilt_closed_position": 0, - "tilt_min": 0, - "tilt_max": 100, + "tilt_min": 100, + "tilt_max": 0, "tilt_optimistic": False, - "tilt_invert_state": True, "set_position_topic": None, "set_position_template": None, "unique_id": None, @@ -2028,10 +2000,9 @@ async def test_find_percentage_in_range_altered_inverted(hass, mqtt_mock): "value_template": None, "tilt_open_position": 180, "tilt_closed_position": 80, - "tilt_min": 80, - "tilt_max": 180, + "tilt_min": 180, + "tilt_max": 80, "tilt_optimistic": False, - "tilt_invert_state": True, "set_position_topic": None, "set_position_template": None, "unique_id": None, @@ -2075,7 +2046,6 @@ async def test_find_in_range_defaults(hass, mqtt_mock): "tilt_min": 0, "tilt_max": 100, "tilt_optimistic": False, - "tilt_invert_state": False, "set_position_topic": None, "set_position_template": None, "unique_id": None, @@ -2119,7 +2089,6 @@ async def test_find_in_range_altered(hass, mqtt_mock): "tilt_min": 80, "tilt_max": 180, "tilt_optimistic": False, - "tilt_invert_state": False, "set_position_topic": None, "set_position_template": None, "unique_id": None, @@ -2160,10 +2129,9 @@ async def test_find_in_range_defaults_inverted(hass, mqtt_mock): "value_template": None, "tilt_open_position": 100, "tilt_closed_position": 0, - "tilt_min": 0, - "tilt_max": 100, + "tilt_min": 100, + "tilt_max": 0, "tilt_optimistic": False, - "tilt_invert_state": True, "set_position_topic": None, "set_position_template": None, "unique_id": None, @@ -2204,10 +2172,9 @@ async def test_find_in_range_altered_inverted(hass, mqtt_mock): "value_template": None, "tilt_open_position": 180, "tilt_closed_position": 80, - "tilt_min": 80, - "tilt_max": 180, + "tilt_min": 180, + "tilt_max": 80, "tilt_optimistic": False, - "tilt_invert_state": True, "set_position_topic": None, "set_position_template": None, "unique_id": None, @@ -2430,105 +2397,6 @@ async def test_entity_debug_info_message(hass, mqtt_mock): ) -async def test_deprecated_value_template_for_position_topic_warning( - hass, caplog, mqtt_mock -): - """Test warning when value_template is used for position_topic.""" - assert await async_setup_component( - hass, - cover.DOMAIN, - { - cover.DOMAIN: { - "platform": "mqtt", - "name": "test", - "command_topic": "command-topic", - "set_position_topic": "set-position-topic", - "position_topic": "position-topic", - "value_template": "{{100-62}}", - } - }, - ) - await hass.async_block_till_done() - - assert ( - "Using 'value_template' for 'position_topic' is deprecated " - "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_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", - "tilt_invert_state": True, - } - }, - ) - 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'" - ) 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 -): - """Test no warning when position_template is used for position_topic.""" - assert await async_setup_component( - hass, - cover.DOMAIN, - { - cover.DOMAIN: { - "platform": "mqtt", - "name": "test", - "command_topic": "command-topic", - "set_position_topic": "set-position-topic", - "position_topic": "position-topic", - "position_template": "{{100-62}}", - } - }, - ) - await hass.async_block_till_done() - - assert ( - "using 'value_template' for 'position_topic' is deprecated " - "and will be removed from Home Assistant in version 2021.6, " - "please replace it with 'position_template'" - ) not in caplog.text - - async def test_state_and_position_topics_state_not_set_via_position_topic( hass, mqtt_mock ): @@ -2969,3 +2837,142 @@ async def test_position_via_position_topic_template_return_invalid_json( async_fire_mqtt_message(hass, "get-position-topic", "55") assert ("Payload '{'position': Undefined}' is not numeric") in caplog.text + + +async def test_set_position_topic_without_get_position_topic_error( + hass, caplog, mqtt_mock +): + """Test error when set_position_topic is used without position_topic.""" + assert await async_setup_component( + hass, + cover.DOMAIN, + { + cover.DOMAIN: { + "platform": "mqtt", + "name": "test", + "command_topic": "command-topic", + "set_position_topic": "set-position-topic", + "value_template": "{{100-62}}", + } + }, + ) + await hass.async_block_till_done() + + assert ( + f"'{CONF_SET_POSITION_TOPIC}' must be set together with '{CONF_GET_POSITION_TOPIC}'." + ) in caplog.text + + +async def test_value_template_without_state_topic_error(hass, caplog, mqtt_mock): + """Test error when value_template is used and state_topic is missing.""" + assert await async_setup_component( + hass, + cover.DOMAIN, + { + cover.DOMAIN: { + "platform": "mqtt", + "name": "test", + "command_topic": "command-topic", + "value_template": "{{100-62}}", + } + }, + ) + await hass.async_block_till_done() + + assert ( + f"'{CONF_VALUE_TEMPLATE}' must be set together with '{CONF_STATE_TOPIC}'." + ) in caplog.text + + +async def test_position_template_without_position_topic_error(hass, caplog, mqtt_mock): + """Test error when position_template is used and position_topic is missing.""" + assert await async_setup_component( + hass, + cover.DOMAIN, + { + cover.DOMAIN: { + "platform": "mqtt", + "name": "test", + "command_topic": "command-topic", + "position_template": "{{100-52}}", + } + }, + ) + await hass.async_block_till_done() + + assert ( + f"'{CONF_GET_POSITION_TEMPLATE}' must be set together with '{CONF_GET_POSITION_TOPIC}'." + in caplog.text + ) + + +async def test_set_position_template_without_set_position_topic( + hass, caplog, mqtt_mock +): + """Test error when set_position_template is used and set_position_topic is missing.""" + assert await async_setup_component( + hass, + cover.DOMAIN, + { + cover.DOMAIN: { + "platform": "mqtt", + "name": "test", + "command_topic": "command-topic", + "set_position_template": "{{100-42}}", + } + }, + ) + await hass.async_block_till_done() + + assert ( + f"'{CONF_SET_POSITION_TEMPLATE}' must be set together with '{CONF_SET_POSITION_TOPIC}'." + in caplog.text + ) + + +async def test_tilt_command_template_without_tilt_command_topic( + hass, caplog, mqtt_mock +): + """Test error when tilt_command_template is used and tilt_command_topic is missing.""" + assert await async_setup_component( + hass, + cover.DOMAIN, + { + cover.DOMAIN: { + "platform": "mqtt", + "name": "test", + "command_topic": "command-topic", + "tilt_command_template": "{{100-32}}", + } + }, + ) + await hass.async_block_till_done() + + assert ( + f"'{CONF_TILT_COMMAND_TEMPLATE}' must be set together with '{CONF_TILT_COMMAND_TOPIC}'." + in caplog.text + ) + + +async def test_tilt_status_template_without_tilt_status_topic_topic( + hass, caplog, mqtt_mock +): + """Test error when tilt_status_template is used and tilt_status_topic is missing.""" + assert await async_setup_component( + hass, + cover.DOMAIN, + { + cover.DOMAIN: { + "platform": "mqtt", + "name": "test", + "command_topic": "command-topic", + "tilt_status_template": "{{100-22}}", + } + }, + ) + await hass.async_block_till_done() + + assert ( + f"'{CONF_TILT_STATUS_TEMPLATE}' must be set together with '{CONF_TILT_STATUS_TOPIC}'." + in caplog.text + )