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") _LOGGER.error("gateway setup failed")
return False return False
if MYSENSORS_GATEWAYS not in hass.data: if DOMAIN not in hass.data:
hass.data[MYSENSORS_GATEWAYS] = {} hass.data[DOMAIN] = {}
hass.data[MYSENSORS_GATEWAYS][entry.entry_id] = gateway
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(): async def finish():
for platform in SUPPORTED_PLATFORMS_WITH_ENTRY_SUPPORT: 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) await hass.config_entries.async_forward_entry_unload(entry, platform)
key = MYSENSORS_ON_UNLOAD.format(entry.entry_id) key = MYSENSORS_ON_UNLOAD.format(entry.entry_id)
if key in hass.data: if key in hass.data[DOMAIN]:
for fnct in hass.data[key]: for fnct in hass.data[DOMAIN][key]:
fnct() 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)) hass.async_create_task(gw_stop(hass, gateway))
return True return True
@@ -222,9 +225,9 @@ async def on_unload(
else: else:
uniqueid = entry.entry_id uniqueid = entry.entry_id
key = MYSENSORS_ON_UNLOAD.format(uniqueid) key = MYSENSORS_ON_UNLOAD.format(uniqueid)
if key not in hass.data: if key not in hass.data[DOMAIN]:
hass.data[key] = [] hass.data[DOMAIN][key] = []
hass.data[key].append(fnct) hass.data[DOMAIN][key].append(fnct)
@callback @callback

View File

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

View File

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