diff --git a/homeassistant/components/mqtt/cover.py b/homeassistant/components/mqtt/cover.py index a8de06ff1ca..0a78a102a13 100644 --- a/homeassistant/components/mqtt/cover.py +++ b/homeassistant/components/mqtt/cover.py @@ -94,7 +94,6 @@ DEFAULT_TILT_MIN = 0 DEFAULT_TILT_OPEN_POSITION = 100 DEFAULT_TILT_OPTIMISTIC = False -OPEN_CLOSE_FEATURES = SUPPORT_OPEN | SUPPORT_CLOSE TILT_FEATURES = ( SUPPORT_OPEN_TILT | SUPPORT_CLOSE_TILT @@ -151,8 +150,12 @@ PLATFORM_SCHEMA = vol.All( vol.Optional(CONF_GET_POSITION_TOPIC): mqtt.valid_subscribe_topic, vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, vol.Optional(CONF_OPTIMISTIC, default=DEFAULT_OPTIMISTIC): cv.boolean, - vol.Optional(CONF_PAYLOAD_CLOSE, default=DEFAULT_PAYLOAD_CLOSE): cv.string, - vol.Optional(CONF_PAYLOAD_OPEN, default=DEFAULT_PAYLOAD_OPEN): cv.string, + vol.Optional(CONF_PAYLOAD_CLOSE, default=DEFAULT_PAYLOAD_CLOSE): vol.Any( + cv.string, None + ), + vol.Optional(CONF_PAYLOAD_OPEN, default=DEFAULT_PAYLOAD_OPEN): vol.Any( + cv.string, None + ), vol.Optional(CONF_PAYLOAD_STOP, default=DEFAULT_PAYLOAD_STOP): vol.Any( cv.string, None ), @@ -474,8 +477,10 @@ class MqttCover(MqttEntity, CoverEntity): """Flag supported features.""" supported_features = 0 if self._config.get(CONF_COMMAND_TOPIC) is not None: - supported_features = OPEN_CLOSE_FEATURES - + if self._config.get(CONF_PAYLOAD_OPEN) is not None: + supported_features |= SUPPORT_OPEN + if self._config.get(CONF_PAYLOAD_CLOSE) is not None: + supported_features |= SUPPORT_CLOSE if self._config.get(CONF_PAYLOAD_STOP) is not None: supported_features |= SUPPORT_STOP diff --git a/tests/components/mqtt/test_cover.py b/tests/components/mqtt/test_cover.py index c9dd30038ab..c5a8552dc20 100644 --- a/tests/components/mqtt/test_cover.py +++ b/tests/components/mqtt/test_cover.py @@ -1014,6 +1014,50 @@ async def test_no_command_topic(hass, mqtt_mock): assert hass.states.get("cover.test").attributes["supported_features"] == 240 +async def test_no_payload_close(hass, mqtt_mock): + """Test with no close payload.""" + assert await async_setup_component( + hass, + cover.DOMAIN, + { + cover.DOMAIN: { + "platform": "mqtt", + "name": "test", + "command_topic": "command-topic", + "qos": 0, + "payload_open": "OPEN", + "payload_close": None, + "payload_stop": "STOP", + } + }, + ) + await hass.async_block_till_done() + + assert hass.states.get("cover.test").attributes["supported_features"] == 9 + + +async def test_no_payload_open(hass, mqtt_mock): + """Test with no open payload.""" + assert await async_setup_component( + hass, + cover.DOMAIN, + { + cover.DOMAIN: { + "platform": "mqtt", + "name": "test", + "command_topic": "command-topic", + "qos": 0, + "payload_open": None, + "payload_close": "CLOSE", + "payload_stop": "STOP", + } + }, + ) + await hass.async_block_till_done() + + assert hass.states.get("cover.test").attributes["supported_features"] == 10 + + async def test_no_payload_stop(hass, mqtt_mock): """Test with no stop payload.""" assert await async_setup_component(