diff --git a/custom_components/intratone/__init__.py b/custom_components/intratone/__init__.py index d17baf7..0999ef4 100644 --- a/custom_components/intratone/__init__.py +++ b/custom_components/intratone/__init__.py @@ -93,6 +93,42 @@ def report_relay_status( } ) +# Answer call service and hang up service definition + +SERVICE_ANSWER_CALL = "answer_call" +SERVICE_HANG_UP = "hang_up" + +ENTRY_TARGET_SCHEMA = vol.Schema({vol.Optional("entry_id"): cv.string}) + +def _resolve_entries(hass: HomeAssistant, call: ServiceCall) -> list[IntratoneConfigEntry]: + entries = [ + e + for e in hass.config_entries.async_entries(DOMAIN) + if e.state.recoverable and getattr(e, "runtime_data", None) + ] + target = call.data.get("entry_id") + if target: + entries = [e for e in entries if e.entry_id == target] + return entries + + +async def _async_setup_call_services(hass: HomeAssistant) -> None: + async def _answer_call(call: ServiceCall) -> None: + for entry in _resolve_entries(hass, call): + coordinator = entry.runtime_data.coordinator + await coordinator.async_ensure_call_started() + await coordinator.async_wait_for_stream() + + async def _hang_up(call: ServiceCall) -> None: + for entry in _resolve_entries(hass, call): + await entry.runtime_data.call_manager.hang_up() + + hass.services.async_register( + DOMAIN, SERVICE_ANSWER_CALL, _answer_call, schema=ENTRY_TARGET_SCHEMA + ) + hass.services.async_register( + DOMAIN, SERVICE_HANG_UP, _hang_up, schema=ENTRY_TARGET_SCHEMA + ) @dataclass class IntratoneRuntime: @@ -146,8 +182,11 @@ async def _simulate_ring(call: ServiceCall) -> None: hass.services.async_register( DOMAIN, SERVICE_SIMULATE_RING, _simulate_ring, schema=SIMULATE_RING_SCHEMA ) - return True + # Register the answer_call and hang_up services + await _async_setup_call_services(hass) + + return True async def async_setup_entry(hass: HomeAssistant, entry: IntratoneConfigEntry) -> bool: """Set up Intratone from a config entry."""