mirror of
https://github.com/home-assistant/core.git
synced 2025-07-10 06:47:09 +00:00
Explicitly pass in the config_entry in roku coordinator (#137968)
explicitly pass in the config_entry in coordinator
This commit is contained in:
parent
96b4a71f6f
commit
fa35f29c27
@ -2,12 +2,10 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.const import 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
|
from .coordinator import RokuConfigEntry, RokuDataUpdateCoordinator
|
||||||
from .coordinator import RokuDataUpdateCoordinator
|
|
||||||
|
|
||||||
PLATFORMS = [
|
PLATFORMS = [
|
||||||
Platform.BINARY_SENSOR,
|
Platform.BINARY_SENSOR,
|
||||||
@ -17,22 +15,10 @@ PLATFORMS = [
|
|||||||
Platform.SENSOR,
|
Platform.SENSOR,
|
||||||
]
|
]
|
||||||
|
|
||||||
type RokuConfigEntry = ConfigEntry[RokuDataUpdateCoordinator]
|
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: RokuConfigEntry) -> 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:
|
coordinator = RokuDataUpdateCoordinator(hass, entry)
|
||||||
device_id = entry.entry_id
|
|
||||||
|
|
||||||
coordinator = RokuDataUpdateCoordinator(
|
|
||||||
hass,
|
|
||||||
host=entry.data[CONF_HOST],
|
|
||||||
device_id=device_id,
|
|
||||||
play_media_app_id=entry.options.get(
|
|
||||||
CONF_PLAY_MEDIA_APP_ID, DEFAULT_PLAY_MEDIA_APP_ID
|
|
||||||
),
|
|
||||||
)
|
|
||||||
await coordinator.async_config_entry_first_refresh()
|
await coordinator.async_config_entry_first_refresh()
|
||||||
|
|
||||||
entry.runtime_data = coordinator
|
entry.runtime_data = coordinator
|
||||||
|
@ -15,7 +15,7 @@ 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 . import RokuConfigEntry
|
from .coordinator import RokuConfigEntry
|
||||||
from .entity import RokuEntity
|
from .entity import RokuEntity
|
||||||
|
|
||||||
# Coordinator is used to centralize the data updates
|
# Coordinator is used to centralize the data updates
|
||||||
|
@ -25,8 +25,8 @@ from homeassistant.helpers.service_info.ssdp import (
|
|||||||
)
|
)
|
||||||
from homeassistant.helpers.service_info.zeroconf import ZeroconfServiceInfo
|
from homeassistant.helpers.service_info.zeroconf import ZeroconfServiceInfo
|
||||||
|
|
||||||
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
|
||||||
|
from .coordinator import RokuConfigEntry
|
||||||
|
|
||||||
DATA_SCHEMA = vol.Schema({vol.Required(CONF_HOST): str})
|
DATA_SCHEMA = vol.Schema({vol.Required(CONF_HOST): str})
|
||||||
|
|
||||||
|
@ -8,33 +8,44 @@ import logging
|
|||||||
from rokuecp import Roku, RokuError
|
from rokuecp import Roku, RokuError
|
||||||
from rokuecp.models import Device
|
from rokuecp.models import Device
|
||||||
|
|
||||||
|
from homeassistant.config_entries import ConfigEntry
|
||||||
|
from homeassistant.const import CONF_HOST
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
from homeassistant.helpers.debounce import Debouncer
|
from homeassistant.helpers.debounce import Debouncer
|
||||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||||
from homeassistant.util.dt import utcnow
|
from homeassistant.util.dt import utcnow
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .const import CONF_PLAY_MEDIA_APP_ID, DEFAULT_PLAY_MEDIA_APP_ID, DOMAIN
|
||||||
|
|
||||||
REQUEST_REFRESH_DELAY = 0.35
|
REQUEST_REFRESH_DELAY = 0.35
|
||||||
|
|
||||||
SCAN_INTERVAL = timedelta(seconds=10)
|
SCAN_INTERVAL = timedelta(seconds=10)
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
type RokuConfigEntry = ConfigEntry[RokuDataUpdateCoordinator]
|
||||||
|
|
||||||
|
|
||||||
class RokuDataUpdateCoordinator(DataUpdateCoordinator[Device]):
|
class RokuDataUpdateCoordinator(DataUpdateCoordinator[Device]):
|
||||||
"""Class to manage fetching Roku data."""
|
"""Class to manage fetching Roku data."""
|
||||||
|
|
||||||
|
config_entry: RokuConfigEntry
|
||||||
last_full_update: datetime | None
|
last_full_update: datetime | None
|
||||||
roku: Roku
|
roku: Roku
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self, hass: HomeAssistant, *, host: str, device_id: str, play_media_app_id: str
|
self,
|
||||||
|
hass: HomeAssistant,
|
||||||
|
config_entry: RokuConfigEntry,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize global Roku data updater."""
|
"""Initialize global Roku data updater."""
|
||||||
self.device_id = device_id
|
self.device_id = config_entry.unique_id or config_entry.entry_id
|
||||||
self.roku = Roku(host=host, session=async_get_clientsession(hass))
|
self.roku = Roku(
|
||||||
self.play_media_app_id = play_media_app_id
|
host=config_entry.data[CONF_HOST], session=async_get_clientsession(hass)
|
||||||
|
)
|
||||||
|
self.play_media_app_id = config_entry.options.get(
|
||||||
|
CONF_PLAY_MEDIA_APP_ID, DEFAULT_PLAY_MEDIA_APP_ID
|
||||||
|
)
|
||||||
|
|
||||||
self.full_update_interval = timedelta(minutes=15)
|
self.full_update_interval = timedelta(minutes=15)
|
||||||
self.last_full_update = None
|
self.last_full_update = None
|
||||||
@ -42,6 +53,7 @@ class RokuDataUpdateCoordinator(DataUpdateCoordinator[Device]):
|
|||||||
super().__init__(
|
super().__init__(
|
||||||
hass,
|
hass,
|
||||||
_LOGGER,
|
_LOGGER,
|
||||||
|
config_entry=config_entry,
|
||||||
name=DOMAIN,
|
name=DOMAIN,
|
||||||
update_interval=SCAN_INTERVAL,
|
update_interval=SCAN_INTERVAL,
|
||||||
# We don't want an immediate refresh since the device
|
# We don't want an immediate refresh since the device
|
||||||
|
@ -6,7 +6,7 @@ from typing import Any
|
|||||||
|
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
from . import RokuConfigEntry
|
from .coordinator import RokuConfigEntry
|
||||||
|
|
||||||
|
|
||||||
async def async_get_config_entry_diagnostics(
|
async def async_get_config_entry_diagnostics(
|
||||||
|
@ -6,8 +6,8 @@ from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC, Device
|
|||||||
from homeassistant.helpers.entity import EntityDescription
|
from homeassistant.helpers.entity import EntityDescription
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
from . import RokuDataUpdateCoordinator
|
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
|
from .coordinator import RokuDataUpdateCoordinator
|
||||||
|
|
||||||
|
|
||||||
class RokuEntity(CoordinatorEntity[RokuDataUpdateCoordinator]):
|
class RokuEntity(CoordinatorEntity[RokuDataUpdateCoordinator]):
|
||||||
|
@ -29,7 +29,6 @@ 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,
|
||||||
@ -40,7 +39,7 @@ from .const import (
|
|||||||
ATTR_THUMBNAIL,
|
ATTR_THUMBNAIL,
|
||||||
SERVICE_SEARCH,
|
SERVICE_SEARCH,
|
||||||
)
|
)
|
||||||
from .coordinator import RokuDataUpdateCoordinator
|
from .coordinator import RokuConfigEntry, 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
|
||||||
|
|
||||||
|
@ -9,7 +9,7 @@ from homeassistant.components.remote import ATTR_NUM_REPEATS, RemoteEntity
|
|||||||
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 . import RokuConfigEntry
|
from .coordinator import RokuConfigEntry
|
||||||
from .entity import RokuEntity
|
from .entity import RokuEntity
|
||||||
from .helpers import roku_exception_handler
|
from .helpers import roku_exception_handler
|
||||||
|
|
||||||
|
@ -12,7 +12,7 @@ from homeassistant.components.select import SelectEntity, SelectEntityDescriptio
|
|||||||
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 . import RokuConfigEntry
|
from .coordinator import RokuConfigEntry
|
||||||
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
|
||||||
|
|
||||||
|
@ -12,7 +12,7 @@ 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 . import RokuConfigEntry
|
from .coordinator import RokuConfigEntry
|
||||||
from .entity import RokuEntity
|
from .entity import RokuEntity
|
||||||
|
|
||||||
# Coordinator is used to centralize the data updates
|
# Coordinator is used to centralize the data updates
|
||||||
|
Loading…
x
Reference in New Issue
Block a user