From 20a2e129fbafb354ca573872e251fd42a814e2e4 Mon Sep 17 00:00:00 2001 From: Jan Bouwhuis Date: Tue, 26 Sep 2023 19:53:45 +0200 Subject: [PATCH] Intialize mqtt lock in an unknown state in pessimistic mode (#100943) Intialize mqtt lock as unknown in pessimistic mode --- homeassistant/components/mqtt/lock.py | 12 +++++++----- tests/components/mqtt/test_lock.py | 11 ++++++----- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/mqtt/lock.py b/homeassistant/components/mqtt/lock.py index ebc4eced9c2..cdaa00e3d63 100644 --- a/homeassistant/components/mqtt/lock.py +++ b/homeassistant/components/mqtt/lock.py @@ -150,7 +150,6 @@ class MqttLock(MqttEntity, LockEntity): discovery_data: DiscoveryInfoType | None, ) -> None: """Initialize the lock.""" - self._attr_is_locked = False MqttEntity.__init__(self, hass, config, config_entry, discovery_data) @staticmethod @@ -160,10 +159,13 @@ class MqttLock(MqttEntity, LockEntity): def _setup_from_config(self, config: ConfigType) -> None: """(Re)Setup the entity.""" - self._optimistic = ( - config[CONF_OPTIMISTIC] or self._config.get(CONF_STATE_TOPIC) is None - ) - self._attr_assumed_state = bool(self._optimistic) + if ( + optimistic := config[CONF_OPTIMISTIC] + or config.get(CONF_STATE_TOPIC) is None + ): + self._attr_is_locked = False + self._optimistic = optimistic + self._attr_assumed_state = bool(optimistic) self._compiled_pattern = config.get(CONF_CODE_FORMAT) self._attr_code_format = ( diff --git a/tests/components/mqtt/test_lock.py b/tests/components/mqtt/test_lock.py index f9a33c211ee..0045e003804 100644 --- a/tests/components/mqtt/test_lock.py +++ b/tests/components/mqtt/test_lock.py @@ -22,6 +22,7 @@ from homeassistant.const import ( ATTR_CODE, ATTR_ENTITY_ID, ATTR_SUPPORTED_FEATURES, + STATE_UNKNOWN, Platform, ) from homeassistant.core import HomeAssistant @@ -107,7 +108,7 @@ async def test_controlling_state_via_topic( await mqtt_mock_entry() state = hass.states.get("lock.test") - assert state.state is STATE_UNLOCKED + assert state.state is STATE_UNKNOWN assert not state.attributes.get(ATTR_ASSUMED_STATE) assert not state.attributes.get(ATTR_SUPPORTED_FEATURES) @@ -137,7 +138,7 @@ async def test_controlling_non_default_state_via_topic( await mqtt_mock_entry() state = hass.states.get("lock.test") - assert state.state is STATE_UNLOCKED + assert state.state is STATE_UNKNOWN assert not state.attributes.get(ATTR_ASSUMED_STATE) async_fire_mqtt_message(hass, "state-topic", payload) @@ -197,7 +198,7 @@ async def test_controlling_state_via_topic_and_json_message( await mqtt_mock_entry() state = hass.states.get("lock.test") - assert state.state is STATE_UNLOCKED + assert state.state is STATE_UNKNOWN async_fire_mqtt_message(hass, "state-topic", payload) @@ -256,7 +257,7 @@ async def test_controlling_non_default_state_via_topic_and_json_message( await mqtt_mock_entry() state = hass.states.get("lock.test") - assert state.state is STATE_UNLOCKED + assert state.state is STATE_UNKNOWN async_fire_mqtt_message(hass, "state-topic", payload) @@ -574,7 +575,7 @@ async def test_sending_mqtt_commands_pessimistic( mqtt_mock = await mqtt_mock_entry() state = hass.states.get("lock.test") - assert state.state is STATE_UNLOCKED + assert state.state is STATE_UNKNOWN assert state.attributes.get(ATTR_SUPPORTED_FEATURES) == LockEntityFeature.OPEN # send lock command to lock