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

Include URI for device identification #18

Merged
merged 3 commits into from
Jul 3, 2024

Conversation

jaminh
Copy link
Contributor

@jaminh jaminh commented Jun 10, 2024

When receiving a call parse the SIP URI from the Contact or From header to use for identifying a device. Using the URI will make it possible to distinguish between multiple endpoints calling through a centralized server (such as Asterisk) which would otherwise appear to come from the same IP address. Keeping track of the Contact header URI will also make it possible to send outbound calls to the device using the correct URI.

When receiving a call parse the SIP URI from the Contact or From header
to use for identifying a device. Using the URI will make it possible to
distinguish between multiple endpoints calling through a centralized
server (such as Asterisk) which would otherwise appear to come from the
same IP address. Keeping track of the Contact header URI will also make
it possible to send outbound calls to the device using the correct URI.
Comment on lines +87 to +91
if (headers.get('contact') is not None):
caller_uri, caller_name = self._parse_uri_header(headers.get('contact'))
# We can try using the From header as a fallback
elif (headers.get('from') is not None):
caller_uri, caller_name = self._parse_uri_header(headers.get('from'))
Copy link
Contributor

Choose a reason for hiding this comment

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

Great opportunity to leverage the walrus

Suggested change
if (headers.get('contact') is not None):
caller_uri, caller_name = self._parse_uri_header(headers.get('contact'))
# We can try using the From header as a fallback
elif (headers.get('from') is not None):
caller_uri, caller_name = self._parse_uri_header(headers.get('from'))
if contact_header := headers.get('contact'):
caller_uri, caller_name = self._parse_uri_header(contact_header)
# We can try using the From header as a fallback
elif from_header := headers.get('from'):
caller_uri, caller_name = self._parse_uri_header(from_header)

@balloob balloob merged commit 394e495 into home-assistant-libs:master Jul 3, 2024
@@ -203,6 +229,26 @@ def answer(
server_rtp_port,
)

def _parse_uri_header(
header: str
Copy link
Contributor

Choose a reason for hiding this comment

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

All methods shuold start with self. The test passed but the code wasn't working. I fixed it in 4d08d66

@jaminh
Copy link
Contributor Author

jaminh commented Jul 4, 2024

I'll admit python is a second language for me, so thank you for the feedback. With this change merged we might want to reopen https://github.com/home-assistant/core/pull/111050/files for the associated changes in HA, although it needs to be updated for the change to using the entire URI rather than parsing the individual parts like I was doing originally.

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

Successfully merging this pull request may close these issues.

2 participants