forked from home-assistant/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
VoIP: Add is active call binary sensor (home-assistant#91486)
* Refactor VoIP integration for more entities * Add active call binary sensor * Add actually missing binary sensor files * Improve test coverage
- Loading branch information
Showing
14 changed files
with
308 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
"""Binary sensor for VoIP.""" | ||
|
||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
from homeassistant.components.binary_sensor import ( | ||
BinarySensorEntity, | ||
BinarySensorEntityDescription, | ||
) | ||
from homeassistant.config_entries import ConfigEntry | ||
from homeassistant.core import HomeAssistant, callback | ||
from homeassistant.helpers.entity_platform import AddEntitiesCallback | ||
|
||
from .const import DOMAIN | ||
from .devices import VoIPDevice | ||
from .entity import VoIPEntity | ||
|
||
if TYPE_CHECKING: | ||
from . import DomainData | ||
|
||
|
||
async def async_setup_entry( | ||
hass: HomeAssistant, | ||
config_entry: ConfigEntry, | ||
async_add_entities: AddEntitiesCallback, | ||
) -> None: | ||
"""Set up VoIP binary sensor entities.""" | ||
domain_data: DomainData = hass.data[DOMAIN] | ||
|
||
@callback | ||
def async_add_device(device: VoIPDevice) -> None: | ||
"""Add device.""" | ||
async_add_entities([VoIPCallActive(device)]) | ||
|
||
domain_data.devices.async_add_new_device_listener(async_add_device) | ||
|
||
async_add_entities([VoIPCallActive(device) for device in domain_data.devices]) | ||
|
||
|
||
class VoIPCallActive(VoIPEntity, BinarySensorEntity): | ||
"""Entity to represent voip is allowed.""" | ||
|
||
entity_description = BinarySensorEntityDescription( | ||
key="call_active", | ||
translation_key="call_active", | ||
) | ||
_attr_is_on = False | ||
|
||
async def async_added_to_hass(self) -> None: | ||
"""Call when entity about to be added to hass.""" | ||
await super().async_added_to_hass() | ||
|
||
self.async_on_remove(self._device.async_listen_update(self._is_active_changed)) | ||
|
||
@callback | ||
def _is_active_changed(self, device: VoIPDevice) -> None: | ||
"""Call when active state changed.""" | ||
self._attr_is_on = self._device.is_active | ||
self.async_write_ha_state() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.