Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Binary sensor for VoIP."""
2 
3 from __future__ import annotations
4 
5 from typing import TYPE_CHECKING
6 
8  BinarySensorEntity,
9  BinarySensorEntityDescription,
10 )
11 from homeassistant.config_entries import ConfigEntry
12 from homeassistant.core import HomeAssistant, callback
13 from homeassistant.helpers import issue_registry as ir
14 from homeassistant.helpers.entity_platform import AddEntitiesCallback
15 
16 from .const import DOMAIN
17 from .devices import VoIPDevice
18 from .entity import VoIPEntity
19 
20 if TYPE_CHECKING:
21  from . import DomainData
22 
23 
25  hass: HomeAssistant,
26  config_entry: ConfigEntry,
27  async_add_entities: AddEntitiesCallback,
28 ) -> None:
29  """Set up VoIP binary sensor entities."""
30  domain_data: DomainData = hass.data[DOMAIN]
31 
32  @callback
33  def async_add_device(device: VoIPDevice) -> None:
34  """Add device."""
36 
37  domain_data.devices.async_add_new_device_listener(async_add_device)
38 
39  async_add_entities([VoIPCallInProgress(device) for device in domain_data.devices])
40 
41 
43  """Entity to represent voip call is in progress."""
44 
45  entity_description = BinarySensorEntityDescription(
46  entity_registry_enabled_default=False,
47  key="call_in_progress",
48  translation_key="call_in_progress",
49  )
50  _attr_is_on = False
51 
52  async def async_added_to_hass(self) -> None:
53  """Call when entity about to be added to hass."""
54  await super().async_added_to_hass()
55 
56  self.async_on_removeasync_on_remove(
57  self.voip_devicevoip_device.async_listen_update(self._is_active_changed_is_active_changed)
58  )
59 
60  await super().async_added_to_hass()
61  if TYPE_CHECKING:
62  assert self.registry_entryregistry_entry is not None
63  ir.async_create_issue(
64  self.hasshass,
65  DOMAIN,
66  f"assist_in_progress_deprecated_{self.registry_entry.id}",
67  breaks_in_ha_version="2025.4",
68  data={
69  "entity_id": self.entity_identity_id,
70  "entity_uuid": self.registry_entryregistry_entry.id,
71  "integration_name": "VoIP",
72  },
73  is_fixable=True,
74  severity=ir.IssueSeverity.WARNING,
75  translation_key="assist_in_progress_deprecated",
76  translation_placeholders={
77  "integration_name": "VoIP",
78  },
79  )
80 
81  async def async_will_remove_from_hass(self) -> None:
82  """Remove issue."""
83  await super().async_will_remove_from_hass()
84  if TYPE_CHECKING:
85  assert self.registry_entryregistry_entry is not None
86  ir.async_delete_issue(
87  self.hasshass,
88  DOMAIN,
89  f"assist_in_progress_deprecated_{self.registry_entry.id}",
90  )
91 
92  @callback
93  def _is_active_changed(self, device: VoIPDevice) -> None:
94  """Call when active state changed."""
95  self._attr_is_on_attr_is_on_attr_is_on = self.voip_devicevoip_device.is_active
96  self.async_write_ha_stateasync_write_ha_state()
None async_on_remove(self, CALLBACK_TYPE func)
Definition: entity.py:1331
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)