Don't block motionEye setup on NoURLAvailableError (#54225)

Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
This commit is contained in:
Dermot Duffy 2021-08-07 21:29:52 -07:00 committed by GitHub
parent 11f15f66af
commit 75726a2695
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 71 additions and 28 deletions

View file

@ -53,7 +53,7 @@ from homeassistant.helpers.dispatcher import (
async_dispatcher_send,
)
from homeassistant.helpers.entity import DeviceInfo, EntityDescription
from homeassistant.helpers.network import get_url
from homeassistant.helpers.network import NoURLAvailableError, get_url
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
@ -145,12 +145,21 @@ def listen_for_new_cameras(
@callback
def async_generate_motioneye_webhook(hass: HomeAssistant, webhook_id: str) -> str:
def async_generate_motioneye_webhook(
hass: HomeAssistant, webhook_id: str
) -> str | None:
"""Generate the full local URL for a webhook_id."""
try:
return "{}{}".format(
get_url(hass, allow_cloud=False),
async_generate_path(webhook_id),
)
except NoURLAvailableError:
_LOGGER.warning(
"Unable to get Home Assistant URL. Have you set the internal and/or "
"external URLs in Configuration -> General?"
)
return None
@callback
@ -228,7 +237,8 @@ def _add_camera(
if entry.options.get(CONF_WEBHOOK_SET, DEFAULT_WEBHOOK_SET):
url = async_generate_motioneye_webhook(hass, entry.data[CONF_WEBHOOK_ID])
if _set_webhook(
if url and (
_set_webhook(
_build_url(
device,
url,
@ -239,7 +249,8 @@ def _add_camera(
KEY_WEB_HOOK_NOTIFICATIONS_HTTP_METHOD,
KEY_WEB_HOOK_NOTIFICATIONS_ENABLED,
camera,
) | _set_webhook(
)
| _set_webhook(
_build_url(
device,
url,
@ -250,6 +261,7 @@ def _add_camera(
KEY_WEB_HOOK_STORAGE_HTTP_METHOD,
KEY_WEB_HOOK_STORAGE_ENABLED,
camera,
)
):
hass.async_create_task(client.async_set_camera(camera_id, camera))

View file

@ -32,11 +32,13 @@ from homeassistant.const import (
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.network import NoURLAvailableError
from homeassistant.setup import async_setup_component
from . import (
TEST_CAMERA,
TEST_CAMERA_DEVICE_IDENTIFIER,
TEST_CAMERA_ENTITY_ID,
TEST_CAMERA_ID,
TEST_CAMERA_NAME,
TEST_CAMERAS,
@ -251,6 +253,35 @@ async def test_setup_camera_with_correct_webhook(
assert not client.async_set_camera.called
async def test_setup_camera_with_no_home_assistant_urls(
hass: HomeAssistant,
caplog: Any,
) -> None:
"""Verify setup works without Home Assistant internal/external URLs."""
client = create_mock_motioneye_client()
config_entry = create_mock_motioneye_config_entry(hass, data={CONF_URL: TEST_URL})
with patch(
"homeassistant.components.motioneye.get_url", side_effect=NoURLAvailableError
):
await setup_mock_motioneye_config_entry(
hass,
config_entry=config_entry,
client=client,
)
# Should log a warning ...
assert "Unable to get Home Assistant URL" in caplog.text
# ... should not set callbacks in the camera ...
assert not client.async_set_camera.called
# ... but camera should still be present.
entity_state = hass.states.get(TEST_CAMERA_ENTITY_ID)
assert entity_state
async def test_good_query(hass: HomeAssistant, aiohttp_client: Any) -> None:
"""Test good callbacks."""
await async_setup_component(hass, "http", {"http": {}})