Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Component to embed Google Cast."""
2 
3 from __future__ import annotations
4 
5 from typing import Protocol
6 
7 from pychromecast import Chromecast
8 
9 from homeassistant.components.media_player import BrowseMedia, MediaType
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.const import Platform
12 from homeassistant.core import HomeAssistant, callback
13 from homeassistant.exceptions import HomeAssistantError
14 from homeassistant.helpers import device_registry as dr
16  async_process_integration_platforms,
17 )
18 
19 from . import home_assistant_cast
20 from .const import DOMAIN
21 
22 PLATFORMS = [Platform.MEDIA_PLAYER]
23 
24 
25 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
26  """Set up Cast from a config entry."""
27  hass.data[DOMAIN] = {"cast_platform": {}, "unknown_models": {}}
28  await home_assistant_cast.async_setup_ha_cast(hass, entry)
29  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
30  await async_process_integration_platforms(hass, DOMAIN, _register_cast_platform)
31  return True
32 
33 
34 class CastProtocol(Protocol):
35  """Define the format of cast platforms."""
36 
38  self, hass: HomeAssistant, cast_type: str
39  ) -> list[BrowseMedia]:
40  """Create a list of root objects for media browsing."""
41 
42  async def async_browse_media(
43  self,
44  hass: HomeAssistant,
45  media_content_type: MediaType | str,
46  media_content_id: str,
47  cast_type: str,
48  ) -> BrowseMedia | None:
49  """Browse media.
50 
51  Return a BrowseMedia object or None if the media does not belong to
52  this platform.
53  """
54 
55  async def async_play_media(
56  self,
57  hass: HomeAssistant,
58  cast_entity_id: str,
59  chromecast: Chromecast,
60  media_type: MediaType | str,
61  media_id: str,
62  ) -> bool:
63  """Play media.
64 
65  Return True if the media is played by the platform, False if not.
66  """
67 
68 
69 @callback
71  hass: HomeAssistant, integration_domain: str, platform: CastProtocol
72 ):
73  """Register a cast platform."""
74  if (
75  not hasattr(platform, "async_get_media_browser_root_object")
76  or not hasattr(platform, "async_browse_media")
77  or not hasattr(platform, "async_play_media")
78  ):
79  raise HomeAssistantError(f"Invalid cast platform {platform}")
80  hass.data[DOMAIN]["cast_platform"][integration_domain] = platform
81 
82 
83 async def async_remove_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
84  """Remove Home Assistant Cast user."""
85  await home_assistant_cast.async_remove_user(hass, entry)
86 
87 
89  hass: HomeAssistant, config_entry: ConfigEntry, device_entry: dr.DeviceEntry
90 ) -> bool:
91  """Remove cast config entry from a device.
92 
93  The actual cleanup is done in CastMediaPlayerEntity.async_will_remove_from_hass.
94  """
95  return True
list[BrowseMedia] async_get_media_browser_root_object(self, HomeAssistant hass, str cast_type)
Definition: __init__.py:39
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:25
bool async_remove_config_entry_device(HomeAssistant hass, ConfigEntry config_entry, dr.DeviceEntry device_entry)
Definition: __init__.py:90
None async_remove_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:83
def _register_cast_platform(HomeAssistant hass, str integration_domain, CastProtocol platform)
Definition: __init__.py:72
bool async_play_media(HomeAssistant hass, str cast_entity_id, Chromecast chromecast, MediaType|str media_type, str media_id)
Definition: cast.py:123
def async_browse_media(hass, media_content_type, media_content_id, platform=None)
Definition: __init__.py:69
None async_process_integration_platforms(HomeAssistant hass, str platform_name, Callable[[HomeAssistant, str, Any], Awaitable[None]|None] process_platform, bool wait_for_platforms=False)