mirror of
https://github.com/home-assistant/core.git
synced 2025-07-28 15:47:12 +00:00
Move apple_tv base entity to separate module (#126029)
This commit is contained in:
parent
2e76b1f834
commit
f395688c2d
@ -32,14 +32,16 @@ from homeassistant.core import Event, HomeAssistant, callback
|
|||||||
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
||||||
from homeassistant.helpers import device_registry as dr
|
from homeassistant.helpers import device_registry as dr
|
||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
from homeassistant.helpers.device_registry import DeviceInfo
|
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||||
from homeassistant.helpers.dispatcher import (
|
|
||||||
async_dispatcher_connect,
|
|
||||||
async_dispatcher_send,
|
|
||||||
)
|
|
||||||
from homeassistant.helpers.entity import Entity
|
|
||||||
|
|
||||||
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__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -49,9 +51,6 @@ DEFAULT_NAME_HP = "HomePod"
|
|||||||
BACKOFF_TIME_LOWER_LIMIT = 15 # seconds
|
BACKOFF_TIME_LOWER_LIMIT = 15 # seconds
|
||||||
BACKOFF_TIME_UPPER_LIMIT = 300 # Five minutes
|
BACKOFF_TIME_UPPER_LIMIT = 300 # Five minutes
|
||||||
|
|
||||||
SIGNAL_CONNECTED = "apple_tv_connected"
|
|
||||||
SIGNAL_DISCONNECTED = "apple_tv_disconnected"
|
|
||||||
|
|
||||||
PLATFORMS = [Platform.MEDIA_PLAYER, Platform.REMOTE]
|
PLATFORMS = [Platform.MEDIA_PLAYER, Platform.REMOTE]
|
||||||
|
|
||||||
AUTH_EXCEPTIONS = (
|
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)
|
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):
|
class AppleTVManager(DeviceListener):
|
||||||
"""Connection and power manager for an Apple TV.
|
"""Connection and power manager for an Apple TV.
|
||||||
|
|
||||||
|
@ -6,3 +6,6 @@ CONF_CREDENTIALS = "credentials"
|
|||||||
CONF_IDENTIFIERS = "identifiers"
|
CONF_IDENTIFIERS = "identifiers"
|
||||||
|
|
||||||
CONF_START_OFF = "start_off"
|
CONF_START_OFF = "start_off"
|
||||||
|
|
||||||
|
SIGNAL_CONNECTED = "apple_tv_connected"
|
||||||
|
SIGNAL_DISCONNECTED = "apple_tv_disconnected"
|
||||||
|
71
homeassistant/components/apple_tv/entity.py
Normal file
71
homeassistant/components/apple_tv/entity.py
Normal file
@ -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."""
|
@ -42,8 +42,9 @@ from homeassistant.core import HomeAssistant, callback
|
|||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
import homeassistant.util.dt as dt_util
|
import homeassistant.util.dt as dt_util
|
||||||
|
|
||||||
from . import AppleTvConfigEntry, AppleTVEntity, AppleTVManager
|
from . import AppleTvConfigEntry, AppleTVManager
|
||||||
from .browse_media import build_app_list
|
from .browse_media import build_app_list
|
||||||
|
from .entity import AppleTVEntity
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -19,7 +19,8 @@ from homeassistant.const import CONF_NAME
|
|||||||
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 AppleTvConfigEntry, AppleTVEntity
|
from . import AppleTvConfigEntry
|
||||||
|
from .entity import AppleTVEntity
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user