mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Prefer a local webhook for Overseerr (#134667)
This commit is contained in:
parent
fea4a00424
commit
7f473b8260
@ -61,6 +61,21 @@ class OverseerrWebhookManager:
|
|||||||
self.entry = entry
|
self.entry = entry
|
||||||
self.client = entry.runtime_data.client
|
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:
|
async def register_webhook(self) -> None:
|
||||||
"""Register webhook."""
|
"""Register webhook."""
|
||||||
async_register(
|
async_register(
|
||||||
@ -71,26 +86,26 @@ class OverseerrWebhookManager:
|
|||||||
self.handle_webhook,
|
self.handle_webhook,
|
||||||
allowed_methods=[METH_POST],
|
allowed_methods=[METH_POST],
|
||||||
)
|
)
|
||||||
url = async_generate_url(self.hass, self.entry.data[CONF_WEBHOOK_ID])
|
if not await self.check_need_change():
|
||||||
if not await self.check_need_change(url):
|
|
||||||
return
|
return
|
||||||
LOGGER.debug("Setting Overseerr webhook to %s", url)
|
for url in self.webhook_urls:
|
||||||
if not await self.client.test_webhook_notification_config(url, JSON_PAYLOAD):
|
if await self.client.test_webhook_notification_config(url, JSON_PAYLOAD):
|
||||||
LOGGER.debug("Failed to set Overseerr webhook")
|
LOGGER.debug("Setting Overseerr webhook to %s", url)
|
||||||
return
|
await self.client.set_webhook_notification_config(
|
||||||
await self.client.set_webhook_notification_config(
|
enabled=True,
|
||||||
enabled=True,
|
types=REGISTERED_NOTIFICATIONS,
|
||||||
types=REGISTERED_NOTIFICATIONS,
|
webhook_url=url,
|
||||||
webhook_url=url,
|
json_payload=JSON_PAYLOAD,
|
||||||
json_payload=JSON_PAYLOAD,
|
)
|
||||||
)
|
return
|
||||||
|
LOGGER.error("Failed to set Overseerr webhook")
|
||||||
|
|
||||||
async def check_need_change(self, url: str) -> bool:
|
async def check_need_change(self) -> bool:
|
||||||
"""Check if webhook needs to be changed."""
|
"""Check if webhook needs to be changed."""
|
||||||
current_config = await self.client.get_webhook_notification_config()
|
current_config = await self.client.get_webhook_notification_config()
|
||||||
return (
|
return (
|
||||||
not current_config.enabled
|
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.options.json_payload != json.loads(JSON_PAYLOAD)
|
||||||
or current_config.types != REGISTERED_NOTIFICATIONS
|
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.test_webhook_notification_config.assert_called_once()
|
||||||
mock_overseerr_client.set_webhook_notification_config.assert_not_called()
|
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"
|
||||||
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user