Fix KNX expose default value when attribute is `None` (#103446)

Fix KNX expose default value when attribute is `null`
pull/103405/head
Matthias Alphart 2023-11-06 08:39:40 +01:00 committed by GitHub
parent 52736c6039
commit 70196d5ee0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 5 deletions

View File

@ -122,12 +122,12 @@ class KNXExposeSensor:
"""Extract value from state.""" """Extract value from state."""
if state is None or state.state in (STATE_UNKNOWN, STATE_UNAVAILABLE): if state is None or state.state in (STATE_UNKNOWN, STATE_UNAVAILABLE):
value = self.expose_default value = self.expose_default
elif self.expose_attribute is not None:
_attr = state.attributes.get(self.expose_attribute)
value = _attr if _attr is not None else self.expose_default
else: else:
value = ( value = state.state
state.state
if self.expose_attribute is None
else state.attributes.get(self.expose_attribute, self.expose_default)
)
if self.expose_type == "binary": if self.expose_type == "binary":
if value in (1, STATE_ON, "True"): if value in (1, STATE_ON, "True"):
return True return True

View File

@ -85,6 +85,14 @@ async def test_expose_attribute(hass: HomeAssistant, knx: KNXTestKit) -> None:
hass.states.async_set(entity_id, "off", {}) hass.states.async_set(entity_id, "off", {})
await knx.assert_telegram_count(0) await knx.assert_telegram_count(0)
# Change attribute; keep state
hass.states.async_set(entity_id, "on", {attribute: 1})
await knx.assert_write("1/1/8", (1,))
# Change state to "off"; null attribute
hass.states.async_set(entity_id, "off", {attribute: None})
await knx.assert_telegram_count(0)
async def test_expose_attribute_with_default( async def test_expose_attribute_with_default(
hass: HomeAssistant, knx: KNXTestKit hass: HomeAssistant, knx: KNXTestKit
@ -132,6 +140,14 @@ async def test_expose_attribute_with_default(
hass.states.async_set(entity_id, "off", {}) hass.states.async_set(entity_id, "off", {})
await knx.assert_write("1/1/8", (0,)) await knx.assert_write("1/1/8", (0,))
# Change state and attribute
hass.states.async_set(entity_id, "on", {attribute: 1})
await knx.assert_write("1/1/8", (1,))
# Change state to "off"; null attribute
hass.states.async_set(entity_id, "off", {attribute: None})
await knx.assert_write("1/1/8", (0,))
async def test_expose_string(hass: HomeAssistant, knx: KNXTestKit) -> None: async def test_expose_string(hass: HomeAssistant, knx: KNXTestKit) -> None:
"""Test an expose to send string values of up to 14 bytes only.""" """Test an expose to send string values of up to 14 bytes only."""