Skip to content
Snippets Groups Projects
Unverified Commit 7f473b82 authored by Joost Lekkerkerker's avatar Joost Lekkerkerker Committed by GitHub
Browse files

Prefer a local webhook for Overseerr (#134667)

parent fea4a004
No related branches found
No related tags found
No related merge requests found
......@@ -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
)
......
......@@ -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"
)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment