Bump pytradfri to 7.0.0, support multiple gateways (#39609)

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
Paulus Schoutsen 2020-09-03 14:47:14 +02:00 committed by GitHub
parent 8ca9724258
commit 948ec80b6e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 41 additions and 14 deletions

View File

@ -1,4 +1,5 @@
"""Support for IKEA Tradfri."""
import asyncio
import logging
from pytradfri import Gateway, RequestError
@ -27,11 +28,13 @@ from .const import (
DOMAIN,
KEY_API,
KEY_GATEWAY,
TRADFRI_DEVICE_TYPES,
PLATFORMS,
)
_LOGGER = logging.getLogger(__name__)
FACTORY = "tradfri_factory"
CONFIG_SCHEMA = vol.Schema(
{
DOMAIN: vol.Schema(
@ -96,7 +99,7 @@ async def async_setup_entry(hass, entry):
"""Create a gateway."""
# host, identity, key, allow_tradfri_groups
factory = APIFactory(
factory = await APIFactory.init(
entry.data[CONF_HOST],
psk_id=entry.data[CONF_IDENTITY],
psk=entry.data[CONF_KEY],
@ -119,6 +122,8 @@ async def async_setup_entry(hass, entry):
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[FACTORY] = factory
dev_reg = await hass.helpers.device_registry.async_get_registry()
dev_reg.async_get_or_create(
@ -132,9 +137,29 @@ async def async_setup_entry(hass, entry):
sw_version=gateway_info.firmware_version,
)
for device in TRADFRI_DEVICE_TYPES:
for component in PLATFORMS:
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(entry, device)
hass.config_entries.async_forward_entry_setup(entry, component)
)
return True
async def async_unload_entry(hass, entry):
"""Unload a config entry."""
unload_ok = all(
await asyncio.gather(
*[
hass.config_entries.async_forward_entry_unload(entry, component)
for component in PLATFORMS
]
)
)
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()
return unload_ok

View File

@ -90,7 +90,7 @@ class FlowHandler(config_entries.ConfigFlow):
host = discovery_info["host"]
for entry in self._async_current_entries():
if entry.data[CONF_HOST] != host:
if entry.data.get(CONF_HOST) != host:
continue
# Backwards compat, we update old entries
@ -107,7 +107,7 @@ class FlowHandler(config_entries.ConfigFlow):
async def async_step_import(self, user_input):
"""Import a config entry."""
for entry in self._async_current_entries():
if entry.data[CONF_HOST] == user_input["host"]:
if entry.data.get(CONF_HOST) == user_input["host"]:
return self.async_abort(reason="already_configured")
# Happens if user has host directly in configuration.yaml
@ -141,8 +141,8 @@ class FlowHandler(config_entries.ConfigFlow):
same_hub_entries = [
entry.entry_id
for entry in self._async_current_entries()
if entry.data[CONF_GATEWAY_ID] == gateway_id
or entry.data[CONF_HOST] == host
if entry.data.get(CONF_GATEWAY_ID) == gateway_id
or entry.data.get(CONF_HOST) == host
]
if same_hub_entries:
@ -161,7 +161,7 @@ async def authenticate(hass, host, security_code):
identity = uuid4().hex
api_factory = APIFactory(host, psk_id=identity)
api_factory = await APIFactory.init(host, psk_id=identity)
try:
with async_timeout.timeout(5):
@ -170,6 +170,8 @@ async def authenticate(hass, host, security_code):
raise AuthError("invalid_security_code") from err
except asyncio.TimeoutError as err:
raise AuthError("timeout") from err
finally:
await api_factory.shutdown()
return await get_gateway_info(hass, host, identity, key)
@ -178,7 +180,7 @@ async def get_gateway_info(hass, host, identity, key):
"""Return info for the gateway."""
try:
factory = APIFactory(host, psk_id=identity, psk=key)
factory = await APIFactory.init(host, psk_id=identity, psk=key)
api = factory.request
gateway = Gateway()

View File

@ -23,4 +23,4 @@ KEY_GATEWAY = "tradfri_gateway"
KEY_SECURITY_CODE = "security_code"
SUPPORTED_GROUP_FEATURES = SUPPORT_BRIGHTNESS | SUPPORT_TRANSITION
SUPPORTED_LIGHT_FEATURES = SUPPORT_TRANSITION
TRADFRI_DEVICE_TYPES = ["cover", "light", "sensor", "switch"]
PLATFORMS = ["cover", "light", "sensor", "switch"]

View File

@ -3,7 +3,7 @@
"name": "IKEA TRÅDFRI",
"config_flow": true,
"documentation": "https://www.home-assistant.io/integrations/tradfri",
"requirements": ["pytradfri[async]==6.4.0"],
"requirements": ["pytradfri[async]==7.0.0"],
"homekit": {
"models": ["TRADFRI"]
},

View File

@ -1805,7 +1805,7 @@ pytraccar==0.9.0
pytrackr==0.0.5
# homeassistant.components.tradfri
pytradfri[async]==6.4.0
pytradfri[async]==7.0.0
# homeassistant.components.trafikverket_train
# homeassistant.components.trafikverket_weatherstation

View File

@ -848,7 +848,7 @@ pytile==4.0.0
pytraccar==0.9.0
# homeassistant.components.tradfri
pytradfri[async]==6.4.0
pytradfri[async]==7.0.0
# homeassistant.components.vera
pyvera==0.3.9