From 7f473b8260f4120f2151f9a300fa49b382bdc449 Mon Sep 17 00:00:00 2001 From: Joost Lekkerkerker <joostlek@outlook.com> Date: Sat, 4 Jan 2025 15:39:47 +0100 Subject: [PATCH] Prefer a local webhook for Overseerr (#134667) --- .../components/overseerr/__init__.py | 45 ++++++++++++------- tests/components/overseerr/test_init.py | 22 +++++++++ 2 files changed, 52 insertions(+), 15 deletions(-) diff --git a/homeassistant/components/overseerr/__init__.py b/homeassistant/components/overseerr/__init__.py index db39a0d3e92..0148dc27fdd 100644 --- a/homeassistant/components/overseerr/__init__.py +++ b/homeassistant/components/overseerr/__init__.py @@ -61,6 +61,21 @@ class OverseerrWebhookManager: self.entry = entry self.client = entry.runtime_data.client + @property + def webhook_urls(self) -> list[str]: + """Return webhook URLs.""" + urls = [ + async_generate_url( + self.hass, self.entry.data[CONF_WEBHOOK_ID], prefer_external=external + ) + for external in (False, True) + ] + res = [] + for url in urls: + if url not in res: + res.append(url) + return res + async def register_webhook(self) -> None: """Register webhook.""" async_register( @@ -71,26 +86,26 @@ class OverseerrWebhookManager: self.handle_webhook, allowed_methods=[METH_POST], ) - url = async_generate_url(self.hass, self.entry.data[CONF_WEBHOOK_ID]) - if not await self.check_need_change(url): - return - LOGGER.debug("Setting Overseerr webhook to %s", url) - if not await self.client.test_webhook_notification_config(url, JSON_PAYLOAD): - LOGGER.debug("Failed to set Overseerr webhook") + if not await self.check_need_change(): return - await self.client.set_webhook_notification_config( - enabled=True, - types=REGISTERED_NOTIFICATIONS, - webhook_url=url, - json_payload=JSON_PAYLOAD, - ) - - async def check_need_change(self, url: str) -> bool: + for url in self.webhook_urls: + if await self.client.test_webhook_notification_config(url, JSON_PAYLOAD): + LOGGER.debug("Setting Overseerr webhook to %s", url) + await self.client.set_webhook_notification_config( + enabled=True, + types=REGISTERED_NOTIFICATIONS, + webhook_url=url, + json_payload=JSON_PAYLOAD, + ) + return + LOGGER.error("Failed to set Overseerr webhook") + + async def check_need_change(self) -> bool: """Check if webhook needs to be changed.""" current_config = await self.client.get_webhook_notification_config() return ( not current_config.enabled - or current_config.options.webhook_url != url + or current_config.options.webhook_url not in self.webhook_urls or current_config.options.json_payload != json.loads(JSON_PAYLOAD) or current_config.types != REGISTERED_NOTIFICATIONS ) diff --git a/tests/components/overseerr/test_init.py b/tests/components/overseerr/test_init.py index 853859d9b39..0962cd2c2f1 100644 --- a/tests/components/overseerr/test_init.py +++ b/tests/components/overseerr/test_init.py @@ -128,3 +128,25 @@ async def test_webhook_failing_test( mock_overseerr_client.test_webhook_notification_config.assert_called_once() mock_overseerr_client.set_webhook_notification_config.assert_not_called() + + +async def test_prefer_internal_ip( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_overseerr_client: AsyncMock, +) -> None: + """Test the integration prefers internal IP.""" + mock_overseerr_client.test_webhook_notification_config.return_value = False + hass.config.internal_url = "http://192.168.0.123:8123" + hass.config.external_url = "https://www.example.com" + await hass.async_block_till_done(wait_background_tasks=True) + await setup_integration(hass, mock_config_entry) + + assert ( + mock_overseerr_client.test_webhook_notification_config.call_args_list[0][0][0] + == "http://192.168.0.123:8123/api/webhook/test-webhook-id" + ) + assert ( + mock_overseerr_client.test_webhook_notification_config.call_args_list[1][0][0] + == "https://www.example.com/api/webhook/test-webhook-id" + ) -- GitLab