mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 22:27:07 +00:00
Move yamaha_musiccast coordinator to separate module (#126546)
This commit is contained in:
parent
d101fb33b3
commit
d2ab7dd9fb
@ -2,29 +2,22 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from datetime import timedelta
|
|
||||||
import logging
|
import logging
|
||||||
from typing import TYPE_CHECKING
|
|
||||||
|
|
||||||
from aiomusiccast import MusicCastConnectionException
|
from aiomusiccast.musiccast_device import MusicCastDevice
|
||||||
from aiomusiccast.musiccast_device import MusicCastData, MusicCastDevice
|
|
||||||
|
|
||||||
from homeassistant.components import ssdp
|
from homeassistant.components import ssdp
|
||||||
from homeassistant.config_entries import ConfigEntry
|
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 homeassistant.helpers.aiohttp_client import async_get_clientsession
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
|
||||||
|
|
||||||
from .const import CONF_SERIAL, CONF_UPNP_DESC, DOMAIN
|
from .const import CONF_SERIAL, CONF_UPNP_DESC, DOMAIN
|
||||||
|
from .coordinator import MusicCastDataUpdateCoordinator
|
||||||
if TYPE_CHECKING:
|
|
||||||
from .entity import MusicCastDeviceEntity
|
|
||||||
|
|
||||||
PLATFORMS = [Platform.MEDIA_PLAYER, Platform.NUMBER, Platform.SELECT, Platform.SWITCH]
|
PLATFORMS = [Platform.MEDIA_PLAYER, Platform.NUMBER, Platform.SELECT, Platform.SWITCH]
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
SCAN_INTERVAL = timedelta(seconds=60)
|
|
||||||
|
|
||||||
|
|
||||||
async def get_upnp_desc(hass: HomeAssistant, host: str):
|
async def get_upnp_desc(hass: HomeAssistant, host: str):
|
||||||
@ -90,22 +83,3 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
async def async_reload_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
|
async def async_reload_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
|
||||||
"""Reload config entry."""
|
"""Reload config entry."""
|
||||||
await hass.config_entries.async_reload(entry.entry_id)
|
await hass.config_entries.async_reload(entry.entry_id)
|
||||||
|
|
||||||
|
|
||||||
class MusicCastDataUpdateCoordinator(DataUpdateCoordinator[MusicCastData]): # pylint: disable=hass-enforce-class-module
|
|
||||||
"""Class to manage fetching data from the API."""
|
|
||||||
|
|
||||||
def __init__(self, hass: HomeAssistant, client: MusicCastDevice) -> None:
|
|
||||||
"""Initialize."""
|
|
||||||
self.musiccast = client
|
|
||||||
|
|
||||||
super().__init__(hass, _LOGGER, name=DOMAIN, update_interval=SCAN_INTERVAL)
|
|
||||||
self.entities: list[MusicCastDeviceEntity] = []
|
|
||||||
|
|
||||||
async def _async_update_data(self) -> MusicCastData:
|
|
||||||
"""Update data via library."""
|
|
||||||
try:
|
|
||||||
await self.musiccast.fetch()
|
|
||||||
except MusicCastConnectionException as exception:
|
|
||||||
raise UpdateFailed from exception
|
|
||||||
return self.musiccast.data
|
|
||||||
|
41
homeassistant/components/yamaha_musiccast/coordinator.py
Normal file
41
homeassistant/components/yamaha_musiccast/coordinator.py
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
"""The MusicCast integration."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from datetime import timedelta
|
||||||
|
import logging
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
|
from aiomusiccast import MusicCastConnectionException
|
||||||
|
from aiomusiccast.musiccast_device import MusicCastData, MusicCastDevice
|
||||||
|
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||||
|
|
||||||
|
from .const import DOMAIN
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from .entity import MusicCastDeviceEntity
|
||||||
|
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
SCAN_INTERVAL = timedelta(seconds=60)
|
||||||
|
|
||||||
|
|
||||||
|
class MusicCastDataUpdateCoordinator(DataUpdateCoordinator[MusicCastData]):
|
||||||
|
"""Class to manage fetching data from the API."""
|
||||||
|
|
||||||
|
def __init__(self, hass: HomeAssistant, client: MusicCastDevice) -> None:
|
||||||
|
"""Initialize."""
|
||||||
|
self.musiccast = client
|
||||||
|
|
||||||
|
super().__init__(hass, _LOGGER, name=DOMAIN, update_interval=SCAN_INTERVAL)
|
||||||
|
self.entities: list[MusicCastDeviceEntity] = []
|
||||||
|
|
||||||
|
async def _async_update_data(self) -> MusicCastData:
|
||||||
|
"""Update data via library."""
|
||||||
|
try:
|
||||||
|
await self.musiccast.fetch()
|
||||||
|
except MusicCastConnectionException as exception:
|
||||||
|
raise UpdateFailed from exception
|
||||||
|
return self.musiccast.data
|
@ -12,8 +12,8 @@ from homeassistant.helpers.device_registry import (
|
|||||||
)
|
)
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
from . import MusicCastDataUpdateCoordinator
|
|
||||||
from .const import BRAND, DEFAULT_ZONE, DOMAIN, ENTITY_CATEGORY_MAPPING
|
from .const import BRAND, DEFAULT_ZONE, DOMAIN, ENTITY_CATEGORY_MAPPING
|
||||||
|
from .coordinator import MusicCastDataUpdateCoordinator
|
||||||
|
|
||||||
|
|
||||||
class MusicCastEntity(CoordinatorEntity[MusicCastDataUpdateCoordinator]):
|
class MusicCastEntity(CoordinatorEntity[MusicCastDataUpdateCoordinator]):
|
||||||
|
@ -27,7 +27,6 @@ from homeassistant.helpers.entity import Entity
|
|||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.util import uuid
|
from homeassistant.util import uuid
|
||||||
|
|
||||||
from . import MusicCastDataUpdateCoordinator
|
|
||||||
from .const import (
|
from .const import (
|
||||||
ATTR_MAIN_SYNC,
|
ATTR_MAIN_SYNC,
|
||||||
ATTR_MC_LINK,
|
ATTR_MC_LINK,
|
||||||
@ -38,6 +37,7 @@ from .const import (
|
|||||||
MEDIA_CLASS_MAPPING,
|
MEDIA_CLASS_MAPPING,
|
||||||
NULL_GROUP,
|
NULL_GROUP,
|
||||||
)
|
)
|
||||||
|
from .coordinator import MusicCastDataUpdateCoordinator
|
||||||
from .entity import MusicCastDeviceEntity
|
from .entity import MusicCastDeviceEntity
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
@ -9,7 +9,8 @@ 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 . import DOMAIN, MusicCastDataUpdateCoordinator
|
from .const import DOMAIN
|
||||||
|
from .coordinator import MusicCastDataUpdateCoordinator
|
||||||
from .entity import MusicCastCapabilityEntity
|
from .entity import MusicCastCapabilityEntity
|
||||||
|
|
||||||
|
|
||||||
|
@ -9,8 +9,8 @@ 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 . import DOMAIN, MusicCastDataUpdateCoordinator
|
from .const import DOMAIN, TRANSLATION_KEY_MAPPING
|
||||||
from .const import TRANSLATION_KEY_MAPPING
|
from .coordinator import MusicCastDataUpdateCoordinator
|
||||||
from .entity import MusicCastCapabilityEntity
|
from .entity import MusicCastCapabilityEntity
|
||||||
|
|
||||||
|
|
||||||
|
@ -9,7 +9,8 @@ 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 . import DOMAIN, MusicCastDataUpdateCoordinator
|
from .const import DOMAIN
|
||||||
|
from .coordinator import MusicCastDataUpdateCoordinator
|
||||||
from .entity import MusicCastCapabilityEntity
|
from .entity import MusicCastCapabilityEntity
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user