mirror of
https://github.com/home-assistant/core.git
synced 2025-07-14 16:57:10 +00:00
Add automatic keep-alive for IKEA Trådfri (#41778)
* Added automatic keep-alive for IKEA Trådfri * Don't shut down factory on keep-alive error * Avoid keep-alive after shutdown Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io> Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
This commit is contained in:
parent
d16c04a8e0
commit
8be3f2f2a5
@ -1,14 +1,19 @@
|
|||||||
"""Support for IKEA Tradfri."""
|
"""Support for IKEA Tradfri."""
|
||||||
import asyncio
|
import asyncio
|
||||||
|
from datetime import timedelta
|
||||||
|
import logging
|
||||||
|
|
||||||
from pytradfri import Gateway, RequestError
|
from pytradfri import Gateway, RequestError
|
||||||
from pytradfri.api.aiocoap_api import APIFactory
|
from pytradfri.api.aiocoap_api import APIFactory
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant import config_entries
|
from homeassistant import config_entries
|
||||||
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
|
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
|
||||||
from homeassistant.exceptions import ConfigEntryNotReady
|
from homeassistant.exceptions import ConfigEntryNotReady
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
from homeassistant.helpers.event import async_track_time_interval
|
||||||
|
from homeassistant.helpers.typing import ConfigType, HomeAssistantType
|
||||||
from homeassistant.util.json import load_json
|
from homeassistant.util.json import load_json
|
||||||
|
|
||||||
from . import config_flow # noqa: F401
|
from . import config_flow # noqa: F401
|
||||||
@ -31,6 +36,8 @@ from .const import (
|
|||||||
PLATFORMS,
|
PLATFORMS,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
FACTORY = "tradfri_factory"
|
FACTORY = "tradfri_factory"
|
||||||
LISTENERS = "tradfri_listeners"
|
LISTENERS = "tradfri_listeners"
|
||||||
|
|
||||||
@ -49,7 +56,7 @@ CONFIG_SCHEMA = vol.Schema(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup(hass, config):
|
async def async_setup(hass: HomeAssistantType, config: ConfigType):
|
||||||
"""Set up the Tradfri component."""
|
"""Set up the Tradfri component."""
|
||||||
conf = config.get(DOMAIN)
|
conf = config.get(DOMAIN)
|
||||||
|
|
||||||
@ -94,7 +101,7 @@ async def async_setup(hass, config):
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass, entry):
|
async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry):
|
||||||
"""Create a gateway."""
|
"""Create a gateway."""
|
||||||
# host, identity, key, allow_tradfri_groups
|
# host, identity, key, allow_tradfri_groups
|
||||||
tradfri_data = hass.data.setdefault(DOMAIN, {})[entry.entry_id] = {}
|
tradfri_data = hass.data.setdefault(DOMAIN, {})[entry.entry_id] = {}
|
||||||
@ -147,10 +154,23 @@ async def async_setup_entry(hass, entry):
|
|||||||
hass.config_entries.async_forward_entry_setup(entry, component)
|
hass.config_entries.async_forward_entry_setup(entry, component)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
async def async_keep_alive(now):
|
||||||
|
if hass.is_stopping:
|
||||||
|
return
|
||||||
|
|
||||||
|
try:
|
||||||
|
await api(gateway.get_gateway_info())
|
||||||
|
except RequestError:
|
||||||
|
_LOGGER.error("Keep-alive failed")
|
||||||
|
|
||||||
|
listeners.append(
|
||||||
|
async_track_time_interval(hass, async_keep_alive, timedelta(seconds=60))
|
||||||
|
)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
async def async_unload_entry(hass, entry):
|
async def async_unload_entry(hass: HomeAssistantType, entry: ConfigEntry):
|
||||||
"""Unload a config entry."""
|
"""Unload a config entry."""
|
||||||
unload_ok = all(
|
unload_ok = all(
|
||||||
await asyncio.gather(
|
await asyncio.gather(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user