Intialize mqtt lock in an unknown state in pessimistic mode (#100943)

Intialize mqtt lock as unknown in pessimistic mode
pull/100952/head
Jan Bouwhuis 2023-09-26 19:53:45 +02:00 committed by GitHub
parent 734c4e8e32
commit 20a2e129fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 10 deletions

View File

@ -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 = (

View File

@ -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