Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion custom_components/intratone/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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."""
Expand Down
Loading