mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 18:27:09 +00:00
Convert arcam_fmj to runtime data (#122047)
This commit is contained in:
parent
9970b7eece
commit
8d290dd9d6
@ -13,17 +13,17 @@ from homeassistant.const import CONF_HOST, CONF_PORT, Platform
|
|||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||||
from homeassistant.helpers.typing import ConfigType
|
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
DEFAULT_SCAN_INTERVAL,
|
DEFAULT_SCAN_INTERVAL,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
DOMAIN_DATA_ENTRIES,
|
|
||||||
SIGNAL_CLIENT_DATA,
|
SIGNAL_CLIENT_DATA,
|
||||||
SIGNAL_CLIENT_STARTED,
|
SIGNAL_CLIENT_STARTED,
|
||||||
SIGNAL_CLIENT_STOPPED,
|
SIGNAL_CLIENT_STOPPED,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type ArcamFmjConfigEntry = ConfigEntry[Client]
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
CONFIG_SCHEMA = cv.removed(DOMAIN, raise_if_present=False)
|
CONFIG_SCHEMA = cv.removed(DOMAIN, raise_if_present=False)
|
||||||
@ -31,34 +31,21 @@ CONFIG_SCHEMA = cv.removed(DOMAIN, raise_if_present=False)
|
|||||||
PLATFORMS = [Platform.MEDIA_PLAYER]
|
PLATFORMS = [Platform.MEDIA_PLAYER]
|
||||||
|
|
||||||
|
|
||||||
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
async def async_setup_entry(hass: HomeAssistant, entry: ArcamFmjConfigEntry) -> bool:
|
||||||
"""Set up the component."""
|
|
||||||
hass.data[DOMAIN_DATA_ENTRIES] = {}
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
||||||
"""Set up config entry."""
|
"""Set up config entry."""
|
||||||
entries = hass.data[DOMAIN_DATA_ENTRIES]
|
entry.runtime_data = Client(entry.data[CONF_HOST], entry.data[CONF_PORT])
|
||||||
|
|
||||||
client = Client(entry.data[CONF_HOST], entry.data[CONF_PORT])
|
|
||||||
entries[entry.entry_id] = client
|
|
||||||
|
|
||||||
entry.async_create_background_task(
|
entry.async_create_background_task(
|
||||||
hass, _run_client(hass, client, DEFAULT_SCAN_INTERVAL), "arcam_fmj"
|
hass, _run_client(hass, entry.runtime_data, DEFAULT_SCAN_INTERVAL), "arcam_fmj"
|
||||||
)
|
)
|
||||||
|
|
||||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
"""Cleanup before removing config entry."""
|
"""Cleanup before removing config entry."""
|
||||||
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||||
hass.data[DOMAIN_DATA_ENTRIES].pop(entry.entry_id)
|
|
||||||
|
|
||||||
return unload_ok
|
|
||||||
|
|
||||||
|
|
||||||
async def _run_client(hass: HomeAssistant, client: Client, interval: float) -> None:
|
async def _run_client(hass: HomeAssistant, client: Client, interval: float) -> None:
|
||||||
|
@ -10,18 +10,11 @@ from arcam.fmj.utils import get_uniqueid_from_host, get_uniqueid_from_udn
|
|||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components import ssdp
|
from homeassistant.components import ssdp
|
||||||
from homeassistant.config_entries import ConfigEntry, ConfigFlow, ConfigFlowResult
|
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
|
||||||
from homeassistant.const import CONF_HOST, CONF_PORT
|
from homeassistant.const import CONF_HOST, CONF_PORT
|
||||||
from homeassistant.core import HomeAssistant
|
|
||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
|
|
||||||
from .const import DEFAULT_NAME, DEFAULT_PORT, DOMAIN, DOMAIN_DATA_ENTRIES
|
from .const import DEFAULT_NAME, DEFAULT_PORT, DOMAIN
|
||||||
|
|
||||||
|
|
||||||
def get_entry_client(hass: HomeAssistant, entry: ConfigEntry) -> Client:
|
|
||||||
"""Retrieve client associated with a config entry."""
|
|
||||||
client: Client = hass.data[DOMAIN_DATA_ENTRIES][entry.entry_id]
|
|
||||||
return client
|
|
||||||
|
|
||||||
|
|
||||||
class ArcamFmjFlowHandler(ConfigFlow, domain=DOMAIN):
|
class ArcamFmjFlowHandler(ConfigFlow, domain=DOMAIN):
|
||||||
|
@ -11,5 +11,3 @@ EVENT_TURN_ON = "arcam_fmj.turn_on"
|
|||||||
DEFAULT_PORT = 50000
|
DEFAULT_PORT = 50000
|
||||||
DEFAULT_NAME = "Arcam FMJ"
|
DEFAULT_NAME = "Arcam FMJ"
|
||||||
DEFAULT_SCAN_INTERVAL = 5
|
DEFAULT_SCAN_INTERVAL = 5
|
||||||
|
|
||||||
DOMAIN_DATA_ENTRIES = f"{DOMAIN}.entries"
|
|
||||||
|
@ -19,7 +19,6 @@ from homeassistant.components.media_player import (
|
|||||||
MediaType,
|
MediaType,
|
||||||
)
|
)
|
||||||
from homeassistant.components.media_player.errors import BrowseError
|
from homeassistant.components.media_player.errors import BrowseError
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import ATTR_ENTITY_ID
|
from homeassistant.const import ATTR_ENTITY_ID
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.exceptions import HomeAssistantError
|
from homeassistant.exceptions import HomeAssistantError
|
||||||
@ -27,7 +26,7 @@ from homeassistant.helpers.device_registry import DeviceInfo
|
|||||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from .config_flow import get_entry_client
|
from . import ArcamFmjConfigEntry
|
||||||
from .const import (
|
from .const import (
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
EVENT_TURN_ON,
|
EVENT_TURN_ON,
|
||||||
@ -41,12 +40,12 @@ _LOGGER = logging.getLogger(__name__)
|
|||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: ConfigEntry,
|
config_entry: ArcamFmjConfigEntry,
|
||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the configuration entry."""
|
"""Set up the configuration entry."""
|
||||||
|
|
||||||
client = get_entry_client(hass, config_entry)
|
client = config_entry.runtime_data
|
||||||
|
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
[
|
[
|
||||||
|
@ -8,8 +8,7 @@ from arcam.fmj.client import ConnectionFailed
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components import ssdp
|
from homeassistant.components import ssdp
|
||||||
from homeassistant.components.arcam_fmj.config_flow import get_entry_client
|
from homeassistant.components.arcam_fmj.const import DOMAIN
|
||||||
from homeassistant.components.arcam_fmj.const import DOMAIN, DOMAIN_DATA_ENTRIES
|
|
||||||
from homeassistant.config_entries import SOURCE_SSDP, SOURCE_USER
|
from homeassistant.config_entries import SOURCE_SSDP, SOURCE_USER
|
||||||
from homeassistant.const import CONF_HOST, CONF_PORT, CONF_SOURCE
|
from homeassistant.const import CONF_HOST, CONF_PORT, CONF_SOURCE
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
@ -215,12 +214,3 @@ async def test_user_wrong(
|
|||||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||||
assert result["title"] == f"Arcam FMJ ({MOCK_HOST})"
|
assert result["title"] == f"Arcam FMJ ({MOCK_HOST})"
|
||||||
assert result["result"].unique_id is None
|
assert result["result"].unique_id is None
|
||||||
|
|
||||||
|
|
||||||
async def test_get_entry_client(hass: HomeAssistant) -> None:
|
|
||||||
"""Test helper for configuration."""
|
|
||||||
entry = MockConfigEntry(
|
|
||||||
domain=DOMAIN, data=MOCK_CONFIG_ENTRY, title=MOCK_NAME, unique_id=MOCK_UUID
|
|
||||||
)
|
|
||||||
hass.data[DOMAIN_DATA_ENTRIES] = {entry.entry_id: "dummy"}
|
|
||||||
assert get_entry_client(hass, entry) == "dummy"
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user