Add option to bind client session to specific interface
Proposed change
Add option to select specific interface when getting client session using async_get_clientsession.
Useful in case of host multihoming, eg for async_detect_location_info etc.
Type of change
- [ ] Dependency upgrade
- [ ] Bugfix (non-breaking change which fixes an issue)
- [ ] New integration (thank you!)
- [x] 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:
- Link to developer documentation pull request:
- Link to frontend pull request:
Checklist
- [x] I understand the code I am submitting and can explain how it works.
- [x] The code change is tested and works locally.
- [x] Local tests pass. Your PR cannot be merged unless tests pass
- [x] There is no commented out code in this PR.
- [x] I have followed the development checklist
- [x] I have followed the perfect PR recommendations
- [x] The code has been formatted using Ruff (
ruff format homeassistant tests) - [x] Tests have been added to verify that the new code works.
- [x] Any generated code has been carefully reviewed for correctness and compliance with project standards.
If user exposed functionality or configuration variables are added/changed:
- [ ] Documentation added/updated for www.home-assistant.io
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 runningpython3 -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:
- [ ] I have reviewed two other open pull requests in this repository.
Please elaborate on why this is needed and describe a real use case where this would be used. I cannot follow you on
Useful in case of host multihoming, eg for async_detect_location_info etc.
From the code, each integration would need to specify the interface, and that's for sure something we don't want to do
From the code, each integration would need to specify the interface, and that's for sure something we don't want to do
What do you mean? It's optional.. default behavior remains the same, interface argument is optional.
Okay so, currently, when we call async_detect_location_info w/ a session from async_get_clientsession we cannot control interface selection w/ multihomed hosts.
Let's say you have multiple interfaces configured. Each using different ISP w/ different public IP ranges and want to select specific one used for public IP retrieval (eg for Dynamic DNS like Cloudflare etc.) using async_detect_location_info.
This is currently not possible and it's necessary to implement a custom async_get_clientsession outside the core.
Edit: I also have a use case in mind for Cloudflare integration, but support has to be implemented first. :)
I know that it's optional, but your proposal and use case will only work if all usages are updated to pass in the defined interface. So to support multihoming hosts we need to adjust each integration (adjusting only the few that you need is not something we want, as a different user needs different integrations and so on)...
To even support your use case, additional changes are needed, which are not part of this PR. Please change this PR to include them so we can have the whole picture or link the PRs.
After that, I will put this topic or our next core meeting How do you currently run Home Assistant? Why do you even need a multihoming host, and can this also be achieved on HAOS?
I know that it's optional, but your proposal and use case will only work if all usages are updated to pass in the defined interface.
I'm not sure we understand each other. I don't want to change behavior for all outbound connections.. only when getting public IP for Dynamic DNS updates.
To even support your use case, additional changes are needed, which are not part of this PR. Please change this PR to include them so we can have the whole picture or link the PRs.
You mean changes in Cloudflare integration? It's fairly simple. User would be prompted during configuration (using network integration) to select interface (if there are multiple) and this selected interaface will be used in call of:
async_get_clientsession(hass, interface=interface)
So, do you want me to include these changes in this PR?
How do you currently run Home Assistant? Why do you even need a multihoming host, and can this also be achieved on HAOS?
I have a dedicated connection link (interface) for inbound connections (external access), but it's also a fallback link.
I'm not sure we understand each other. I don't want to change behavior for all outbound connections.. only when getting public IP for Dynamic DNS updates.
But still that means that you need to change a couple of integrations and all the config flows need updates... And again, we should aim for a solution for all users and not just for you, and the current proposed solution is, in my opinion, more work as it solves as any integration supporting it needs to update it.
As I already asked above, please extend this PR so we can see the big picture and how it's used. We will not merge something that is not used anywhere
I've added the requested PoC changes. 😉