mirror of
https://github.com/home-assistant/core.git
synced 2026-05-08 09:38:58 +01:00
Migrate monoprice to runtime_data (#164604)
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
"""The Monoprice 6-Zone Amplifier integration."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
import logging
|
||||
|
||||
from pymonoprice import get_monoprice
|
||||
from pymonoprice import Monoprice, get_monoprice
|
||||
from serial import SerialException
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
@@ -10,14 +13,24 @@ from homeassistant.const import CONF_PORT, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ConfigEntryNotReady
|
||||
|
||||
from .const import CONF_NOT_FIRST_RUN, DOMAIN, FIRST_RUN, MONOPRICE_OBJECT
|
||||
from .const import CONF_NOT_FIRST_RUN
|
||||
|
||||
PLATFORMS = [Platform.MEDIA_PLAYER]
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
type MonopriceConfigEntry = ConfigEntry[MonopriceRuntimeData]
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
|
||||
@dataclass
|
||||
class MonopriceRuntimeData:
|
||||
"""Data stored in the config entry for a Monoprice entry."""
|
||||
|
||||
client: Monoprice
|
||||
first_run: bool
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: MonopriceConfigEntry) -> bool:
|
||||
"""Set up Monoprice 6-Zone Amplifier from a config entry."""
|
||||
port = entry.data[CONF_PORT]
|
||||
|
||||
@@ -37,17 +50,17 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
|
||||
entry.async_on_unload(entry.add_update_listener(_update_listener))
|
||||
|
||||
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = {
|
||||
MONOPRICE_OBJECT: monoprice,
|
||||
FIRST_RUN: first_run,
|
||||
}
|
||||
entry.runtime_data = MonopriceRuntimeData(
|
||||
client=monoprice,
|
||||
first_run=first_run,
|
||||
)
|
||||
|
||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: MonopriceConfigEntry) -> bool:
|
||||
"""Unload a config entry."""
|
||||
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
if not unload_ok:
|
||||
@@ -61,10 +74,7 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
"""
|
||||
del monoprice
|
||||
|
||||
monoprice = hass.data[DOMAIN][entry.entry_id][MONOPRICE_OBJECT]
|
||||
hass.data[DOMAIN].pop(entry.entry_id)
|
||||
|
||||
await hass.async_add_executor_job(_cleanup, monoprice)
|
||||
await hass.async_add_executor_job(_cleanup, entry.runtime_data.client)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
@@ -15,6 +15,3 @@ CONF_NOT_FIRST_RUN = "not_first_run"
|
||||
|
||||
SERVICE_SNAPSHOT = "snapshot"
|
||||
SERVICE_RESTORE = "restore"
|
||||
|
||||
FIRST_RUN = "first_run"
|
||||
MONOPRICE_OBJECT = "monoprice_object"
|
||||
|
||||
@@ -11,21 +11,14 @@ from homeassistant.components.media_player import (
|
||||
MediaPlayerEntityFeature,
|
||||
MediaPlayerState,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_PORT
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import config_validation as cv, entity_platform, service
|
||||
from homeassistant.helpers.device_registry import DeviceInfo
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
|
||||
from .const import (
|
||||
CONF_SOURCES,
|
||||
DOMAIN,
|
||||
FIRST_RUN,
|
||||
MONOPRICE_OBJECT,
|
||||
SERVICE_RESTORE,
|
||||
SERVICE_SNAPSHOT,
|
||||
)
|
||||
from . import MonopriceConfigEntry
|
||||
from .const import CONF_SOURCES, DOMAIN, SERVICE_RESTORE, SERVICE_SNAPSHOT
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -57,13 +50,13 @@ def _get_sources(config_entry):
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: MonopriceConfigEntry,
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up the Monoprice 6-zone amplifier platform."""
|
||||
port = config_entry.data[CONF_PORT]
|
||||
|
||||
monoprice = hass.data[DOMAIN][config_entry.entry_id][MONOPRICE_OBJECT]
|
||||
monoprice = config_entry.runtime_data.client
|
||||
|
||||
sources = _get_sources(config_entry)
|
||||
|
||||
@@ -77,8 +70,7 @@ async def async_setup_entry(
|
||||
)
|
||||
|
||||
# only call update before add if it's the first run so we can try to detect zones
|
||||
first_run = hass.data[DOMAIN][config_entry.entry_id][FIRST_RUN]
|
||||
async_add_entities(entities, first_run)
|
||||
async_add_entities(entities, config_entry.runtime_data.first_run)
|
||||
|
||||
platform = entity_platform.async_get_current_platform()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user