Fix KNX expose default value when attribute is `None` (#103446)
Fix KNX expose default value when attribute is `null`pull/103405/head
parent
52736c6039
commit
70196d5ee0
|
@ -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
|
||||||
|
|
|
@ -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."""
|
||||||
|
|
Loading…
Reference in New Issue