From f395688c2df539bc18e76903b58d38a7c5234120 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Mon, 16 Sep 2024 11:17:06 +0200 Subject: [PATCH] Move apple_tv base entity to separate module (#126029) --- homeassistant/components/apple_tv/__init__.py | 77 +++---------------- homeassistant/components/apple_tv/const.py | 3 + homeassistant/components/apple_tv/entity.py | 71 +++++++++++++++++ .../components/apple_tv/media_player.py | 3 +- homeassistant/components/apple_tv/remote.py | 3 +- 5 files changed, 87 insertions(+), 70 deletions(-) create mode 100644 homeassistant/components/apple_tv/entity.py diff --git a/homeassistant/components/apple_tv/__init__.py b/homeassistant/components/apple_tv/__init__.py index d0e414c4e9e..f4417134b37 100644 --- a/homeassistant/components/apple_tv/__init__.py +++ b/homeassistant/components/apple_tv/__init__.py @@ -32,14 +32,16 @@ from homeassistant.core import Event, HomeAssistant, callback from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady from homeassistant.helpers import device_registry as dr from homeassistant.helpers.aiohttp_client import async_get_clientsession -from homeassistant.helpers.device_registry import DeviceInfo -from homeassistant.helpers.dispatcher import ( - async_dispatcher_connect, - async_dispatcher_send, -) -from homeassistant.helpers.entity import Entity +from homeassistant.helpers.dispatcher import async_dispatcher_send -from .const import CONF_CREDENTIALS, CONF_IDENTIFIERS, CONF_START_OFF, DOMAIN +from .const import ( + CONF_CREDENTIALS, + CONF_IDENTIFIERS, + CONF_START_OFF, + DOMAIN, + SIGNAL_CONNECTED, + SIGNAL_DISCONNECTED, +) _LOGGER = logging.getLogger(__name__) @@ -49,9 +51,6 @@ DEFAULT_NAME_HP = "HomePod" BACKOFF_TIME_LOWER_LIMIT = 15 # seconds BACKOFF_TIME_UPPER_LIMIT = 300 # Five minutes -SIGNAL_CONNECTED = "apple_tv_connected" -SIGNAL_DISCONNECTED = "apple_tv_disconnected" - PLATFORMS = [Platform.MEDIA_PLAYER, Platform.REMOTE] AUTH_EXCEPTIONS = ( @@ -120,64 +119,6 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) -class AppleTVEntity(Entity): - """Device that sends commands to an Apple TV.""" - - _attr_should_poll = False - _attr_has_entity_name = True - _attr_name = None - atv: AppleTVInterface | None = None - - def __init__(self, name: str, identifier: str, manager: AppleTVManager) -> None: - """Initialize device.""" - self.manager = manager - self._attr_unique_id = identifier - self._attr_device_info = DeviceInfo( - identifiers={(DOMAIN, identifier)}, - name=name, - ) - - async def async_added_to_hass(self) -> None: - """Handle when an entity is about to be added to Home Assistant.""" - - @callback - def _async_connected(atv: AppleTVInterface) -> None: - """Handle that a connection was made to a device.""" - self.atv = atv - self.async_device_connected(atv) - self.async_write_ha_state() - - @callback - def _async_disconnected() -> None: - """Handle that a connection to a device was lost.""" - self.async_device_disconnected() - self.atv = None - self.async_write_ha_state() - - if self.manager.atv: - # ATV is already connected - _async_connected(self.manager.atv) - - self.async_on_remove( - async_dispatcher_connect( - self.hass, f"{SIGNAL_CONNECTED}_{self.unique_id}", _async_connected - ) - ) - self.async_on_remove( - async_dispatcher_connect( - self.hass, - f"{SIGNAL_DISCONNECTED}_{self.unique_id}", - _async_disconnected, - ) - ) - - def async_device_connected(self, atv: AppleTVInterface) -> None: - """Handle when connection is made to device.""" - - def async_device_disconnected(self) -> None: - """Handle when connection was lost to device.""" - - class AppleTVManager(DeviceListener): """Connection and power manager for an Apple TV. diff --git a/homeassistant/components/apple_tv/const.py b/homeassistant/components/apple_tv/const.py index 5fb169ec259..dd215337f1c 100644 --- a/homeassistant/components/apple_tv/const.py +++ b/homeassistant/components/apple_tv/const.py @@ -6,3 +6,6 @@ CONF_CREDENTIALS = "credentials" CONF_IDENTIFIERS = "identifiers" CONF_START_OFF = "start_off" + +SIGNAL_CONNECTED = "apple_tv_connected" +SIGNAL_DISCONNECTED = "apple_tv_disconnected" diff --git a/homeassistant/components/apple_tv/entity.py b/homeassistant/components/apple_tv/entity.py new file mode 100644 index 00000000000..ad8364e2927 --- /dev/null +++ b/homeassistant/components/apple_tv/entity.py @@ -0,0 +1,71 @@ +"""The Apple TV integration.""" + +from __future__ import annotations + +from pyatv.interface import AppleTV as AppleTVInterface + +from homeassistant.core import callback +from homeassistant.helpers.device_registry import DeviceInfo +from homeassistant.helpers.dispatcher import async_dispatcher_connect +from homeassistant.helpers.entity import Entity + +from . import AppleTVManager +from .const import DOMAIN, SIGNAL_CONNECTED, SIGNAL_DISCONNECTED + + +class AppleTVEntity(Entity): + """Device that sends commands to an Apple TV.""" + + _attr_should_poll = False + _attr_has_entity_name = True + _attr_name = None + atv: AppleTVInterface | None = None + + def __init__(self, name: str, identifier: str, manager: AppleTVManager) -> None: + """Initialize device.""" + self.manager = manager + self._attr_unique_id = identifier + self._attr_device_info = DeviceInfo( + identifiers={(DOMAIN, identifier)}, + name=name, + ) + + async def async_added_to_hass(self) -> None: + """Handle when an entity is about to be added to Home Assistant.""" + + @callback + def _async_connected(atv: AppleTVInterface) -> None: + """Handle that a connection was made to a device.""" + self.atv = atv + self.async_device_connected(atv) + self.async_write_ha_state() + + @callback + def _async_disconnected() -> None: + """Handle that a connection to a device was lost.""" + self.async_device_disconnected() + self.atv = None + self.async_write_ha_state() + + if self.manager.atv: + # ATV is already connected + _async_connected(self.manager.atv) + + self.async_on_remove( + async_dispatcher_connect( + self.hass, f"{SIGNAL_CONNECTED}_{self.unique_id}", _async_connected + ) + ) + self.async_on_remove( + async_dispatcher_connect( + self.hass, + f"{SIGNAL_DISCONNECTED}_{self.unique_id}", + _async_disconnected, + ) + ) + + def async_device_connected(self, atv: AppleTVInterface) -> None: + """Handle when connection is made to device.""" + + def async_device_disconnected(self) -> None: + """Handle when connection was lost to device.""" diff --git a/homeassistant/components/apple_tv/media_player.py b/homeassistant/components/apple_tv/media_player.py index 9fb9dee46e1..c6b71c64b4f 100644 --- a/homeassistant/components/apple_tv/media_player.py +++ b/homeassistant/components/apple_tv/media_player.py @@ -42,8 +42,9 @@ from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity_platform import AddEntitiesCallback import homeassistant.util.dt as dt_util -from . import AppleTvConfigEntry, AppleTVEntity, AppleTVManager +from . import AppleTvConfigEntry, AppleTVManager from .browse_media import build_app_list +from .entity import AppleTVEntity _LOGGER = logging.getLogger(__name__) diff --git a/homeassistant/components/apple_tv/remote.py b/homeassistant/components/apple_tv/remote.py index a93a89cad3e..7f2c9f1b591 100644 --- a/homeassistant/components/apple_tv/remote.py +++ b/homeassistant/components/apple_tv/remote.py @@ -19,7 +19,8 @@ from homeassistant.const import CONF_NAME from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback -from . import AppleTvConfigEntry, AppleTVEntity +from . import AppleTvConfigEntry +from .entity import AppleTVEntity _LOGGER = logging.getLogger(__name__)