From e349608f92104740f4b17e484b6f5635cff1fcfe Mon Sep 17 00:00:00 2001 From: nic <31355096+nabbi@users.noreply.github.com> Date: Mon, 8 Jan 2024 13:30:44 -0600 Subject: [PATCH] Retry zoneminder connection setup (#107519) * zoneminder setup retry connection Makes ZM setup be async for enabling connection retry attempts This also requires zm-py version bump v0.5.4 as that dependency was patched in conjunction to resolve this issue Closes #105271 Signed-off-by: Nic Boet * ruff format Signed-off-by: Nic Boet * ruff fixes Signed-off-by: Nic Boet * RequestsConnectionError Signed-off-by: Nic Boet * revert async changes Signed-off-by: Nic Boet --------- Signed-off-by: Nic Boet --- homeassistant/components/zoneminder/__init__.py | 10 +++++++++- homeassistant/components/zoneminder/camera.py | 6 ++++-- homeassistant/components/zoneminder/manifest.json | 2 +- homeassistant/components/zoneminder/sensor.py | 5 ++++- homeassistant/components/zoneminder/switch.py | 6 ++++-- requirements_all.txt | 2 +- 6 files changed, 23 insertions(+), 8 deletions(-) diff --git a/homeassistant/components/zoneminder/__init__.py b/homeassistant/components/zoneminder/__init__.py index 5e9c881af85..1ff73048440 100644 --- a/homeassistant/components/zoneminder/__init__.py +++ b/homeassistant/components/zoneminder/__init__.py @@ -1,6 +1,7 @@ """Support for ZoneMinder.""" import logging +from requests.exceptions import ConnectionError as RequestsConnectionError import voluptuous as vol from zoneminder.zm import ZoneMinder @@ -75,7 +76,14 @@ def setup(hass: HomeAssistant, config: ConfigType) -> bool: ) hass.data[DOMAIN][host_name] = zm_client - success = zm_client.login() and success + try: + success = zm_client.login() and success + except RequestsConnectionError as ex: + _LOGGER.error( + "ZoneMinder connection failure to %s: %s", + host_name, + ex, + ) def set_active_state(call: ServiceCall) -> None: """Set the ZoneMinder run state to the given state name.""" diff --git a/homeassistant/components/zoneminder/camera.py b/homeassistant/components/zoneminder/camera.py index e87e6f814cc..d8b2aa805e7 100644 --- a/homeassistant/components/zoneminder/camera.py +++ b/homeassistant/components/zoneminder/camera.py @@ -8,6 +8,7 @@ from zoneminder.zm import ZoneMinder from homeassistant.components.mjpeg import MjpegCamera, filter_urllib3_logging from homeassistant.core import HomeAssistant +from homeassistant.exceptions import PlatformNotReady from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType @@ -28,8 +29,9 @@ def setup_platform( zm_client: ZoneMinder for zm_client in hass.data[ZONEMINDER_DOMAIN].values(): if not (monitors := zm_client.get_monitors()): - _LOGGER.warning("Could not fetch monitors from ZoneMinder host: %s") - return + raise PlatformNotReady( + "Camera could not fetch any monitors from ZoneMinder" + ) for monitor in monitors: _LOGGER.info("Initializing camera %s", monitor.id) diff --git a/homeassistant/components/zoneminder/manifest.json b/homeassistant/components/zoneminder/manifest.json index 309ce43101c..f441a800555 100644 --- a/homeassistant/components/zoneminder/manifest.json +++ b/homeassistant/components/zoneminder/manifest.json @@ -5,5 +5,5 @@ "documentation": "https://www.home-assistant.io/integrations/zoneminder", "iot_class": "local_polling", "loggers": ["zoneminder"], - "requirements": ["zm-py==0.5.3"] + "requirements": ["zm-py==0.5.4"] } diff --git a/homeassistant/components/zoneminder/sensor.py b/homeassistant/components/zoneminder/sensor.py index c995e84343b..47863b5a5df 100644 --- a/homeassistant/components/zoneminder/sensor.py +++ b/homeassistant/components/zoneminder/sensor.py @@ -14,6 +14,7 @@ from homeassistant.components.sensor import ( ) from homeassistant.const import CONF_MONITORED_CONDITIONS from homeassistant.core import HomeAssistant +from homeassistant.exceptions import PlatformNotReady import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType @@ -77,7 +78,9 @@ def setup_platform( zm_client: ZoneMinder for zm_client in hass.data[ZONEMINDER_DOMAIN].values(): if not (monitors := zm_client.get_monitors()): - _LOGGER.warning("Could not fetch any monitors from ZoneMinder") + raise PlatformNotReady( + "Sensor could not fetch any monitors from ZoneMinder" + ) for monitor in monitors: sensors.append(ZMSensorMonitors(monitor)) diff --git a/homeassistant/components/zoneminder/switch.py b/homeassistant/components/zoneminder/switch.py index 985866272a6..b722ef53a77 100644 --- a/homeassistant/components/zoneminder/switch.py +++ b/homeassistant/components/zoneminder/switch.py @@ -11,6 +11,7 @@ from zoneminder.zm import ZoneMinder from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchEntity from homeassistant.const import CONF_COMMAND_OFF, CONF_COMMAND_ON from homeassistant.core import HomeAssistant +from homeassistant.exceptions import PlatformNotReady import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType @@ -42,8 +43,9 @@ def setup_platform( zm_client: ZoneMinder for zm_client in hass.data[ZONEMINDER_DOMAIN].values(): if not (monitors := zm_client.get_monitors()): - _LOGGER.warning("Could not fetch monitors from ZoneMinder") - return + raise PlatformNotReady( + "Switch could not fetch any monitors from ZoneMinder" + ) for monitor in monitors: switches.append(ZMSwitchMonitors(monitor, on_state, off_state)) diff --git a/requirements_all.txt b/requirements_all.txt index 29700711dbe..dc2e1dd898e 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -2902,7 +2902,7 @@ zigpy-znp==0.12.1 zigpy==0.60.4 # homeassistant.components.zoneminder -zm-py==0.5.3 +zm-py==0.5.4 # homeassistant.components.zwave_js zwave-js-server-python==0.55.3