Allow more input params to webhook generate_url helper (#112334)

* allow more params to helper

* switch back to f-string

* add test

* switch to proper method

* add allow_external, internal params

* fx default

* add signature comparison

* remove test, change prefer_external

---------

Co-authored-by: Erik Montnemery <erik@montnemery.com>
This commit is contained in:
Josef Zweck 2024-06-05 15:55:37 +02:00 committed by GitHub
parent 066cd6dbef
commit d0a036c617
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 29 additions and 5 deletions

View File

@ -12,7 +12,7 @@ from pytedee_async.exception import TedeeDataUpdateException, TedeeWebhookExcept
from homeassistant.components.http import HomeAssistantView
from homeassistant.components.webhook import (
async_generate_id as webhook_generate_id,
async_generate_path as webhook_generate_path,
async_generate_url as webhook_generate_url,
async_register as webhook_register,
async_unregister as webhook_unregister,
)
@ -66,8 +66,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: TedeeConfigEntry) -> boo
await coordinator.tedee_client.cleanup_webhooks_by_host(instance_url)
except (TedeeDataUpdateException, TedeeWebhookException) as ex:
_LOGGER.warning("Failed to cleanup Tedee webhooks by host: %s", ex)
webhook_url = (
f"{instance_url}{webhook_generate_path(entry.data[CONF_WEBHOOK_ID])}"
webhook_url = webhook_generate_url(
hass, entry.data[CONF_WEBHOOK_ID], allow_external=False, allow_ip=True
)
webhook_name = "Tedee"
if entry.title != NAME:

View File

@ -87,10 +87,17 @@ def async_generate_id() -> str:
@callback
@bind_hass
def async_generate_url(hass: HomeAssistant, webhook_id: str) -> str:
def async_generate_url(
hass: HomeAssistant,
webhook_id: str,
allow_internal: bool = True,
allow_external: bool = True,
allow_ip: bool | None = None,
prefer_external: bool | None = True,
) -> str:
"""Generate the full URL for a webhook_id."""
return (
f"{get_url(hass, prefer_external=True, allow_cloud=False)}"
f"{get_url(hass,allow_internal=allow_internal, allow_external=allow_external, allow_cloud=False, allow_ip=allow_ip, prefer_external=prefer_external,)}"
f"{async_generate_path(webhook_id)}"
)

View File

@ -56,6 +56,22 @@ async def test_generate_webhook_url(hass: HomeAssistant) -> None:
assert url == "https://example.com/api/webhook/some_id"
async def test_generate_webhook_url_internal(hass: HomeAssistant) -> None:
"""Test we can get the internal URL."""
await async_process_ha_core_config(
hass,
{
"internal_url": "http://192.168.1.100:8123",
"external_url": "https://example.com",
},
)
url = webhook.async_generate_url(
hass, "some_id", allow_external=False, allow_ip=True
)
assert url == "http://192.168.1.100:8123/api/webhook/some_id"
async def test_async_generate_path(hass: HomeAssistant) -> None:
"""Test generating just the path component of the url correctly."""
path = webhook.async_generate_path("some_id")