Migrate anthemav to use runtime_data (#125537)

This commit is contained in:
epenet 2024-09-08 22:04:34 +02:00 committed by GitHub
parent 4bcde36a97
commit 6f88b6e64e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 15 deletions

View File

@ -13,14 +13,16 @@ from homeassistant.core import Event, HomeAssistant, callback
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.dispatcher import async_dispatcher_send
from .const import ANTHEMAV_UPDATE_SIGNAL, DEVICE_TIMEOUT_SECONDS, DOMAIN
from .const import ANTHEMAV_UPDATE_SIGNAL, DEVICE_TIMEOUT_SECONDS
type AnthemavConfigEntry = ConfigEntry[anthemav.Connection]
PLATFORMS = [Platform.MEDIA_PLAYER]
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_setup_entry(hass: HomeAssistant, entry: AnthemavConfigEntry) -> bool:
"""Set up Anthem A/V Receivers from a config entry."""
@callback
@ -41,7 +43,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
except (OSError, DeviceError) as err:
raise ConfigEntryNotReady from err
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = avr
entry.runtime_data = avr
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
@ -56,16 +58,12 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_unload_entry(hass: HomeAssistant, entry: AnthemavConfigEntry) -> bool:
"""Unload a config entry."""
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
avr = hass.data[DOMAIN][entry.entry_id]
avr = entry.runtime_data
_LOGGER.debug("Close avr connection")
avr.close()
if avr is not None:
_LOGGER.debug("Close avr connection")
avr.close()
if unload_ok:
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok

View File

@ -4,7 +4,6 @@ from __future__ import annotations
import logging
from anthemav.connection import Connection
from anthemav.protocol import AVR
from homeassistant.components.media_player import (
@ -13,13 +12,13 @@ from homeassistant.components.media_player import (
MediaPlayerEntityFeature,
MediaPlayerState,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_MAC, CONF_MODEL
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import AnthemavConfigEntry
from .const import ANTHEMAV_UPDATE_SIGNAL, DOMAIN, MANUFACTURER
_LOGGER = logging.getLogger(__name__)
@ -27,7 +26,7 @@ _LOGGER = logging.getLogger(__name__)
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: AnthemavConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up entry."""
@ -35,7 +34,7 @@ async def async_setup_entry(
mac_address = config_entry.data[CONF_MAC]
model = config_entry.data[CONF_MODEL]
avr: Connection = hass.data[DOMAIN][config_entry.entry_id]
avr = config_entry.runtime_data
_LOGGER.debug("Connection data dump: %s", avr.dump_conndata)