MySensors: Move all storage in hass object under DOMAIN

The integration now stores everything under hass.data["mysensors"]
instead of using several top level keys.
This commit is contained in:
functionpointer
2021-01-22 20:34:43 +01:00
parent 251dcf67b7
commit 296f325b00
3 changed files with 25 additions and 21 deletions

View File

@@ -173,9 +173,12 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool
_LOGGER.error("gateway setup failed")
return False
if MYSENSORS_GATEWAYS not in hass.data:
hass.data[MYSENSORS_GATEWAYS] = {}
hass.data[MYSENSORS_GATEWAYS][entry.entry_id] = gateway
if DOMAIN not in hass.data:
hass.data[DOMAIN] = {}
if MYSENSORS_GATEWAYS not in hass.data[DOMAIN]:
hass.data[DOMAIN][MYSENSORS_GATEWAYS] = {}
hass.data[DOMAIN][MYSENSORS_GATEWAYS][entry.entry_id] = gateway
async def finish():
for platform in SUPPORTED_PLATFORMS_WITH_ENTRY_SUPPORT:
@@ -200,11 +203,11 @@ async def async_unload_entry(hass: HomeAssistantType, entry: ConfigEntry) -> boo
await hass.config_entries.async_forward_entry_unload(entry, platform)
key = MYSENSORS_ON_UNLOAD.format(entry.entry_id)
if key in hass.data:
for fnct in hass.data[key]:
if key in hass.data[DOMAIN]:
for fnct in hass.data[DOMAIN][key]:
fnct()
del hass.data[MYSENSORS_GATEWAYS][entry.entry_id]
del hass.data[DOMAIN][MYSENSORS_GATEWAYS][entry.entry_id]
hass.async_create_task(gw_stop(hass, gateway))
return True
@@ -222,9 +225,9 @@ async def on_unload(
else:
uniqueid = entry.entry_id
key = MYSENSORS_ON_UNLOAD.format(uniqueid)
if key not in hass.data:
hass.data[key] = []
hass.data[key].append(fnct)
if key not in hass.data[DOMAIN]:
hass.data[DOMAIN][key] = []
hass.data[DOMAIN][key].append(fnct)
@callback

View File

@@ -60,8 +60,8 @@ class MySensorsDevice:
"""Remove this entity from home assistant."""
for platform in PLATFORM_TYPES:
platform_str = MYSENSORS_PLATFORM_DEVICES.format(platform)
if platform_str in self.hass.data:
platform_dict = self.hass.data[platform_str]
if platform_str in self.hass.data[DOMAIN]:
platform_dict = self.hass.data[DOMAIN][platform_str]
if self.dev_id in platform_dict:
if platform_dict[self.dev_id] is not self:
self._logger.warning(
@@ -192,9 +192,9 @@ class MySensorsDevice:
def get_mysensors_devices(hass, domain: str) -> Dict[DevId, MySensorsDevice]:
"""Return MySensors devices for a hass platform name."""
if MYSENSORS_PLATFORM_DEVICES.format(domain) not in hass.data:
hass.data[MYSENSORS_PLATFORM_DEVICES.format(domain)] = {}
return hass.data[MYSENSORS_PLATFORM_DEVICES.format(domain)]
if MYSENSORS_PLATFORM_DEVICES.format(domain) not in hass.data[DOMAIN]:
hass.data[DOMAIN][MYSENSORS_PLATFORM_DEVICES.format(domain)] = {}
return hass.data[DOMAIN][MYSENSORS_PLATFORM_DEVICES.format(domain)]
class MySensorsEntity(MySensorsDevice, Entity):

View File

@@ -25,6 +25,7 @@ from .const import (
CONF_TOPIC_IN_PREFIX,
CONF_TOPIC_OUT_PREFIX,
CONF_VERSION,
DOMAIN,
MYSENSORS_GATEWAY_READY,
MYSENSORS_GATEWAY_START_TASK,
MYSENSORS_GATEWAYS,
@@ -62,9 +63,9 @@ def get_mysensors_gateway(
hass: HomeAssistantType, gateway_id: GatewayId
) -> Optional[BaseAsyncGateway]:
"""Return the Gateway for a given GatewayId."""
if MYSENSORS_GATEWAYS not in hass.data:
hass.data[MYSENSORS_GATEWAYS] = {}
gateways = hass.data.get(MYSENSORS_GATEWAYS)
if MYSENSORS_GATEWAYS not in hass.data[DOMAIN]:
hass.data[DOMAIN][MYSENSORS_GATEWAYS] = {}
gateways = hass.data[DOMAIN].get(MYSENSORS_GATEWAYS)
return gateways.get(gateway_id)
@@ -217,7 +218,7 @@ async def _discover_persistent_devices(
async def gw_stop(hass, gateway: BaseAsyncGateway):
"""Stop the gateway."""
_LOGGER.info("stopping gateway %s", gateway.entry_id)
connect_task = hass.data.get(
connect_task = hass.data[DOMAIN].get(
MYSENSORS_GATEWAY_START_TASK.format(gateway.entry_id), None
)
if connect_task is not None and not connect_task.done():
@@ -228,7 +229,7 @@ async def gw_stop(hass, gateway: BaseAsyncGateway):
async def _gw_start(hass: HomeAssistantType, gateway: BaseAsyncGateway):
"""Start the gateway."""
# Don't use hass.async_create_task to avoid holding up setup indefinitely.
hass.data[
hass.data[DOMAIN][
MYSENSORS_GATEWAY_START_TASK.format(gateway.entry_id)
] = asyncio.create_task(
gateway.start()
@@ -243,7 +244,7 @@ async def _gw_start(hass: HomeAssistantType, gateway: BaseAsyncGateway):
return
gateway_ready = asyncio.Future()
gateway_ready_key = MYSENSORS_GATEWAY_READY.format(gateway.entry_id)
hass.data[gateway_ready_key] = gateway_ready
hass.data[DOMAIN][gateway_ready_key] = gateway_ready
try:
with async_timeout.timeout(GATEWAY_READY_TIMEOUT):
@@ -255,7 +256,7 @@ async def _gw_start(hass: HomeAssistantType, gateway: BaseAsyncGateway):
GATEWAY_READY_TIMEOUT,
)
finally:
hass.data.pop(gateway_ready_key, None)
hass.data[DOMAIN].pop(gateway_ready_key, None)
def _gw_callback_factory(