Make sure Guardian data storage conforms to standards (#57809)

This commit is contained in:
Aaron Bach 2021-10-22 04:25:05 -06:00 committed by GitHub
parent a0bb2c8b33
commit 6dd72869a6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 34 additions and 51 deletions

View file

@ -29,7 +29,6 @@ from .const import (
DATA_COORDINATOR, DATA_COORDINATOR,
DATA_COORDINATOR_PAIRED_SENSOR, DATA_COORDINATOR_PAIRED_SENSOR,
DATA_PAIRED_SENSOR_MANAGER, DATA_PAIRED_SENSOR_MANAGER,
DATA_UNSUB_DISPATCHER_CONNECT,
DOMAIN, DOMAIN,
LOGGER, LOGGER,
SIGNAL_PAIRED_SENSOR_COORDINATOR_ADDED, SIGNAL_PAIRED_SENSOR_COORDINATOR_ADDED,
@ -41,22 +40,13 @@ PLATFORMS = ["binary_sensor", "sensor", "switch"]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Elexa Guardian from a config entry.""" """Set up Elexa Guardian from a config entry."""
hass.data.setdefault( hass.data.setdefault(DOMAIN, {})
DOMAIN, hass.data[DOMAIN][entry.entry_id] = {
{ DATA_COORDINATOR: {},
DATA_CLIENT: {}, DATA_COORDINATOR_PAIRED_SENSOR: {},
DATA_COORDINATOR: {}, }
DATA_COORDINATOR_PAIRED_SENSOR: {},
DATA_PAIRED_SENSOR_MANAGER: {}, client = Client(entry.data[CONF_IP_ADDRESS], port=entry.data[CONF_PORT])
DATA_UNSUB_DISPATCHER_CONNECT: {},
},
)
client = hass.data[DOMAIN][DATA_CLIENT][entry.entry_id] = Client(
entry.data[CONF_IP_ADDRESS], port=entry.data[CONF_PORT]
)
hass.data[DOMAIN][DATA_COORDINATOR][entry.entry_id] = {}
hass.data[DOMAIN][DATA_COORDINATOR_PAIRED_SENSOR][entry.entry_id] = {}
hass.data[DOMAIN][DATA_UNSUB_DISPATCHER_CONNECT][entry.entry_id] = []
# The valve controller's UDP-based API can't handle concurrent requests very well, # The valve controller's UDP-based API can't handle concurrent requests very well,
# so we use a lock to ensure that only one API request is reaching it at a time: # so we use a lock to ensure that only one API request is reaching it at a time:
@ -71,7 +61,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
(API_VALVE_STATUS, client.valve.status), (API_VALVE_STATUS, client.valve.status),
(API_WIFI_STATUS, client.wifi.status), (API_WIFI_STATUS, client.wifi.status),
): ):
coordinator = hass.data[DOMAIN][DATA_COORDINATOR][entry.entry_id][ coordinator = hass.data[DOMAIN][entry.entry_id][DATA_COORDINATOR][
api api
] = GuardianDataUpdateCoordinator( ] = GuardianDataUpdateCoordinator(
hass, hass,
@ -84,11 +74,12 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
init_valve_controller_tasks.append(coordinator.async_refresh()) init_valve_controller_tasks.append(coordinator.async_refresh())
await asyncio.gather(*init_valve_controller_tasks) await asyncio.gather(*init_valve_controller_tasks)
hass.data[DOMAIN][entry.entry_id][DATA_CLIENT] = client
# Set up an object to evaluate each batch of paired sensor UIDs and add/remove # Set up an object to evaluate each batch of paired sensor UIDs and add/remove
# devices as appropriate: # devices as appropriate:
paired_sensor_manager = hass.data[DOMAIN][DATA_PAIRED_SENSOR_MANAGER][ paired_sensor_manager = hass.data[DOMAIN][entry.entry_id][
entry.entry_id DATA_PAIRED_SENSOR_MANAGER
] = PairedSensorManager(hass, entry, client, api_lock) ] = PairedSensorManager(hass, entry, client, api_lock)
await paired_sensor_manager.async_process_latest_paired_sensor_uids() await paired_sensor_manager.async_process_latest_paired_sensor_uids()
@ -99,7 +90,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
paired_sensor_manager.async_process_latest_paired_sensor_uids() paired_sensor_manager.async_process_latest_paired_sensor_uids()
) )
hass.data[DOMAIN][DATA_COORDINATOR][entry.entry_id][ hass.data[DOMAIN][entry.entry_id][DATA_COORDINATOR][
API_SENSOR_PAIR_DUMP API_SENSOR_PAIR_DUMP
].async_add_listener(async_process_paired_sensor_uids) ].async_add_listener(async_process_paired_sensor_uids)
@ -113,12 +104,7 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry.""" """Unload a config entry."""
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok: if unload_ok:
hass.data[DOMAIN][DATA_CLIENT].pop(entry.entry_id) hass.data[DOMAIN].pop(entry.entry_id)
hass.data[DOMAIN][DATA_COORDINATOR].pop(entry.entry_id)
hass.data[DOMAIN][DATA_COORDINATOR_PAIRED_SENSOR].pop(entry.entry_id)
for unsub in hass.data[DOMAIN][DATA_UNSUB_DISPATCHER_CONNECT][entry.entry_id]:
unsub()
hass.data[DOMAIN][DATA_UNSUB_DISPATCHER_CONNECT].pop(entry.entry_id)
return unload_ok return unload_ok
@ -146,8 +132,8 @@ class PairedSensorManager:
self._paired_uids.add(uid) self._paired_uids.add(uid)
coordinator = self._hass.data[DOMAIN][DATA_COORDINATOR_PAIRED_SENSOR][ coordinator = self._hass.data[DOMAIN][self._entry.entry_id][
self._entry.entry_id DATA_COORDINATOR_PAIRED_SENSOR
][uid] = GuardianDataUpdateCoordinator( ][uid] = GuardianDataUpdateCoordinator(
self._hass, self._hass,
client=self._client, client=self._client,
@ -170,7 +156,7 @@ class PairedSensorManager:
"""Process a list of new UIDs.""" """Process a list of new UIDs."""
try: try:
uids = set( uids = set(
self._hass.data[DOMAIN][DATA_COORDINATOR][self._entry.entry_id][ self._hass.data[DOMAIN][self._entry.entry_id][DATA_COORDINATOR][
API_SENSOR_PAIR_DUMP API_SENSOR_PAIR_DUMP
].data["paired_uids"] ].data["paired_uids"]
) )
@ -197,8 +183,8 @@ class PairedSensorManager:
# Clear out objects related to this paired sensor: # Clear out objects related to this paired sensor:
self._paired_uids.remove(uid) self._paired_uids.remove(uid)
self._hass.data[DOMAIN][DATA_COORDINATOR_PAIRED_SENSOR][ self._hass.data[DOMAIN][self._entry.entry_id][
self._entry.entry_id DATA_COORDINATOR_PAIRED_SENSOR
].pop(uid) ].pop(uid)
# Remove the paired sensor device from the device registry (which will # Remove the paired sensor device from the device registry (which will

View file

@ -22,7 +22,6 @@ from .const import (
CONF_UID, CONF_UID,
DATA_COORDINATOR, DATA_COORDINATOR,
DATA_COORDINATOR_PAIRED_SENSOR, DATA_COORDINATOR_PAIRED_SENSOR,
DATA_UNSUB_DISPATCHER_CONNECT,
DOMAIN, DOMAIN,
SIGNAL_PAIRED_SENSOR_COORDINATOR_ADDED, SIGNAL_PAIRED_SENSOR_COORDINATOR_ADDED,
) )
@ -69,7 +68,7 @@ async def async_setup_entry(
@callback @callback
def add_new_paired_sensor(uid: str) -> None: def add_new_paired_sensor(uid: str) -> None:
"""Add a new paired sensor.""" """Add a new paired sensor."""
coordinator = hass.data[DOMAIN][DATA_COORDINATOR_PAIRED_SENSOR][entry.entry_id][ coordinator = hass.data[DOMAIN][entry.entry_id][DATA_COORDINATOR_PAIRED_SENSOR][
uid uid
] ]
@ -81,7 +80,7 @@ async def async_setup_entry(
) )
# Handle adding paired sensors after HASS startup: # Handle adding paired sensors after HASS startup:
hass.data[DOMAIN][DATA_UNSUB_DISPATCHER_CONNECT][entry.entry_id].append( entry.async_on_unload(
async_dispatcher_connect( async_dispatcher_connect(
hass, hass,
SIGNAL_PAIRED_SENSOR_COORDINATOR_ADDED.format(entry.data[CONF_UID]), SIGNAL_PAIRED_SENSOR_COORDINATOR_ADDED.format(entry.data[CONF_UID]),
@ -92,7 +91,7 @@ async def async_setup_entry(
# Add all valve controller-specific binary sensors: # Add all valve controller-specific binary sensors:
sensors: list[PairedSensorBinarySensor | ValveControllerBinarySensor] = [ sensors: list[PairedSensorBinarySensor | ValveControllerBinarySensor] = [
ValveControllerBinarySensor( ValveControllerBinarySensor(
entry, hass.data[DOMAIN][DATA_COORDINATOR][entry.entry_id], description entry, hass.data[DOMAIN][entry.entry_id][DATA_COORDINATOR], description
) )
for description in VALVE_CONTROLLER_DESCRIPTIONS for description in VALVE_CONTROLLER_DESCRIPTIONS
] ]
@ -101,8 +100,8 @@ async def async_setup_entry(
sensors.extend( sensors.extend(
[ [
PairedSensorBinarySensor(entry, coordinator, description) PairedSensorBinarySensor(entry, coordinator, description)
for coordinator in hass.data[DOMAIN][DATA_COORDINATOR_PAIRED_SENSOR][ for coordinator in hass.data[DOMAIN][entry.entry_id][
entry.entry_id DATA_COORDINATOR_PAIRED_SENSOR
].values() ].values()
for description in PAIRED_SENSOR_DESCRIPTIONS for description in PAIRED_SENSOR_DESCRIPTIONS
] ]

View file

@ -18,6 +18,5 @@ DATA_CLIENT = "client"
DATA_COORDINATOR = "coordinator" DATA_COORDINATOR = "coordinator"
DATA_COORDINATOR_PAIRED_SENSOR = "coordinator_paired_sensor" DATA_COORDINATOR_PAIRED_SENSOR = "coordinator_paired_sensor"
DATA_PAIRED_SENSOR_MANAGER = "paired_sensor_manager" DATA_PAIRED_SENSOR_MANAGER = "paired_sensor_manager"
DATA_UNSUB_DISPATCHER_CONNECT = "unsub_dispatcher_connect"
SIGNAL_PAIRED_SENSOR_COORDINATOR_ADDED = "guardian_paired_sensor_coordinator_added_{0}" SIGNAL_PAIRED_SENSOR_COORDINATOR_ADDED = "guardian_paired_sensor_coordinator_added_{0}"

View file

@ -26,7 +26,6 @@ from .const import (
CONF_UID, CONF_UID,
DATA_COORDINATOR, DATA_COORDINATOR,
DATA_COORDINATOR_PAIRED_SENSOR, DATA_COORDINATOR_PAIRED_SENSOR,
DATA_UNSUB_DISPATCHER_CONNECT,
DOMAIN, DOMAIN,
SIGNAL_PAIRED_SENSOR_COORDINATOR_ADDED, SIGNAL_PAIRED_SENSOR_COORDINATOR_ADDED,
) )
@ -75,7 +74,7 @@ async def async_setup_entry(
@callback @callback
def add_new_paired_sensor(uid: str) -> None: def add_new_paired_sensor(uid: str) -> None:
"""Add a new paired sensor.""" """Add a new paired sensor."""
coordinator = hass.data[DOMAIN][DATA_COORDINATOR_PAIRED_SENSOR][entry.entry_id][ coordinator = hass.data[DOMAIN][entry.entry_id][DATA_COORDINATOR_PAIRED_SENSOR][
uid uid
] ]
@ -87,7 +86,7 @@ async def async_setup_entry(
) )
# Handle adding paired sensors after HASS startup: # Handle adding paired sensors after HASS startup:
hass.data[DOMAIN][DATA_UNSUB_DISPATCHER_CONNECT][entry.entry_id].append( entry.async_on_unload(
async_dispatcher_connect( async_dispatcher_connect(
hass, hass,
SIGNAL_PAIRED_SENSOR_COORDINATOR_ADDED.format(entry.data[CONF_UID]), SIGNAL_PAIRED_SENSOR_COORDINATOR_ADDED.format(entry.data[CONF_UID]),
@ -98,7 +97,7 @@ async def async_setup_entry(
# Add all valve controller-specific binary sensors: # Add all valve controller-specific binary sensors:
sensors: list[PairedSensorSensor | ValveControllerSensor] = [ sensors: list[PairedSensorSensor | ValveControllerSensor] = [
ValveControllerSensor( ValveControllerSensor(
entry, hass.data[DOMAIN][DATA_COORDINATOR][entry.entry_id], description entry, hass.data[DOMAIN][entry.entry_id][DATA_COORDINATOR], description
) )
for description in VALVE_CONTROLLER_DESCRIPTIONS for description in VALVE_CONTROLLER_DESCRIPTIONS
] ]
@ -107,8 +106,8 @@ async def async_setup_entry(
sensors.extend( sensors.extend(
[ [
PairedSensorSensor(entry, coordinator, description) PairedSensorSensor(entry, coordinator, description)
for coordinator in hass.data[DOMAIN][DATA_COORDINATOR_PAIRED_SENSOR][ for coordinator in hass.data[DOMAIN][entry.entry_id][
entry.entry_id DATA_COORDINATOR_PAIRED_SENSOR
].values() ].values()
for description in PAIRED_SENSOR_DESCRIPTIONS for description in PAIRED_SENSOR_DESCRIPTIONS
] ]

View file

@ -81,8 +81,8 @@ async def async_setup_entry(
[ [
ValveControllerSwitch( ValveControllerSwitch(
entry, entry,
hass.data[DOMAIN][DATA_CLIENT][entry.entry_id], hass.data[DOMAIN][entry.entry_id][DATA_CLIENT],
hass.data[DOMAIN][DATA_COORDINATOR][entry.entry_id], hass.data[DOMAIN][entry.entry_id][DATA_COORDINATOR],
) )
] ]
) )
@ -160,8 +160,8 @@ class ValveControllerSwitch(ValveControllerEntity, SwitchEntity):
LOGGER.error("Error while adding paired sensor: %s", err) LOGGER.error("Error while adding paired sensor: %s", err)
return return
await self.hass.data[DOMAIN][DATA_PAIRED_SENSOR_MANAGER][ await self.hass.data[DOMAIN][self._entry.entry_id][
self._entry.entry_id DATA_PAIRED_SENSOR_MANAGER
].async_pair_sensor(uid) ].async_pair_sensor(uid)
async def async_reboot(self) -> None: async def async_reboot(self) -> None:
@ -189,8 +189,8 @@ class ValveControllerSwitch(ValveControllerEntity, SwitchEntity):
LOGGER.error("Error while removing paired sensor: %s", err) LOGGER.error("Error while removing paired sensor: %s", err)
return return
await self.hass.data[DOMAIN][DATA_PAIRED_SENSOR_MANAGER][ await self.hass.data[DOMAIN][self._entry.entry_id][
self._entry.entry_id DATA_PAIRED_SENSOR_MANAGER
].async_unpair_sensor(uid) ].async_unpair_sensor(uid)
async def async_upgrade_firmware( async def async_upgrade_firmware(