mirror of
https://github.com/home-assistant/core.git
synced 2026-08-24 10:13:52 -05:00
Don't restore non-KNX attributes for KNX sensors (#179932)
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5
parent
9d12e00daf
commit
c830734535
@@ -181,7 +181,9 @@ class _KnxSensor(RestoreSensor, _KnxEntityBase):
|
||||
)
|
||||
):
|
||||
self._attr_native_value = last_sensor_data.native_value
|
||||
self._attr_extra_state_attributes.update(last_state.attributes)
|
||||
# only restore KNX specific attributes - others may have changed
|
||||
if (source := last_state.attributes.get(ATTR_SOURCE)) is not None:
|
||||
self._attr_extra_state_attributes[ATTR_SOURCE] = source
|
||||
await super().async_added_to_hass()
|
||||
|
||||
@override
|
||||
|
||||
@@ -12,8 +12,19 @@ from homeassistant.components.knx.const import (
|
||||
CONF_SYNC_STATE,
|
||||
)
|
||||
from homeassistant.components.knx.schema import SensorSchema
|
||||
from homeassistant.components.sensor import SensorDeviceClass, SensorStateClass
|
||||
from homeassistant.const import CONF_NAME, CONF_TYPE, STATE_UNKNOWN, Platform
|
||||
from homeassistant.components.sensor import (
|
||||
ATTR_STATE_CLASS,
|
||||
SensorDeviceClass,
|
||||
SensorStateClass,
|
||||
)
|
||||
from homeassistant.const import (
|
||||
ATTR_DEVICE_CLASS,
|
||||
ATTR_UNIT_OF_MEASUREMENT,
|
||||
CONF_NAME,
|
||||
CONF_TYPE,
|
||||
STATE_UNKNOWN,
|
||||
Platform,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant, State
|
||||
|
||||
from . import KnxEntityGenerator
|
||||
@@ -99,6 +110,55 @@ async def test_sensor_restore(hass: HomeAssistant, knx: KNXTestKit) -> None:
|
||||
assert not events
|
||||
|
||||
|
||||
async def test_sensor_restore_ignores_stale_attributes(
|
||||
hass: HomeAssistant, knx: KNXTestKit
|
||||
) -> None:
|
||||
"""Test that restoring doesn't reapply attributes the entity doesn't provide."""
|
||||
fake_state = State(
|
||||
"sensor.test",
|
||||
"ignored in favour of native_value",
|
||||
{
|
||||
ATTR_SOURCE: knx.INDIVIDUAL_ADDRESS,
|
||||
ATTR_DEVICE_CLASS: SensorDeviceClass.POWER,
|
||||
ATTR_STATE_CLASS: SensorStateClass.TOTAL_INCREASING,
|
||||
ATTR_UNIT_OF_MEASUREMENT: "W",
|
||||
},
|
||||
)
|
||||
extra_data = {"native_value": "42", "native_unit_of_measurement": None}
|
||||
mock_restore_cache_with_extra_data(hass, [(fake_state, extra_data)])
|
||||
|
||||
await knx.setup_integration(
|
||||
{
|
||||
SensorSchema.PLATFORM: [
|
||||
{
|
||||
CONF_NAME: "test",
|
||||
CONF_STATE_ADDRESS: "2/2/2",
|
||||
CONF_TYPE: "2byte_unsigned", # no unit or device class
|
||||
CONF_SYNC_STATE: False,
|
||||
},
|
||||
]
|
||||
}
|
||||
)
|
||||
|
||||
knx.assert_state(
|
||||
"sensor.test",
|
||||
"42",
|
||||
**{ATTR_SOURCE: knx.INDIVIDUAL_ADDRESS},
|
||||
device_class=None,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
unit_of_measurement=None,
|
||||
)
|
||||
# a new telegram doesn't reintroduce the stale attributes either
|
||||
await knx.receive_write("2/2/2", (0x00, 0x07))
|
||||
knx.assert_state(
|
||||
"sensor.test",
|
||||
"7",
|
||||
device_class=None,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
unit_of_measurement=None,
|
||||
)
|
||||
|
||||
|
||||
async def test_last_reported(
|
||||
hass: HomeAssistant,
|
||||
knx: KNXTestKit,
|
||||
|
||||
Reference in New Issue
Block a user