mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Move yamaha_musiccast base entity to separate module (#126544)
This commit is contained in:
parent
95948e4eb7
commit
6d83a15ad5
@ -4,35 +4,22 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
import logging
|
import logging
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
from aiomusiccast import MusicCastConnectionException
|
from aiomusiccast import MusicCastConnectionException
|
||||||
from aiomusiccast.capabilities import Capability
|
|
||||||
from aiomusiccast.musiccast_device import MusicCastData, 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 ATTR_CONNECTIONS, ATTR_VIA_DEVICE, 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.device_registry import (
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||||
CONNECTION_NETWORK_MAC,
|
|
||||||
DeviceInfo,
|
|
||||||
format_mac,
|
|
||||||
)
|
|
||||||
from homeassistant.helpers.update_coordinator import (
|
|
||||||
CoordinatorEntity,
|
|
||||||
DataUpdateCoordinator,
|
|
||||||
UpdateFailed,
|
|
||||||
)
|
|
||||||
|
|
||||||
from .const import (
|
from .const import CONF_SERIAL, CONF_UPNP_DESC, DOMAIN
|
||||||
BRAND,
|
|
||||||
CONF_SERIAL,
|
if TYPE_CHECKING:
|
||||||
CONF_UPNP_DESC,
|
from .entity import MusicCastDeviceEntity
|
||||||
DEFAULT_ZONE,
|
|
||||||
DOMAIN,
|
|
||||||
ENTITY_CATEGORY_MAPPING,
|
|
||||||
)
|
|
||||||
|
|
||||||
PLATFORMS = [Platform.MEDIA_PLAYER, Platform.NUMBER, Platform.SELECT, Platform.SWITCH]
|
PLATFORMS = [Platform.MEDIA_PLAYER, Platform.NUMBER, Platform.SELECT, Platform.SWITCH]
|
||||||
|
|
||||||
@ -122,99 +109,3 @@ class MusicCastDataUpdateCoordinator(DataUpdateCoordinator[MusicCastData]): # p
|
|||||||
except MusicCastConnectionException as exception:
|
except MusicCastConnectionException as exception:
|
||||||
raise UpdateFailed from exception
|
raise UpdateFailed from exception
|
||||||
return self.musiccast.data
|
return self.musiccast.data
|
||||||
|
|
||||||
|
|
||||||
class MusicCastEntity(CoordinatorEntity[MusicCastDataUpdateCoordinator]):
|
|
||||||
"""Defines a base MusicCast entity."""
|
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
*,
|
|
||||||
name: str,
|
|
||||||
icon: str,
|
|
||||||
coordinator: MusicCastDataUpdateCoordinator,
|
|
||||||
enabled_default: bool = True,
|
|
||||||
) -> None:
|
|
||||||
"""Initialize the MusicCast entity."""
|
|
||||||
super().__init__(coordinator)
|
|
||||||
self._attr_entity_registry_enabled_default = enabled_default
|
|
||||||
self._attr_icon = icon
|
|
||||||
self._attr_name = name
|
|
||||||
|
|
||||||
|
|
||||||
class MusicCastDeviceEntity(MusicCastEntity):
|
|
||||||
"""Defines a MusicCast device entity."""
|
|
||||||
|
|
||||||
_zone_id: str = DEFAULT_ZONE
|
|
||||||
|
|
||||||
@property
|
|
||||||
def device_id(self):
|
|
||||||
"""Return the ID of the current device."""
|
|
||||||
if self._zone_id == DEFAULT_ZONE:
|
|
||||||
return self.coordinator.data.device_id
|
|
||||||
return f"{self.coordinator.data.device_id}_{self._zone_id}"
|
|
||||||
|
|
||||||
@property
|
|
||||||
def device_name(self):
|
|
||||||
"""Return the name of the current device."""
|
|
||||||
return self.coordinator.data.zones[self._zone_id].name
|
|
||||||
|
|
||||||
@property
|
|
||||||
def device_info(self) -> DeviceInfo:
|
|
||||||
"""Return device information about this MusicCast device."""
|
|
||||||
|
|
||||||
device_info = DeviceInfo(
|
|
||||||
name=self.device_name,
|
|
||||||
identifiers={
|
|
||||||
(
|
|
||||||
DOMAIN,
|
|
||||||
self.device_id,
|
|
||||||
)
|
|
||||||
},
|
|
||||||
manufacturer=BRAND,
|
|
||||||
model=self.coordinator.data.model_name,
|
|
||||||
sw_version=self.coordinator.data.system_version,
|
|
||||||
)
|
|
||||||
|
|
||||||
if self._zone_id == DEFAULT_ZONE:
|
|
||||||
device_info[ATTR_CONNECTIONS] = {
|
|
||||||
(CONNECTION_NETWORK_MAC, format_mac(mac))
|
|
||||||
for mac in self.coordinator.data.mac_addresses.values()
|
|
||||||
}
|
|
||||||
else:
|
|
||||||
device_info[ATTR_VIA_DEVICE] = (DOMAIN, self.coordinator.data.device_id)
|
|
||||||
|
|
||||||
return device_info
|
|
||||||
|
|
||||||
async def async_added_to_hass(self):
|
|
||||||
"""Run when this Entity has been added to HA."""
|
|
||||||
await super().async_added_to_hass()
|
|
||||||
# All entities should register callbacks to update HA when their state changes
|
|
||||||
self.coordinator.musiccast.register_callback(self.async_write_ha_state)
|
|
||||||
|
|
||||||
async def async_will_remove_from_hass(self):
|
|
||||||
"""Entity being removed from hass."""
|
|
||||||
await super().async_will_remove_from_hass()
|
|
||||||
self.coordinator.musiccast.remove_callback(self.async_write_ha_state)
|
|
||||||
|
|
||||||
|
|
||||||
class MusicCastCapabilityEntity(MusicCastDeviceEntity):
|
|
||||||
"""Base Entity type for all capabilities."""
|
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
coordinator: MusicCastDataUpdateCoordinator,
|
|
||||||
capability: Capability,
|
|
||||||
zone_id: str | None = None,
|
|
||||||
) -> None:
|
|
||||||
"""Initialize a capability based entity."""
|
|
||||||
if zone_id is not None:
|
|
||||||
self._zone_id = zone_id
|
|
||||||
self.capability = capability
|
|
||||||
super().__init__(name=capability.name, icon="", coordinator=coordinator)
|
|
||||||
self._attr_entity_category = ENTITY_CATEGORY_MAPPING.get(capability.entity_type)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def unique_id(self) -> str:
|
|
||||||
"""Return the unique ID for this entity."""
|
|
||||||
return f"{self.device_id}_{self.capability.id}"
|
|
||||||
|
112
homeassistant/components/yamaha_musiccast/entity.py
Normal file
112
homeassistant/components/yamaha_musiccast/entity.py
Normal file
@ -0,0 +1,112 @@
|
|||||||
|
"""The MusicCast integration."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from aiomusiccast.capabilities import Capability
|
||||||
|
|
||||||
|
from homeassistant.const import ATTR_CONNECTIONS, ATTR_VIA_DEVICE
|
||||||
|
from homeassistant.helpers.device_registry import (
|
||||||
|
CONNECTION_NETWORK_MAC,
|
||||||
|
DeviceInfo,
|
||||||
|
format_mac,
|
||||||
|
)
|
||||||
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
|
from . import MusicCastDataUpdateCoordinator
|
||||||
|
from .const import BRAND, DEFAULT_ZONE, DOMAIN, ENTITY_CATEGORY_MAPPING
|
||||||
|
|
||||||
|
|
||||||
|
class MusicCastEntity(CoordinatorEntity[MusicCastDataUpdateCoordinator]):
|
||||||
|
"""Defines a base MusicCast entity."""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
*,
|
||||||
|
name: str,
|
||||||
|
icon: str,
|
||||||
|
coordinator: MusicCastDataUpdateCoordinator,
|
||||||
|
enabled_default: bool = True,
|
||||||
|
) -> None:
|
||||||
|
"""Initialize the MusicCast entity."""
|
||||||
|
super().__init__(coordinator)
|
||||||
|
self._attr_entity_registry_enabled_default = enabled_default
|
||||||
|
self._attr_icon = icon
|
||||||
|
self._attr_name = name
|
||||||
|
|
||||||
|
|
||||||
|
class MusicCastDeviceEntity(MusicCastEntity):
|
||||||
|
"""Defines a MusicCast device entity."""
|
||||||
|
|
||||||
|
_zone_id: str = DEFAULT_ZONE
|
||||||
|
|
||||||
|
@property
|
||||||
|
def device_id(self):
|
||||||
|
"""Return the ID of the current device."""
|
||||||
|
if self._zone_id == DEFAULT_ZONE:
|
||||||
|
return self.coordinator.data.device_id
|
||||||
|
return f"{self.coordinator.data.device_id}_{self._zone_id}"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def device_name(self):
|
||||||
|
"""Return the name of the current device."""
|
||||||
|
return self.coordinator.data.zones[self._zone_id].name
|
||||||
|
|
||||||
|
@property
|
||||||
|
def device_info(self) -> DeviceInfo:
|
||||||
|
"""Return device information about this MusicCast device."""
|
||||||
|
|
||||||
|
device_info = DeviceInfo(
|
||||||
|
name=self.device_name,
|
||||||
|
identifiers={
|
||||||
|
(
|
||||||
|
DOMAIN,
|
||||||
|
self.device_id,
|
||||||
|
)
|
||||||
|
},
|
||||||
|
manufacturer=BRAND,
|
||||||
|
model=self.coordinator.data.model_name,
|
||||||
|
sw_version=self.coordinator.data.system_version,
|
||||||
|
)
|
||||||
|
|
||||||
|
if self._zone_id == DEFAULT_ZONE:
|
||||||
|
device_info[ATTR_CONNECTIONS] = {
|
||||||
|
(CONNECTION_NETWORK_MAC, format_mac(mac))
|
||||||
|
for mac in self.coordinator.data.mac_addresses.values()
|
||||||
|
}
|
||||||
|
else:
|
||||||
|
device_info[ATTR_VIA_DEVICE] = (DOMAIN, self.coordinator.data.device_id)
|
||||||
|
|
||||||
|
return device_info
|
||||||
|
|
||||||
|
async def async_added_to_hass(self):
|
||||||
|
"""Run when this Entity has been added to HA."""
|
||||||
|
await super().async_added_to_hass()
|
||||||
|
# All entities should register callbacks to update HA when their state changes
|
||||||
|
self.coordinator.musiccast.register_callback(self.async_write_ha_state)
|
||||||
|
|
||||||
|
async def async_will_remove_from_hass(self):
|
||||||
|
"""Entity being removed from hass."""
|
||||||
|
await super().async_will_remove_from_hass()
|
||||||
|
self.coordinator.musiccast.remove_callback(self.async_write_ha_state)
|
||||||
|
|
||||||
|
|
||||||
|
class MusicCastCapabilityEntity(MusicCastDeviceEntity):
|
||||||
|
"""Base Entity type for all capabilities."""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
coordinator: MusicCastDataUpdateCoordinator,
|
||||||
|
capability: Capability,
|
||||||
|
zone_id: str | None = None,
|
||||||
|
) -> None:
|
||||||
|
"""Initialize a capability based entity."""
|
||||||
|
if zone_id is not None:
|
||||||
|
self._zone_id = zone_id
|
||||||
|
self.capability = capability
|
||||||
|
super().__init__(name=capability.name, icon="", coordinator=coordinator)
|
||||||
|
self._attr_entity_category = ENTITY_CATEGORY_MAPPING.get(capability.entity_type)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def unique_id(self) -> str:
|
||||||
|
"""Return the unique ID for this entity."""
|
||||||
|
return f"{self.device_id}_{self.capability.id}"
|
@ -27,7 +27,7 @@ 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, MusicCastDeviceEntity
|
from . import MusicCastDataUpdateCoordinator
|
||||||
from .const import (
|
from .const import (
|
||||||
ATTR_MAIN_SYNC,
|
ATTR_MAIN_SYNC,
|
||||||
ATTR_MC_LINK,
|
ATTR_MC_LINK,
|
||||||
@ -38,6 +38,7 @@ from .const import (
|
|||||||
MEDIA_CLASS_MAPPING,
|
MEDIA_CLASS_MAPPING,
|
||||||
NULL_GROUP,
|
NULL_GROUP,
|
||||||
)
|
)
|
||||||
|
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, MusicCastCapabilityEntity, MusicCastDataUpdateCoordinator
|
from . import DOMAIN, MusicCastDataUpdateCoordinator
|
||||||
|
from .entity import MusicCastCapabilityEntity
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
|
@ -9,8 +9,9 @@ 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, MusicCastCapabilityEntity, MusicCastDataUpdateCoordinator
|
from . import DOMAIN, MusicCastDataUpdateCoordinator
|
||||||
from .const import TRANSLATION_KEY_MAPPING
|
from .const import TRANSLATION_KEY_MAPPING
|
||||||
|
from .entity import MusicCastCapabilityEntity
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
|
@ -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, MusicCastCapabilityEntity, MusicCastDataUpdateCoordinator
|
from . import DOMAIN, MusicCastDataUpdateCoordinator
|
||||||
|
from .entity import MusicCastCapabilityEntity
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user