mirror of
https://github.com/home-assistant/core.git
synced 2025-07-15 17:27:10 +00:00
Use runtime_data for roku (#132781)
* use runtime_data for roku * unload cleanup * tweaks * tweaks * fix tests * fix tests * Update config_flow.py * Update config_flow.py
This commit is contained in:
parent
355e80aa56
commit
73feeacc39
@ -6,7 +6,7 @@ from homeassistant.config_entries import ConfigEntry
|
|||||||
from homeassistant.const import CONF_HOST, Platform
|
from homeassistant.const import CONF_HOST, Platform
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
from .const import CONF_PLAY_MEDIA_APP_ID, DEFAULT_PLAY_MEDIA_APP_ID, DOMAIN
|
from .const import CONF_PLAY_MEDIA_APP_ID, DEFAULT_PLAY_MEDIA_APP_ID
|
||||||
from .coordinator import RokuDataUpdateCoordinator
|
from .coordinator import RokuDataUpdateCoordinator
|
||||||
|
|
||||||
PLATFORMS = [
|
PLATFORMS = [
|
||||||
@ -17,8 +17,10 @@ PLATFORMS = [
|
|||||||
Platform.SENSOR,
|
Platform.SENSOR,
|
||||||
]
|
]
|
||||||
|
|
||||||
|
type RokuConfigEntry = ConfigEntry[RokuDataUpdateCoordinator]
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
||||||
|
async def async_setup_entry(hass: HomeAssistant, entry: RokuConfigEntry) -> bool:
|
||||||
"""Set up Roku from a config entry."""
|
"""Set up Roku from a config entry."""
|
||||||
if (device_id := entry.unique_id) is None:
|
if (device_id := entry.unique_id) is None:
|
||||||
device_id = entry.entry_id
|
device_id = entry.entry_id
|
||||||
@ -33,7 +35,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
)
|
)
|
||||||
await coordinator.async_config_entry_first_refresh()
|
await coordinator.async_config_entry_first_refresh()
|
||||||
|
|
||||||
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
|
entry.runtime_data = coordinator
|
||||||
|
|
||||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||||
|
|
||||||
@ -42,13 +44,11 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_unload_entry(hass: HomeAssistant, entry: RokuConfigEntry) -> bool:
|
||||||
"""Unload a config entry."""
|
"""Unload a config entry."""
|
||||||
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
|
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||||
hass.data[DOMAIN].pop(entry.entry_id)
|
|
||||||
return unload_ok
|
|
||||||
|
|
||||||
|
|
||||||
async def async_reload_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
|
async def async_reload_entry(hass: HomeAssistant, entry: RokuConfigEntry) -> None:
|
||||||
"""Reload the config entry when it changed."""
|
"""Reload the config entry when it changed."""
|
||||||
await hass.config_entries.async_reload(entry.entry_id)
|
await hass.config_entries.async_reload(entry.entry_id)
|
||||||
|
@ -11,12 +11,11 @@ from homeassistant.components.binary_sensor import (
|
|||||||
BinarySensorEntity,
|
BinarySensorEntity,
|
||||||
BinarySensorEntityDescription,
|
BinarySensorEntityDescription,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import EntityCategory
|
from homeassistant.const import EntityCategory
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from .const import DOMAIN
|
from . import RokuConfigEntry
|
||||||
from .entity import RokuEntity
|
from .entity import RokuEntity
|
||||||
|
|
||||||
|
|
||||||
@ -56,15 +55,13 @@ BINARY_SENSORS: tuple[RokuBinarySensorEntityDescription, ...] = (
|
|||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
entry: ConfigEntry,
|
entry: RokuConfigEntry,
|
||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up a Roku binary sensors based on a config entry."""
|
"""Set up a Roku binary sensors based on a config entry."""
|
||||||
coordinator = hass.data[DOMAIN][entry.entry_id]
|
|
||||||
|
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
RokuBinarySensorEntity(
|
RokuBinarySensorEntity(
|
||||||
coordinator=coordinator,
|
coordinator=entry.runtime_data,
|
||||||
description=description,
|
description=description,
|
||||||
)
|
)
|
||||||
for description in BINARY_SENSORS
|
for description in BINARY_SENSORS
|
||||||
|
@ -10,16 +10,12 @@ from rokuecp import Roku, RokuError
|
|||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components import ssdp, zeroconf
|
from homeassistant.components import ssdp, zeroconf
|
||||||
from homeassistant.config_entries import (
|
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult, OptionsFlow
|
||||||
ConfigEntry,
|
|
||||||
ConfigFlow,
|
|
||||||
ConfigFlowResult,
|
|
||||||
OptionsFlow,
|
|
||||||
)
|
|
||||||
from homeassistant.const import CONF_HOST, CONF_NAME
|
from homeassistant.const import CONF_HOST, CONF_NAME
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
|
|
||||||
|
from . import RokuConfigEntry
|
||||||
from .const import CONF_PLAY_MEDIA_APP_ID, DEFAULT_PLAY_MEDIA_APP_ID, DOMAIN
|
from .const import CONF_PLAY_MEDIA_APP_ID, DEFAULT_PLAY_MEDIA_APP_ID, DOMAIN
|
||||||
|
|
||||||
DATA_SCHEMA = vol.Schema({vol.Required(CONF_HOST): str})
|
DATA_SCHEMA = vol.Schema({vol.Required(CONF_HOST): str})
|
||||||
@ -164,7 +160,7 @@ class RokuConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
@callback
|
@callback
|
||||||
def async_get_options_flow(
|
def async_get_options_flow(
|
||||||
config_entry: ConfigEntry,
|
config_entry: RokuConfigEntry,
|
||||||
) -> RokuOptionsFlowHandler:
|
) -> RokuOptionsFlowHandler:
|
||||||
"""Create the options flow."""
|
"""Create the options flow."""
|
||||||
return RokuOptionsFlowHandler()
|
return RokuOptionsFlowHandler()
|
||||||
|
@ -4,25 +4,21 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
from .const import DOMAIN
|
from . import RokuConfigEntry
|
||||||
from .coordinator import RokuDataUpdateCoordinator
|
|
||||||
|
|
||||||
|
|
||||||
async def async_get_config_entry_diagnostics(
|
async def async_get_config_entry_diagnostics(
|
||||||
hass: HomeAssistant, config_entry: ConfigEntry
|
hass: HomeAssistant, entry: RokuConfigEntry
|
||||||
) -> dict[str, Any]:
|
) -> dict[str, Any]:
|
||||||
"""Return diagnostics for a config entry."""
|
"""Return diagnostics for a config entry."""
|
||||||
coordinator: RokuDataUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id]
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"entry": {
|
"entry": {
|
||||||
"data": {
|
"data": {
|
||||||
**config_entry.data,
|
**entry.data,
|
||||||
},
|
},
|
||||||
"unique_id": config_entry.unique_id,
|
"unique_id": entry.unique_id,
|
||||||
},
|
},
|
||||||
"data": coordinator.data.as_dict(),
|
"data": entry.runtime_data.data.as_dict(),
|
||||||
}
|
}
|
||||||
|
@ -23,13 +23,13 @@ from homeassistant.components.media_player import (
|
|||||||
async_process_play_media_url,
|
async_process_play_media_url,
|
||||||
)
|
)
|
||||||
from homeassistant.components.stream import FORMAT_CONTENT_TYPE, HLS_PROVIDER
|
from homeassistant.components.stream import FORMAT_CONTENT_TYPE, HLS_PROVIDER
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import ATTR_NAME
|
from homeassistant.const import ATTR_NAME
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers import entity_platform
|
from homeassistant.helpers import entity_platform
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.typing import VolDictType
|
from homeassistant.helpers.typing import VolDictType
|
||||||
|
|
||||||
|
from . import RokuConfigEntry
|
||||||
from .browse_media import async_browse_media
|
from .browse_media import async_browse_media
|
||||||
from .const import (
|
from .const import (
|
||||||
ATTR_ARTIST_NAME,
|
ATTR_ARTIST_NAME,
|
||||||
@ -38,7 +38,6 @@ from .const import (
|
|||||||
ATTR_KEYWORD,
|
ATTR_KEYWORD,
|
||||||
ATTR_MEDIA_TYPE,
|
ATTR_MEDIA_TYPE,
|
||||||
ATTR_THUMBNAIL,
|
ATTR_THUMBNAIL,
|
||||||
DOMAIN,
|
|
||||||
SERVICE_SEARCH,
|
SERVICE_SEARCH,
|
||||||
)
|
)
|
||||||
from .coordinator import RokuDataUpdateCoordinator
|
from .coordinator import RokuDataUpdateCoordinator
|
||||||
@ -83,15 +82,13 @@ SEARCH_SCHEMA: VolDictType = {vol.Required(ATTR_KEYWORD): str}
|
|||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
hass: HomeAssistant, entry: RokuConfigEntry, async_add_entities: AddEntitiesCallback
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the Roku config entry."""
|
"""Set up the Roku config entry."""
|
||||||
coordinator: RokuDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
|
||||||
|
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
[
|
[
|
||||||
RokuMediaPlayer(
|
RokuMediaPlayer(
|
||||||
coordinator=coordinator,
|
coordinator=entry.runtime_data,
|
||||||
)
|
)
|
||||||
],
|
],
|
||||||
True,
|
True,
|
||||||
|
@ -6,28 +6,24 @@ from collections.abc import Iterable
|
|||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from homeassistant.components.remote import ATTR_NUM_REPEATS, RemoteEntity
|
from homeassistant.components.remote import ATTR_NUM_REPEATS, RemoteEntity
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from .const import DOMAIN
|
from . import RokuConfigEntry
|
||||||
from .coordinator import RokuDataUpdateCoordinator
|
|
||||||
from .entity import RokuEntity
|
from .entity import RokuEntity
|
||||||
from .helpers import roku_exception_handler
|
from .helpers import roku_exception_handler
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
entry: ConfigEntry,
|
entry: RokuConfigEntry,
|
||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Load Roku remote based on a config entry."""
|
"""Load Roku remote based on a config entry."""
|
||||||
coordinator: RokuDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
|
||||||
|
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
[
|
[
|
||||||
RokuRemote(
|
RokuRemote(
|
||||||
coordinator=coordinator,
|
coordinator=entry.runtime_data,
|
||||||
)
|
)
|
||||||
],
|
],
|
||||||
True,
|
True,
|
||||||
|
@ -9,12 +9,10 @@ from rokuecp import Roku
|
|||||||
from rokuecp.models import Device as RokuDevice
|
from rokuecp.models import Device as RokuDevice
|
||||||
|
|
||||||
from homeassistant.components.select import SelectEntity, SelectEntityDescription
|
from homeassistant.components.select import SelectEntity, SelectEntityDescription
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from .const import DOMAIN
|
from . import RokuConfigEntry
|
||||||
from .coordinator import RokuDataUpdateCoordinator
|
|
||||||
from .entity import RokuEntity
|
from .entity import RokuEntity
|
||||||
from .helpers import format_channel_name, roku_exception_handler
|
from .helpers import format_channel_name, roku_exception_handler
|
||||||
|
|
||||||
@ -108,16 +106,15 @@ CHANNEL_ENTITY = RokuSelectEntityDescription(
|
|||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
entry: ConfigEntry,
|
entry: RokuConfigEntry,
|
||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up Roku select based on a config entry."""
|
"""Set up Roku select based on a config entry."""
|
||||||
coordinator: RokuDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
device: RokuDevice = entry.runtime_data.data
|
||||||
device: RokuDevice = coordinator.data
|
|
||||||
|
|
||||||
entities: list[RokuSelectEntity] = [
|
entities: list[RokuSelectEntity] = [
|
||||||
RokuSelectEntity(
|
RokuSelectEntity(
|
||||||
coordinator=coordinator,
|
coordinator=entry.runtime_data,
|
||||||
description=description,
|
description=description,
|
||||||
)
|
)
|
||||||
for description in ENTITIES
|
for description in ENTITIES
|
||||||
@ -126,7 +123,7 @@ async def async_setup_entry(
|
|||||||
if len(device.channels) > 0:
|
if len(device.channels) > 0:
|
||||||
entities.append(
|
entities.append(
|
||||||
RokuSelectEntity(
|
RokuSelectEntity(
|
||||||
coordinator=coordinator,
|
coordinator=entry.runtime_data,
|
||||||
description=CHANNEL_ENTITY,
|
description=CHANNEL_ENTITY,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
@ -8,13 +8,11 @@ from dataclasses import dataclass
|
|||||||
from rokuecp.models import Device as RokuDevice
|
from rokuecp.models import Device as RokuDevice
|
||||||
|
|
||||||
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
|
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import EntityCategory
|
from homeassistant.const import EntityCategory
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from .const import DOMAIN
|
from . import RokuConfigEntry
|
||||||
from .coordinator import RokuDataUpdateCoordinator
|
|
||||||
from .entity import RokuEntity
|
from .entity import RokuEntity
|
||||||
|
|
||||||
|
|
||||||
@ -43,15 +41,13 @@ SENSORS: tuple[RokuSensorEntityDescription, ...] = (
|
|||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
entry: ConfigEntry,
|
entry: RokuConfigEntry,
|
||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up Roku sensor based on a config entry."""
|
"""Set up Roku sensor based on a config entry."""
|
||||||
coordinator: RokuDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
|
||||||
|
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
RokuSensorEntity(
|
RokuSensorEntity(
|
||||||
coordinator=coordinator,
|
coordinator=entry.runtime_data,
|
||||||
description=description,
|
description=description,
|
||||||
)
|
)
|
||||||
for description in SENSORS
|
for description in SENSORS
|
||||||
|
@ -4,7 +4,6 @@ from unittest.mock import AsyncMock, MagicMock, patch
|
|||||||
|
|
||||||
from rokuecp import RokuConnectionError
|
from rokuecp import RokuConnectionError
|
||||||
|
|
||||||
from homeassistant.components.roku.const import DOMAIN
|
|
||||||
from homeassistant.config_entries import ConfigEntryState
|
from homeassistant.config_entries import ConfigEntryState
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
@ -38,12 +37,7 @@ async def test_config_entry_no_unique_id(
|
|||||||
await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert mock_config_entry.entry_id in hass.data[DOMAIN]
|
|
||||||
assert mock_config_entry.state is ConfigEntryState.LOADED
|
assert mock_config_entry.state is ConfigEntryState.LOADED
|
||||||
assert (
|
|
||||||
hass.data[DOMAIN][mock_config_entry.entry_id].device_id
|
|
||||||
== mock_config_entry.entry_id
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
async def test_load_unload_config_entry(
|
async def test_load_unload_config_entry(
|
||||||
@ -56,10 +50,9 @@ async def test_load_unload_config_entry(
|
|||||||
await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert mock_config_entry.entry_id in hass.data[DOMAIN]
|
|
||||||
assert mock_config_entry.state is ConfigEntryState.LOADED
|
assert mock_config_entry.state is ConfigEntryState.LOADED
|
||||||
|
|
||||||
await hass.config_entries.async_unload(mock_config_entry.entry_id)
|
await hass.config_entries.async_unload(mock_config_entry.entry_id)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert mock_config_entry.entry_id not in hass.data[DOMAIN]
|
|
||||||
assert mock_config_entry.state is ConfigEntryState.NOT_LOADED
|
assert mock_config_entry.state is ConfigEntryState.NOT_LOADED
|
||||||
|
Loading…
x
Reference in New Issue
Block a user