Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Entity representing a Bang & Olufsen device."""
2 
3 from __future__ import annotations
4 
5 from typing import cast
6 
7 from mozart_api.models import (
8  PlaybackContentMetadata,
9  PlaybackProgress,
10  RenderingState,
11  Source,
12  VolumeLevel,
13  VolumeMute,
14  VolumeState,
15 )
16 from mozart_api.mozart_client import MozartClient
17 
18 from homeassistant.config_entries import ConfigEntry
19 from homeassistant.const import CONF_HOST
20 from homeassistant.core import callback
21 from homeassistant.helpers.device_registry import DeviceInfo
22 from homeassistant.helpers.entity import Entity
23 
24 from .const import DOMAIN
25 
26 
28  """Base class for BangOlufsen Home Assistant objects."""
29 
30  def __init__(self, entry: ConfigEntry, client: MozartClient) -> None:
31  """Initialize the object."""
32 
33  # Set the MozartClient
34  self._client_client = client
35 
36  # get the input from the config entry.
37  self.entry: ConfigEntry = entry
38 
39  # Set the configuration variables.
40  self._host: str = self.entry.data[CONF_HOST]
41  self._unique_id: str = cast(str, self.entry.unique_id)
42 
43  # Objects that get directly updated by notifications.
44  self._playback_metadata: PlaybackContentMetadata = PlaybackContentMetadata()
45  self._playback_progress: PlaybackProgress = PlaybackProgress(total_duration=0)
46  self._playback_source: Source = Source()
47  self._playback_state: RenderingState = RenderingState()
48  self._source_change: Source = Source()
49  self._volume: VolumeState = VolumeState(
50  level=VolumeLevel(level=0), muted=VolumeMute(muted=False)
51  )
52 
53 
55  """Base Entity for BangOlufsen entities."""
56 
57  _attr_has_entity_name = True
58  _attr_should_poll = False
59 
60  def __init__(self, entry: ConfigEntry, client: MozartClient) -> None:
61  """Initialize the object."""
62  super().__init__(entry, client)
63 
64  self._attr_device_info_attr_device_info = DeviceInfo(identifiers={(DOMAIN, self._unique_id)})
65 
66  @callback
67  def _async_update_connection_state(self, connection_state: bool) -> None:
68  """Update entity connection state."""
69  self._attr_available_attr_available = connection_state
70 
71  self.async_write_ha_stateasync_write_ha_state()
None __init__(self, ConfigEntry entry, MozartClient client)
Definition: entity.py:30
None __init__(self, ConfigEntry entry, MozartClient client)
Definition: entity.py:60
None _async_update_connection_state(self, bool connection_state)
Definition: entity.py:67