Clean tradfri hass data and add tests (#39620)

This commit is contained in:
Martin Hjelmare authored and GitHub committed 2020-09-03 18:39:24 +02:00
1 parent d128443a2a
commit bde0bdbf80
9 files changed
+153 -64

No files matched your search

+9 -9
View File
@@ -1,6 +1,5 @@
"""Support for IKEA Tradfri."""
import asyncio
import logging
from pytradfri import Gateway, RequestError
from pytradfri.api.aiocoap_api import APIFactory
@@ -31,9 +30,8 @@ from .const import (
PLATFORMS,
)
_LOGGER = logging.getLogger(__name__)
FACTORY = "tradfri_factory"
LISTENERS = "tradfri_listeners"
CONFIG_SCHEMA = vol.Schema(
{
@@ -98,6 +96,8 @@ async def async_setup(hass, config):
async def async_setup_entry(hass, entry):
"""Create a gateway."""
# host, identity, key, allow_tradfri_groups
tradfri_data = hass.data.setdefault(DOMAIN, {})[entry.entry_id] = {}
listeners = tradfri_data[LISTENERS] = []
factory = await APIFactory.init(
entry.data[CONF_HOST],
@@ -109,7 +109,7 @@ async def async_setup_entry(hass, entry):
"""Close connection when hass stops."""
await factory.shutdown()
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, on_hass_stop)
listeners.append(hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, on_hass_stop))
api = factory.request
gateway = Gateway()
@@ -120,9 +120,8 @@ async def async_setup_entry(hass, entry):
await factory.shutdown()
raise ConfigEntryNotReady from err
hass.data.setdefault(KEY_API, {})[entry.entry_id] = api
hass.data.setdefault(KEY_GATEWAY, {})[entry.entry_id] = gateway
tradfri_data = hass.data.setdefault(DOMAIN, {})[entry.entry_id] = {}
tradfri_data[KEY_API] = api
tradfri_data[KEY_GATEWAY] = gateway
tradfri_data[FACTORY] = factory
dev_reg = await hass.helpers.device_registry.async_get_registry()
@@ -156,10 +155,11 @@ async def async_unload_entry(hass, entry):
)
)
if unload_ok:
hass.data[KEY_API].pop(entry.entry_id)
hass.data[KEY_GATEWAY].pop(entry.entry_id)
tradfri_data = hass.data[DOMAIN].pop(entry.entry_id)
factory = tradfri_data[FACTORY]
await factory.shutdown()
# unsubscribe listeners
for listener in tradfri_data[LISTENERS]:
listener()
return unload_ok
+4 -3
View File
@@ -3,14 +3,15 @@
from homeassistant.components.cover import ATTR_POSITION, CoverEntity
from .base_class import TradfriBaseDevice
from .const import ATTR_MODEL, CONF_GATEWAY_ID, KEY_API, KEY_GATEWAY
from .const import ATTR_MODEL, CONF_GATEWAY_ID, DOMAIN, KEY_API, KEY_GATEWAY
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Load Tradfri covers based on a config entry."""
gateway_id = config_entry.data[CONF_GATEWAY_ID]
api = hass.data[KEY_API][config_entry.entry_id]
gateway = hass.data[KEY_GATEWAY][config_entry.entry_id]
tradfri_data = hass.data[DOMAIN][config_entry.entry_id]
api = tradfri_data[KEY_API]
gateway = tradfri_data[KEY_GATEWAY]
devices_commands = await api(gateway.get_devices())
devices = await api(devices_commands)
+4 -2
View File
@@ -21,6 +21,7 @@ from .const import (
ATTR_TRANSITION_TIME,
CONF_GATEWAY_ID,
CONF_IMPORT_GROUPS,
DOMAIN,
KEY_API,
KEY_GATEWAY,
SUPPORTED_GROUP_FEATURES,
@@ -33,8 +34,9 @@ _LOGGER = logging.getLogger(__name__)
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Load Tradfri lights based on a config entry."""
gateway_id = config_entry.data[CONF_GATEWAY_ID]
api = hass.data[KEY_API][config_entry.entry_id]
gateway = hass.data[KEY_GATEWAY][config_entry.entry_id]
tradfri_data = hass.data[DOMAIN][config_entry.entry_id]
api = tradfri_data[KEY_API]
gateway = tradfri_data[KEY_GATEWAY]
devices_commands = await api(gateway.get_devices())
devices = await api(devices_commands)
+4 -3
View File
@@ -3,14 +3,15 @@
from homeassistant.const import DEVICE_CLASS_BATTERY, UNIT_PERCENTAGE
from .base_class import TradfriBaseDevice
from .const import CONF_GATEWAY_ID, KEY_API, KEY_GATEWAY
from .const import CONF_GATEWAY_ID, DOMAIN, KEY_API, KEY_GATEWAY
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up a Tradfri config entry."""
gateway_id = config_entry.data[CONF_GATEWAY_ID]
api = hass.data[KEY_API][config_entry.entry_id]
gateway = hass.data[KEY_GATEWAY][config_entry.entry_id]
tradfri_data = hass.data[DOMAIN][config_entry.entry_id]
api = tradfri_data[KEY_API]
gateway = tradfri_data[KEY_GATEWAY]
devices_commands = await api(gateway.get_devices())
all_devices = await api(devices_commands)
+4 -3
View File
@@ -2,14 +2,15 @@
from homeassistant.components.switch import SwitchEntity
from .base_class import TradfriBaseDevice
from .const import CONF_GATEWAY_ID, KEY_API, KEY_GATEWAY
from .const import CONF_GATEWAY_ID, DOMAIN, KEY_API, KEY_GATEWAY
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Load Tradfri switches based on a config entry."""
gateway_id = config_entry.data[CONF_GATEWAY_ID]
api = hass.data[KEY_API][config_entry.entry_id]
gateway = hass.data[KEY_GATEWAY][config_entry.entry_id]
tradfri_data = hass.data[DOMAIN][config_entry.entry_id]
api = tradfri_data[KEY_API]
gateway = tradfri_data[KEY_GATEWAY]
devices_commands = await api(gateway.get_devices())
devices = await api(devices_commands)