Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VoIP: Add support for announce #135291

Open
wants to merge 11 commits into
base: dev
Choose a base branch
from

Conversation

jaminh
Copy link
Contributor

@jaminh jaminh commented Jan 10, 2025

Breaking change

Proposed change

This adds the "Announce" service to the VOIP Assist Satellite component. Currently when announce is called HA will call the phone and go into the standard assist pipeline when the call is answered.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.

To help with the load of incoming pull requests:

@home-assistant
Copy link

Hey there @balloob, @synesthesiam, mind taking a look at this pull request as it has been labeled with an integration (voip) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of voip can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign voip Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

@jaminh
Copy link
Contributor Author

jaminh commented Jan 10, 2025

We will need to add some way for the HassVoipDatagramProtocol to pick an appropriate protocol factory for the different use cases: HA receiving a call, HA calling out for an announcement, and HA calling out as part of the start_conversation service.

I imagine at some point we will want to account for what happens when one of those services, announce or start_conversation, is called while there is an active call with the target VOIP device. Currently the code assumes there is not an active call and will always start by calling the phone.

),
): str,
vol.Required(
CONF_SIP_HOST,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we want to ask the user these values?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this to get the IP address of the HA box? We should instead import async_get_source_ip from homeassistant.components.network

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that guaranteed to be the external IP for the HA box? I think there would still be value in allowing the host sent in the outgoing invite message to be configurable, but using async_get_source_ip as a default value would make sense. Depending on SIP server configurations a specific user might be required in the INVITE From header, so even though that configuration isn't officially supported, it is helpful for me. If we make those optional with sane defaults is that still a workable solution?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't want them in the main configuration flow, as users will be confused (we want to ask as little information as possible when setting up an integration).

Can you try if it works for you with async_get_source_ip ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved user and host to an "advanced options" page and defaulted host to the value of async_get_source_ip, I hope that is an acceptable solution?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, let's stick with just calling async_get_source_ip when we need to do an announcement. That method exists to get the right IP address for exactly this reason:

@bind_hass
async def async_get_source_ip(
    hass: HomeAssistant, target_ip: str | UndefinedType = UNDEFINED
) -> str:
    """Get the source ip for a target ip."""

Adding this option puts an extra burden on the user having a bunch of IP addresses configured hidden inside config entry configurations.

voip_id = (
call_info.caller_endpoint.uri
if call_info.caller_endpoint
else call_info.caller_ip
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do an auto-migration here? So every incoming device, we check if it exists under the old ID, and if so, we migrate it to the new endpoint uri.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to update it to do an auto-migration, but it would be difficult for me to test that since I already have new devices for all of my phones.

@balloob balloob changed the title Voip assist sat announce VoIP: Add support for announce Jan 11, 2025
Copy link
Member

@balloob balloob left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you break out the caller ID breaking change into its own PR? I also added a note how we can try to auto-migrate old devices automatically. Would be cool to add to that PR if possible.

@home-assistant
Copy link

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@home-assistant home-assistant bot marked this pull request as draft January 11, 2025 01:51
@balloob
Copy link
Member

balloob commented Jan 11, 2025

Currently when announce is called HA will call the phone and go into the standard assist pipeline when the call is answered.

We should also make sure that we start with speaking the announcement message. Once we did that, Home Assistant should hang up. Users should use start_conversation if they want to continue talking.

@jaminh jaminh force-pushed the voip-assist-sat-announce branch from 430dda1 to a4b7063 Compare January 12, 2025 20:44
@jaminh
Copy link
Contributor Author

jaminh commented Jan 14, 2025

Can you break out the caller ID breaking change into its own PR? I also added a note how we can try to auto-migrate old devices automatically. Would be cool to add to that PR if possible.

#135603

@jaminh jaminh force-pushed the voip-assist-sat-announce branch from 293d6b6 to 8216fd1 Compare January 16, 2025 02:19
@@ -416,6 +416,7 @@ async def _resolve_announcement_media_id(
language=pipeline.tts_language,
options=tts_options,
)
return AssistSatelliteAnnouncement(message, media_id, media_id_source)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this has the potential to affect other assist satellite components, but the VOIP announcements wouldn't work otherwise.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added an internal_media_id property to the AssistSatelliteAnnouncement to hopefully mitigate this issue.

@jaminh jaminh marked this pull request as ready for review January 18, 2025 02:45
@jaminh jaminh requested a review from a team as a code owner January 18, 2025 02:45
@home-assistant home-assistant bot requested a review from balloob January 18, 2025 02:45
@jaminh
Copy link
Contributor Author

jaminh commented Jan 20, 2025

Currently when announce is called HA will call the phone and go into the standard assist pipeline when the call is answered.

We should also make sure that we start with speaking the announcement message. Once we did that, Home Assistant should hang up. Users should use start_conversation if they want to continue talking.

This PR is already pretty unwieldy as it is, I figure we can get this stable and add start_conversation support as a separate PR.

@@ -96,7 +96,11 @@ class AssistSatelliteAnnouncement:
media_id: str
"""Media ID to be played."""

internal_media_id: str
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've extracted this as #136141 and renamed to original_media_id

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I rebase this branch to pick up that change or clean that up later?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rebase.

def on_call(self, call_info: CallInfo):
"""Set up state when starting a call."""
device = self.devices.async_get_or_create(call_info)
if device is not None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The response can never be None.

"""Set up state when starting a call."""
device = self.devices.async_get_or_create(call_info)
if device is not None:
device.current_call = call_info
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this something that we want to pass to set_active instead? Having a call info then indicates it's active.

@@ -126,6 +126,9 @@ async def async_process_audio_stream(
text = transcript.text
break

except TimeoutError:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this added?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without it the call state wouldn't get properly cleaned up which I think was causing subsequent calls to not create a new RTP server and there would be no audio on the phone.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed it in the latest change, I am still having issues with it but it is not the same as before so that might have been a red herring when I added it the first time. I think I need to increase the timeout value because it seems like when I say a command it will make the tone like it heard the command, but then shortly after it hangs up on me from what I assume is caused by the timeout error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this back in because hanging up after the regular command pipeline loop wouldn't work without it.

@synesthesiam
Copy link
Contributor

We need to add an implementation for this still, right?
TypeError: Can't instantiate abstract class HassVoipDatagramProtocol without an implementation for abstract method 'on_hangup'

@jaminh
Copy link
Contributor Author

jaminh commented Jan 22, 2025

We need to add an implementation for this still, right? TypeError: Can't instantiate abstract class HassVoipDatagramProtocol without an implementation for abstract method 'on_hangup'

It should be implemented, line 134 homeassistant/components/voip/voip.py

@balloob
Copy link
Member

balloob commented Jan 23, 2025

It's not explicitely mentioned, but this PR requires home-assistant-libs/voip-utils#25 to be merged and released right?

@jaminh
Copy link
Contributor Author

jaminh commented Jan 23, 2025

It's not explicitely mentioned, but this PR requires home-assistant-libs/voip-utils#25 to be merged and released right?

Yes, is there a way to specify an unreleased version in the manifest?

Edit: I think I found it, https://developers.home-assistant.io/docs/creating_integration_manifest/#custom-requirements-during-development--testing

Currently when making an announcement the phone will be called and will
go into the regular assist pipeline when the call is answered.
The create rtp server method is in voip_utils
Made a queue of pipeline operations that can be added to in case
the announcement service is called during an ongoing call.
Improve task queue handling by using put_nowait for adding tasks to
avoid the need for creating a task for adding items. The task queue
should also be cleared when the call ends. If the call needs to end,
either because it is the end of an announcement or there was an
unrecoverable error, HA should initiate a hang up in an attempt to
cleanly end the call.
@jaminh jaminh force-pushed the voip-assist-sat-announce branch from 6796078 to 4897501 Compare January 23, 2025 14:14
@jaminh
Copy link
Contributor Author

jaminh commented Jan 24, 2025

The pre-commit checks complain if I try to set the voip_utils requirement to point to my branch, so I left that as is.

I am still having an issue where when I hang up after doing the regular voice command pipeline stuff the TimeoutError isn't being caught in the _run_pipeline method which means the call isn't hung up. This means if I try to then play an announcement after that it thinks the previous call is ongoing and tries to play the announcement on that non-existent call. I still haven't figured out a way to get that to work such that it hangs up after I hang up my phone without it randomly hanging up in the middle of processing commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants