Improve restoring UniFi POE entity state (#47148)

* Improve restoring data and better handling when the restore data is empty
Improve readability of some logic related to POE clients

* There is no need to check clients_all in Switch platform

* Add better tests when restoring state

* Port except handling shouldn't be needed anymore

* Walrusify get_last_state
This commit is contained in:
Robert Svensson
2021-03-05 22:09:05 +01:00
committed by GitHub
parent 02e723f206
commit 50d3aae418
3 changed files with 200 additions and 68 deletions

View File

@@ -40,6 +40,7 @@ from homeassistant.core import callback
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers import aiohttp_client
from homeassistant.helpers.dispatcher import async_dispatcher_send
from homeassistant.helpers.entity_registry import async_entries_for_config_entry
from homeassistant.helpers.event import async_track_time_interval
import homeassistant.util.dt as dt_util
@@ -338,21 +339,18 @@ class UniFiController:
self._site_role = description[0]["site_role"]
# Restore clients that is not a part of active clients list.
# Restore clients that are not a part of active clients list.
entity_registry = await self.hass.helpers.entity_registry.async_get_registry()
for entity in entity_registry.entities.values():
if (
entity.config_entry_id != self.config_entry.entry_id
or "-" not in entity.unique_id
):
for entry in async_entries_for_config_entry(
entity_registry, self.config_entry.entry_id
):
if entry.domain == TRACKER_DOMAIN:
mac = entry.unique_id.split("-", 1)[0]
elif entry.domain == SWITCH_DOMAIN:
mac = entry.unique_id.split("-", 1)[1]
else:
continue
mac = ""
if entity.domain == TRACKER_DOMAIN:
mac = entity.unique_id.split("-", 1)[0]
elif entity.domain == SWITCH_DOMAIN:
mac = entity.unique_id.split("-", 1)[1]
if mac in self.api.clients or mac not in self.api.clients_all:
continue
@@ -360,7 +358,7 @@ class UniFiController:
self.api.clients.process_raw([client.raw])
LOGGER.debug(
"Restore disconnected client %s (%s)",
entity.entity_id,
entry.entity_id,
client.mac,
)