Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Entity representing a Sonos power sensor."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
9  BinarySensorDeviceClass,
10  BinarySensorEntity,
11 )
12 from homeassistant.config_entries import ConfigEntry
13 from homeassistant.const import EntityCategory
14 from homeassistant.core import HomeAssistant, callback
15 from homeassistant.helpers.dispatcher import async_dispatcher_connect
16 from homeassistant.helpers.entity_platform import AddEntitiesCallback
17 
18 from .const import SONOS_CREATE_BATTERY, SONOS_CREATE_MIC_SENSOR
19 from .entity import SonosEntity
20 from .helpers import soco_error
21 from .speaker import SonosSpeaker
22 
23 ATTR_BATTERY_POWER_SOURCE = "power_source"
24 
25 _LOGGER = logging.getLogger(__name__)
26 
27 
29  hass: HomeAssistant,
30  config_entry: ConfigEntry,
31  async_add_entities: AddEntitiesCallback,
32 ) -> None:
33  """Set up Sonos from a config entry."""
34 
35  @callback
36  def _async_create_battery_entity(speaker: SonosSpeaker) -> None:
37  _LOGGER.debug("Creating battery binary_sensor on %s", speaker.zone_name)
38  entity = SonosPowerEntity(speaker)
39  async_add_entities([entity])
40 
41  @callback
42  def _async_create_mic_entity(speaker: SonosSpeaker) -> None:
43  _LOGGER.debug("Creating microphone binary_sensor on %s", speaker.zone_name)
45 
46  config_entry.async_on_unload(
48  hass, SONOS_CREATE_BATTERY, _async_create_battery_entity
49  )
50  )
51 
52  config_entry.async_on_unload(
54  hass, SONOS_CREATE_MIC_SENSOR, _async_create_mic_entity
55  )
56  )
57 
58 
60  """Representation of a Sonos power entity."""
61 
62  _attr_entity_category = EntityCategory.DIAGNOSTIC
63  _attr_device_class = BinarySensorDeviceClass.BATTERY_CHARGING
64 
65  def __init__(self, speaker: SonosSpeaker) -> None:
66  """Initialize the power entity binary sensor."""
67  super().__init__(speaker)
68  self._attr_unique_id_attr_unique_id = f"{self.soco.uid}-power"
69 
70  async def _async_fallback_poll(self) -> None:
71  """Poll the device for the current state."""
72  await self.speakerspeaker.async_poll_battery()
73 
74  @property
75  def is_on(self) -> bool | None:
76  """Return the state of the binary sensor."""
77  return self.speakerspeaker.charging
78 
79  @property
80  def extra_state_attributes(self) -> dict[str, Any]:
81  """Return entity specific state attributes."""
82  return {
83  ATTR_BATTERY_POWER_SOURCE: self.speakerspeaker.power_source,
84  }
85 
86  @property
87  def available(self) -> bool:
88  """Return whether this device is available."""
89  return self.speakerspeaker.available and (self.speakerspeaker.charging is not None)
90 
91 
93  """Representation of a Sonos microphone sensor entity."""
94 
95  _attr_entity_category = EntityCategory.DIAGNOSTIC
96  _attr_translation_key = "microphone"
97 
98  def __init__(self, speaker: SonosSpeaker) -> None:
99  """Initialize the microphone binary sensor entity."""
100  super().__init__(speaker)
101  self._attr_unique_id_attr_unique_id = f"{self.soco.uid}-microphone"
102 
103  async def _async_fallback_poll(self) -> None:
104  """Handle polling when subscription fails."""
105  await self.hasshass.async_add_executor_job(self.poll_statepoll_state)
106 
107  @soco_error()
108  def poll_state(self) -> None:
109  """Poll the current state of the microphone."""
110  self.speakerspeaker.mic_enabled = self.socosoco.mic_enabled
111 
112  @property
113  def is_on(self) -> bool | None:
114  """Return the state of the binary sensor."""
115  return self.speakerspeaker.mic_enabled
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103