VoIP: Add is active call binary sensor (#91486)

* Refactor VoIP integration for more entities

* Add active call binary sensor

* Add actually missing binary sensor files

* Improve test coverage
This commit is contained in:
Paulus Schoutsen
2023-04-16 22:59:05 -04:00
committed by GitHub
parent 58ea657fbc
commit 2b6fd0df6a
14 changed files with 305 additions and 84 deletions

View File

@@ -21,7 +21,7 @@ from homeassistant.const import __version__
from homeassistant.core import HomeAssistant
if TYPE_CHECKING:
from .devices import VoIPDevices
from .devices import VoIPDevice, VoIPDevices
_LOGGER = logging.getLogger(__name__)
@@ -41,13 +41,16 @@ class HassVoipDatagramProtocol(VoipDatagramProtocol):
protocol_factory=lambda call_info: PipelineRtpDatagramProtocol(
hass,
hass.config.language,
devices.async_get_or_create(call_info),
),
)
self.hass = hass
self.devices = devices
def is_valid_call(self, call_info: CallInfo) -> bool:
"""Filter calls."""
return self.devices.async_allow_call(call_info)
device = self.devices.async_get_or_create(call_info)
return device.async_allow_call(self.hass)
class PipelineRtpDatagramProtocol(RtpDatagramProtocol):
@@ -57,6 +60,7 @@ class PipelineRtpDatagramProtocol(RtpDatagramProtocol):
self,
hass: HomeAssistant,
language: str,
voip_device: VoIPDevice,
pipeline_timeout: float = 30.0,
audio_timeout: float = 2.0,
) -> None:
@@ -66,6 +70,7 @@ class PipelineRtpDatagramProtocol(RtpDatagramProtocol):
self.hass = hass
self.language = language
self.voip_device = voip_device
self.pipeline: Pipeline | None = None
self.pipeline_timeout = pipeline_timeout
self.audio_timeout = audio_timeout
@@ -76,7 +81,13 @@ class PipelineRtpDatagramProtocol(RtpDatagramProtocol):
def connection_made(self, transport):
"""Server is ready."""
self.transport = transport
super().connection_made(transport)
self.voip_device.set_is_active(True)
def connection_lost(self, exc):
"""Handle connection is lost or closed."""
super().connection_lost(exc)
self.voip_device.set_is_active(False)
def on_chunk(self, audio_bytes: bytes) -> None:
"""Handle raw audio chunk."""