Skip to content

Commit

Permalink
Remove ZHA IasZone sensor migration (home-assistant#111893)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheJulianJES authored Mar 12, 2024
1 parent 1cceaaf commit 9ec0e09
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 92 deletions.
31 changes: 0 additions & 31 deletions homeassistant/components/zha/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from __future__ import annotations

import functools
from typing import Any

from zigpy.quirks.v2 import BinarySensorMetadata, EntityMetadata
import zigpy.types as t
Expand Down Expand Up @@ -209,36 +208,6 @@ def parse(value: bool | int) -> bool:
"""Parse the raw attribute into a bool state."""
return BinarySensor.parse(value & 3) # use only bit 0 and 1 for alarm state

# temporary code to migrate old IasZone sensors to update attribute cache state once
# remove in 2024.4.0
@property
def extra_state_attributes(self) -> dict[str, Any]:
"""Return state attributes."""
return {"migrated_to_cache": True} # writing new state means we're migrated

# temporary migration code
@callback
def async_restore_last_state(self, last_state):
"""Restore previous state."""
# trigger migration if extra state attribute is not present
if "migrated_to_cache" not in last_state.attributes:
self.migrate_to_zigpy_cache(last_state)

# temporary migration code
@callback
def migrate_to_zigpy_cache(self, last_state):
"""Save old IasZone sensor state to attribute cache."""
# previous HA versions did not update the attribute cache for IasZone sensors, so do it once here
# a HA state write is triggered shortly afterwards and writes the "migrated_to_cache" extra state attribute
if last_state.state == STATE_ON:
migrated_state = IasZone.ZoneStatus.Alarm_1
else:
migrated_state = IasZone.ZoneStatus(0)

self._cluster_handler.cluster.update_attribute(
IasZone.attributes_by_name[self._attribute_name].id, migrated_state
)


@STRICT_MATCH(cluster_handler_names=CLUSTER_HANDLER_ZONE, models={"WL4200", "WL4200S"})
class SinopeLeakStatus(BinarySensor):
Expand Down
61 changes: 0 additions & 61 deletions tests/components/zha/test_binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
async_test_rejoin,
find_entity_id,
send_attributes_report,
update_attribute_cache,
)
from .conftest import SIG_EP_INPUT, SIG_EP_OUTPUT, SIG_EP_PROFILE, SIG_EP_TYPE

Expand Down Expand Up @@ -151,66 +150,6 @@ async def test_binary_sensor(
assert hass.states.get(entity_id).state == STATE_OFF


@pytest.mark.parametrize(
"restored_state",
[
STATE_ON,
STATE_OFF,
],
)
async def test_binary_sensor_migration_not_migrated(
hass: HomeAssistant,
zigpy_device_mock,
core_rs,
zha_device_restored,
restored_state,
) -> None:
"""Test temporary ZHA IasZone binary_sensor migration to zigpy cache."""

entity_id = "binary_sensor.fakemanufacturer_fakemodel_ias_zone"
core_rs(entity_id, state=restored_state, attributes={}) # migration sensor state
await async_mock_load_restore_state_from_storage(hass)

zigpy_device = zigpy_device_mock(DEVICE_IAS)

zha_device = await zha_device_restored(zigpy_device)
entity_id = find_entity_id(Platform.BINARY_SENSOR, zha_device, hass)

assert entity_id is not None
assert hass.states.get(entity_id).state == restored_state

# confirm migration extra state attribute was set to True
assert hass.states.get(entity_id).attributes["migrated_to_cache"]


async def test_binary_sensor_migration_already_migrated(
hass: HomeAssistant,
zigpy_device_mock,
core_rs,
zha_device_restored,
) -> None:
"""Test temporary ZHA IasZone binary_sensor migration doesn't migrate multiple times."""

entity_id = "binary_sensor.fakemanufacturer_fakemodel_ias_zone"
core_rs(entity_id, state=STATE_OFF, attributes={"migrated_to_cache": True})
await async_mock_load_restore_state_from_storage(hass)

zigpy_device = zigpy_device_mock(DEVICE_IAS)

cluster = zigpy_device.endpoints.get(1).ias_zone
cluster.PLUGGED_ATTR_READS = {
"zone_status": security.IasZone.ZoneStatus.Alarm_1,
}
update_attribute_cache(cluster)

zha_device = await zha_device_restored(zigpy_device)
entity_id = find_entity_id(Platform.BINARY_SENSOR, zha_device, hass)

assert entity_id is not None
assert hass.states.get(entity_id).state == STATE_ON # matches attribute cache
assert hass.states.get(entity_id).attributes["migrated_to_cache"]


@pytest.mark.parametrize(
"restored_state",
[
Expand Down

0 comments on commit 9ec0e09

Please sign in to comment.