plugwise / plugwise-beta

Custom-component / development version of the Plugwise Home Assistant platform
MIT License
24 stars 8 forks source link

Delete removed Plugwise devices at runtime. #627

Closed bouwew closed 4 months ago

bouwew commented 4 months ago

Fully working locally for my Adam :)

sonarcloud[bot] commented 4 months ago

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

bouwew commented 4 months ago

@mib1185 this is my implementation. It's a bit special because there can be more than 1 type of Plugwise devices in a HA system. The code must only remove a device when it is connected to the actual Plugwise Gateway = via_device.

Anyway, with the shown code I get this in my debug-logging when running the test_remove_device() test:

2024-05-09 11:33:34.361 DEBUG    MainThread homeassistant.components.plugwise:coordinator.py:186 HOI removed device(s) found
2024-05-09 11:33:34.361 DEBUG    MainThread homeassistant.core:core.py:1533 Bus:Handling <Event device_registry_updated[L]: action=remove, device_id=f827c330f0b37eb8d762d3474cd460e3>
2024-05-09 11:33:34.362 DEBUG    MainThread homeassistant.core:core.py:1533 Bus:Handling <Event entity_registry_updated[L]: action=remove, entity_id=number.tom_badkamer_temperature_offset>
2024-05-09 11:33:34.363 DEBUG    MainThread homeassistant.core:core.py:1533 Bus:Handling <Event state_changed[L]: entity_id=number.tom_badkamer_temperature_offset, old_state=<state number.tom_badkamer_temperature_offset=0.1; min=-2.0, max=2.0, step=0.1, mode=box, unit_of_measurement=°C, device_class=temperature, friendly_name=Tom Badkamer Temperature offset @ 2024-05-09T02:33:34.301517-07:00>, new_state=None>
2024-05-09 11:33:34.364 DEBUG    MainThread homeassistant.core:core.py:1533 Bus:Handling <Event entity_registry_updated[L]: action=remove, entity_id=sensor.tom_badkamer_temperature>
2024-05-09 11:33:34.365 DEBUG    MainThread homeassistant.core:core.py:1533 Bus:Handling <Event state_changed[L]: entity_id=sensor.tom_badkamer_temperature, old_state=<state sensor.tom_badkamer_temperature=18.6; state_class=measurement, unit_of_measurement=°C, device_class=temperature, friendly_name=Tom Badkamer Temperature @ 2024-05-09T02:33:34.329754-07:00>, new_state=None>
2024-05-09 11:33:34.366 DEBUG    MainThread homeassistant.core:core.py:1533 Bus:Handling <Event entity_registry_updated[L]: action=remove, entity_id=sensor.tom_badkamer_temperature_difference>
2024-05-09 11:33:34.367 DEBUG    MainThread homeassistant.core:core.py:1533 Bus:Handling <Event state_changed[L]: entity_id=sensor.tom_badkamer_temperature_difference, old_state=<state sensor.tom_badkamer_temperature_difference=-0.2; state_class=measurement, unit_of_measurement=°C, device_class=temperature, friendly_name=Tom Badkamer Temperature difference @ 2024-05-09T02:33:34.331697-07:00>, new_state=None>
2024-05-09 11:33:34.368 DEBUG    MainThread homeassistant.core:core.py:1533 Bus:Handling <Event entity_registry_updated[L]: action=remove, entity_id=sensor.tom_badkamer_battery>
2024-05-09 11:33:34.369 DEBUG    MainThread homeassistant.core:core.py:1533 Bus:Handling <Event state_changed[L]: entity_id=sensor.tom_badkamer_battery, old_state=<state sensor.tom_badkamer_battery=99; state_class=measurement, unit_of_measurement=%, device_class=battery, friendly_name=Tom Badkamer Battery @ 2024-05-09T02:33:34.333355-07:00>, new_state=None>
2024-05-09 11:33:34.369 DEBUG    MainThread homeassistant.core:core.py:1533 Bus:Handling <Event entity_registry_updated[L]: action=remove, entity_id=sensor.tom_badkamer_valve_position>
2024-05-09 11:33:34.371 DEBUG    MainThread homeassistant.core:core.py:1533 Bus:Handling <Event state_changed[L]: entity_id=sensor.tom_badkamer_valve_position, old_state=<state sensor.tom_badkamer_valve_position=100; state_class=measurement, unit_of_measurement=%, friendly_name=Tom Badkamer Valve position @ 2024-05-09T02:33:34.335015-07:00>, new_state=None>
2024-05-09 11:33:34.371 DEBUG    MainThread homeassistant.components.plugwise:coordinator.py:74 Removed plugwise device Tom/Floor 1772a4ea304041adb83f357b751341ff from device_registry

First, the removed device is removed from the device_registry, then the connected entities are removed from the entity_registry.