mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 06:07:17 +00:00
Migrate harmony to use entry.runtime_data (#122312)
This commit is contained in:
parent
272f0bc21c
commit
30373a668c
@ -3,26 +3,18 @@
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
from homeassistant.components.remote import ATTR_ACTIVITY, ATTR_DELAY_SECS
|
from homeassistant.components.remote import ATTR_ACTIVITY, ATTR_DELAY_SECS
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import CONF_HOST, CONF_NAME, EVENT_HOMEASSISTANT_STOP
|
from homeassistant.const import CONF_HOST, CONF_NAME, EVENT_HOMEASSISTANT_STOP
|
||||||
from homeassistant.core import Event, HomeAssistant, callback
|
from homeassistant.core import Event, HomeAssistant, callback
|
||||||
from homeassistant.helpers import entity_registry as er
|
from homeassistant.helpers import entity_registry as er
|
||||||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||||
|
|
||||||
from .const import (
|
from .const import DOMAIN, HARMONY_OPTIONS_UPDATE, PLATFORMS # noqa: F401
|
||||||
CANCEL_LISTENER,
|
from .data import HarmonyConfigEntry, HarmonyData
|
||||||
CANCEL_STOP,
|
|
||||||
DOMAIN,
|
|
||||||
HARMONY_DATA,
|
|
||||||
HARMONY_OPTIONS_UPDATE,
|
|
||||||
PLATFORMS,
|
|
||||||
)
|
|
||||||
from .data import HarmonyData
|
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_setup_entry(hass: HomeAssistant, entry: HarmonyConfigEntry) -> bool:
|
||||||
"""Set up Logitech Harmony Hub from a config entry."""
|
"""Set up Logitech Harmony Hub from a config entry."""
|
||||||
# As there currently is no way to import options from yaml
|
# As there currently is no way to import options from yaml
|
||||||
# when setting up a config entry, we fallback to adding
|
# when setting up a config entry, we fallback to adding
|
||||||
@ -37,19 +29,16 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
|
|
||||||
await _migrate_old_unique_ids(hass, entry.entry_id, data)
|
await _migrate_old_unique_ids(hass, entry.entry_id, data)
|
||||||
|
|
||||||
cancel_listener = entry.add_update_listener(_update_listener)
|
entry.async_on_unload(entry.add_update_listener(_update_listener))
|
||||||
|
|
||||||
async def _async_on_stop(event: Event) -> None:
|
async def _async_on_stop(event: Event) -> None:
|
||||||
await data.shutdown()
|
await data.shutdown()
|
||||||
|
|
||||||
cancel_stop = hass.bus.async_listen(EVENT_HOMEASSISTANT_STOP, _async_on_stop)
|
entry.async_on_unload(
|
||||||
|
hass.bus.async_listen(EVENT_HOMEASSISTANT_STOP, _async_on_stop)
|
||||||
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = {
|
)
|
||||||
HARMONY_DATA: data,
|
|
||||||
CANCEL_LISTENER: cancel_listener,
|
|
||||||
CANCEL_STOP: cancel_stop,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
entry.runtime_data = data
|
||||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
@ -84,7 +73,7 @@ async def _migrate_old_unique_ids(
|
|||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _async_import_options_from_data_if_missing(
|
def _async_import_options_from_data_if_missing(
|
||||||
hass: HomeAssistant, entry: ConfigEntry
|
hass: HomeAssistant, entry: HarmonyConfigEntry
|
||||||
) -> None:
|
) -> None:
|
||||||
options = dict(entry.options)
|
options = dict(entry.options)
|
||||||
modified = 0
|
modified = 0
|
||||||
@ -97,24 +86,16 @@ def _async_import_options_from_data_if_missing(
|
|||||||
hass.config_entries.async_update_entry(entry, options=options)
|
hass.config_entries.async_update_entry(entry, options=options)
|
||||||
|
|
||||||
|
|
||||||
async def _update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
|
async def _update_listener(hass: HomeAssistant, entry: HarmonyConfigEntry) -> None:
|
||||||
"""Handle options update."""
|
"""Handle options update."""
|
||||||
async_dispatcher_send(
|
async_dispatcher_send(
|
||||||
hass, f"{HARMONY_OPTIONS_UPDATE}-{entry.unique_id}", entry.options
|
hass, f"{HARMONY_OPTIONS_UPDATE}-{entry.unique_id}", entry.options
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_unload_entry(hass: HomeAssistant, entry: HarmonyConfigEntry) -> bool:
|
||||||
"""Unload a config entry."""
|
"""Unload a config entry."""
|
||||||
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||||
|
|
||||||
# Shutdown a harmony remote for removal
|
# Shutdown a harmony remote for removal
|
||||||
entry_data = hass.data[DOMAIN][entry.entry_id]
|
await entry.runtime_data.shutdown()
|
||||||
entry_data[CANCEL_LISTENER]()
|
|
||||||
entry_data[CANCEL_STOP]()
|
|
||||||
await entry_data[HARMONY_DATA].shutdown()
|
|
||||||
|
|
||||||
if unload_ok:
|
|
||||||
hass.data[DOMAIN].pop(entry.entry_id)
|
|
||||||
|
|
||||||
return unload_ok
|
return unload_ok
|
||||||
|
@ -27,7 +27,8 @@ from homeassistant.const import CONF_HOST, CONF_NAME
|
|||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
from homeassistant.exceptions import HomeAssistantError
|
from homeassistant.exceptions import HomeAssistantError
|
||||||
|
|
||||||
from .const import DOMAIN, HARMONY_DATA, PREVIOUS_ACTIVE_ACTIVITY, UNIQUE_ID
|
from .const import DOMAIN, PREVIOUS_ACTIVE_ACTIVITY, UNIQUE_ID
|
||||||
|
from .data import HarmonyConfigEntry
|
||||||
from .util import (
|
from .util import (
|
||||||
find_best_name_for_remote,
|
find_best_name_for_remote,
|
||||||
find_unique_id_for_remote,
|
find_unique_id_for_remote,
|
||||||
@ -185,7 +186,7 @@ def _options_from_user_input(user_input: dict[str, Any]) -> dict[str, Any]:
|
|||||||
class OptionsFlowHandler(OptionsFlow):
|
class OptionsFlowHandler(OptionsFlow):
|
||||||
"""Handle a option flow for Harmony."""
|
"""Handle a option flow for Harmony."""
|
||||||
|
|
||||||
def __init__(self, config_entry: ConfigEntry) -> None:
|
def __init__(self, config_entry: HarmonyConfigEntry) -> None:
|
||||||
"""Initialize options flow."""
|
"""Initialize options flow."""
|
||||||
self.config_entry = config_entry
|
self.config_entry = config_entry
|
||||||
|
|
||||||
@ -196,8 +197,7 @@ class OptionsFlowHandler(OptionsFlow):
|
|||||||
if user_input is not None:
|
if user_input is not None:
|
||||||
return self.async_create_entry(title="", data=user_input)
|
return self.async_create_entry(title="", data=user_input)
|
||||||
|
|
||||||
remote = self.hass.data[DOMAIN][self.config_entry.entry_id][HARMONY_DATA]
|
remote = self.config_entry.runtime_data
|
||||||
|
|
||||||
data_schema = vol.Schema(
|
data_schema = vol.Schema(
|
||||||
{
|
{
|
||||||
vol.Optional(
|
vol.Optional(
|
||||||
|
@ -13,8 +13,3 @@ ATTR_DEVICES_LIST = "devices_list"
|
|||||||
ATTR_LAST_ACTIVITY = "last_activity"
|
ATTR_LAST_ACTIVITY = "last_activity"
|
||||||
ATTR_ACTIVITY_STARTING = "activity_starting"
|
ATTR_ACTIVITY_STARTING = "activity_starting"
|
||||||
PREVIOUS_ACTIVE_ACTIVITY = "Previous Active Activity"
|
PREVIOUS_ACTIVE_ACTIVITY = "Previous Active Activity"
|
||||||
|
|
||||||
|
|
||||||
HARMONY_DATA = "harmony_data"
|
|
||||||
CANCEL_LISTENER = "cancel_listener"
|
|
||||||
CANCEL_STOP = "cancel_stop"
|
|
||||||
|
@ -9,6 +9,7 @@ from aioharmony.const import ClientCallbackType, SendCommandDevice
|
|||||||
import aioharmony.exceptions as aioexc
|
import aioharmony.exceptions as aioexc
|
||||||
from aioharmony.harmonyapi import HarmonyAPI as HarmonyClient
|
from aioharmony.harmonyapi import HarmonyAPI as HarmonyClient
|
||||||
|
|
||||||
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import ConfigEntryNotReady
|
from homeassistant.exceptions import ConfigEntryNotReady
|
||||||
from homeassistant.helpers.device_registry import DeviceInfo
|
from homeassistant.helpers.device_registry import DeviceInfo
|
||||||
@ -19,6 +20,9 @@ from .subscriber import HarmonySubscriberMixin
|
|||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
type HarmonyConfigEntry = ConfigEntry[HarmonyData]
|
||||||
|
|
||||||
|
|
||||||
class HarmonyData(HarmonySubscriberMixin):
|
class HarmonyData(HarmonySubscriberMixin):
|
||||||
"""HarmonyData registers for Harmony hub updates."""
|
"""HarmonyData registers for Harmony hub updates."""
|
||||||
|
|
||||||
|
@ -19,7 +19,6 @@ from homeassistant.components.remote import (
|
|||||||
RemoteEntity,
|
RemoteEntity,
|
||||||
RemoteEntityFeature,
|
RemoteEntityFeature,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.core import HassJob, HomeAssistant, callback
|
from homeassistant.core import HassJob, HomeAssistant, callback
|
||||||
from homeassistant.helpers import entity_platform
|
from homeassistant.helpers import entity_platform
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
@ -34,13 +33,12 @@ from .const import (
|
|||||||
ATTR_DEVICES_LIST,
|
ATTR_DEVICES_LIST,
|
||||||
ATTR_LAST_ACTIVITY,
|
ATTR_LAST_ACTIVITY,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
HARMONY_DATA,
|
|
||||||
HARMONY_OPTIONS_UPDATE,
|
HARMONY_OPTIONS_UPDATE,
|
||||||
PREVIOUS_ACTIVE_ACTIVITY,
|
PREVIOUS_ACTIVE_ACTIVITY,
|
||||||
SERVICE_CHANGE_CHANNEL,
|
SERVICE_CHANGE_CHANNEL,
|
||||||
SERVICE_SYNC,
|
SERVICE_SYNC,
|
||||||
)
|
)
|
||||||
from .data import HarmonyData
|
from .data import HarmonyConfigEntry, HarmonyData
|
||||||
from .entity import HarmonyEntity
|
from .entity import HarmonyEntity
|
||||||
from .subscriber import HarmonyCallback
|
from .subscriber import HarmonyCallback
|
||||||
|
|
||||||
@ -57,11 +55,12 @@ HARMONY_CHANGE_CHANNEL_SCHEMA: VolDictType = {
|
|||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
hass: HomeAssistant,
|
||||||
|
entry: HarmonyConfigEntry,
|
||||||
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the Harmony config entry."""
|
"""Set up the Harmony config entry."""
|
||||||
|
data = entry.runtime_data
|
||||||
data: HarmonyData = hass.data[DOMAIN][entry.entry_id][HARMONY_DATA]
|
|
||||||
|
|
||||||
_LOGGER.debug("HarmonyData : %s", data)
|
_LOGGER.debug("HarmonyData : %s", data)
|
||||||
|
|
||||||
|
@ -5,12 +5,11 @@ from __future__ import annotations
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
from homeassistant.components.select import SelectEntity
|
from homeassistant.components.select import SelectEntity
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.core import HassJob, HomeAssistant, callback
|
from homeassistant.core import HassJob, HomeAssistant, callback
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from .const import ACTIVITY_POWER_OFF, DOMAIN, HARMONY_DATA
|
from .const import ACTIVITY_POWER_OFF, DOMAIN
|
||||||
from .data import HarmonyData
|
from .data import HarmonyConfigEntry, HarmonyData
|
||||||
from .entity import HarmonyEntity
|
from .entity import HarmonyEntity
|
||||||
from .subscriber import HarmonyCallback
|
from .subscriber import HarmonyCallback
|
||||||
|
|
||||||
@ -20,11 +19,12 @@ TRANSLATABLE_POWER_OFF = "power_off"
|
|||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
hass: HomeAssistant,
|
||||||
|
entry: HarmonyConfigEntry,
|
||||||
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up harmony activities select."""
|
"""Set up harmony activities select."""
|
||||||
data: HarmonyData = hass.data[DOMAIN][entry.entry_id][HARMONY_DATA]
|
async_add_entities([HarmonyActivitySelect(entry.runtime_data)])
|
||||||
async_add_entities([HarmonyActivitySelect(data)])
|
|
||||||
|
|
||||||
|
|
||||||
class HarmonyActivitySelect(HarmonyEntity, SelectEntity):
|
class HarmonyActivitySelect(HarmonyEntity, SelectEntity):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user