diff --git a/homeassistant/components/withings/__init__.py b/homeassistant/components/withings/__init__.py index 05f2db4b18d..548d230f325 100644 --- a/homeassistant/components/withings/__init__.py +++ b/homeassistant/components/withings/__init__.py @@ -15,6 +15,7 @@ from aiohttp.web import Request, Response from aiowithings import NotificationCategory, WithingsClient from aiowithings.util import to_enum import voluptuous as vol +from yarl import URL from homeassistant.components import cloud from homeassistant.components.application_credentials import ( @@ -179,8 +180,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: webhook_url = await _async_cloudhook_generate_url(hass, entry) else: webhook_url = webhook_generate_url(hass, entry.data[CONF_WEBHOOK_ID]) - - if not webhook_url.startswith("https://"): + url = URL(webhook_url) + if url.scheme != "https" or url.port != 443: LOGGER.warning( "Webhook not registered - " "https and port 443 is required to register the webhook" diff --git a/tests/components/withings/__init__.py b/tests/components/withings/__init__.py index 4d9a0e841b7..9693d21f162 100644 --- a/tests/components/withings/__init__.py +++ b/tests/components/withings/__init__.py @@ -51,7 +51,7 @@ async def setup_integration( if enable_webhooks: await async_process_ha_core_config( hass, - {"external_url": "https://example.local:8123"}, + {"external_url": "https://example.com"}, ) await hass.config_entries.async_setup(config_entry.entry_id) diff --git a/tests/components/withings/fixtures/notify_list.json b/tests/components/withings/fixtures/notify_list.json index 5b368a5c979..ef7a99857e4 100644 --- a/tests/components/withings/fixtures/notify_list.json +++ b/tests/components/withings/fixtures/notify_list.json @@ -8,13 +8,13 @@ }, { "appli": 50, - "callbackurl": "https://example.local:8123/api/webhook/55a7335ea8dee830eed4ef8f84cda8f6d80b83af0847dc74032e86120bffed5e", + "callbackurl": "https://example.com/api/webhook/55a7335ea8dee830eed4ef8f84cda8f6d80b83af0847dc74032e86120bffed5e", "expires": 2147483647, "comment": null }, { "appli": 51, - "callbackurl": "https://example.local:8123/api/webhook/55a7335ea8dee830eed4ef8f84cda8f6d80b83af0847dc74032e86120bffed5e", + "callbackurl": "https://example.com/api/webhook/55a7335ea8dee830eed4ef8f84cda8f6d80b83af0847dc74032e86120bffed5e", "expires": 2147483647, "comment": null } diff --git a/tests/components/withings/test_init.py b/tests/components/withings/test_init.py index 72b9b495344..9418b032a02 100644 --- a/tests/components/withings/test_init.py +++ b/tests/components/withings/test_init.py @@ -130,7 +130,7 @@ async def test_data_manager_webhook_subscription( assert withings.subscribe_notification.call_count == 6 - webhook_url = "https://example.local:8123/api/webhook/55a7335ea8dee830eed4ef8f84cda8f6d80b83af0847dc74032e86120bffed5e" + webhook_url = "https://example.com/api/webhook/55a7335ea8dee830eed4ef8f84cda8f6d80b83af0847dc74032e86120bffed5e" withings.subscribe_notification.assert_any_call( webhook_url, NotificationCategory.WEIGHT @@ -428,12 +428,14 @@ async def test_setup_with_cloud( assert not hass.config_entries.async_entries(DOMAIN) -async def test_setup_without_https( +@pytest.mark.parametrize("url", ["http://example.com", "https://example.com:444"]) +async def test_setup_no_webhook( hass: HomeAssistant, webhook_config_entry: MockConfigEntry, withings: AsyncMock, caplog: pytest.LogCaptureFixture, freezer: FrozenDateTimeFactory, + url: str, ) -> None: """Test if set up with cloud link and without https.""" hass.config.components.add("cloud") @@ -445,7 +447,7 @@ async def test_setup_without_https( ), patch( "homeassistant.components.withings.webhook_generate_url" ) as mock_async_generate_url: - mock_async_generate_url.return_value = "http://example.com" + mock_async_generate_url.return_value = url await setup_integration(hass, webhook_config_entry) await prepare_webhook_setup(hass, freezer)