From 652207272aff6a75f41562b623b7e8b21b8b2a9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Du=C5=A1ek?= Date: Sat, 12 Oct 2024 18:05:48 +0200 Subject: [PATCH] Fix hassio warnings and false True (positive) for DisableLinkage feature (#394) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix hassio warnings setup without awaiting async_forward_entry_setup, which can cause the setup lock to be released before the setup is done. This will stop working in Home Assistant 2025.1 Detected blocking call to load_default_certs Signed-off-by: Jan Dušek * Changing query for DisableLinkage Unify the way it is done elsewhere to prevent excesive false positive retun of True when DisableLinkage is actually available and returns error from camera. After thsi fix, it is queried twice and then the integration knows it is unavailable. Signed-off-by: Jan Dušek --------- Signed-off-by: Jan Dušek --- custom_components/dahua/__init__.py | 16 ++++++++-------- custom_components/dahua/client.py | 5 +---- custom_components/dahua/config_flow.py | 12 +++++++----- 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/custom_components/dahua/__init__.py b/custom_components/dahua/__init__.py index 56a92c7..513c51f 100755 --- a/custom_components/dahua/__init__.py +++ b/custom_components/dahua/__init__.py @@ -17,6 +17,7 @@ from homeassistant.core import CALLBACK_TYPE, Config, HomeAssistant from homeassistant.exceptions import ConfigEntryNotReady, PlatformNotReady from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed +from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.const import EVENT_HOMEASSISTANT_STOP from custom_components.dahua.thread import DahuaEventThread, DahuaVtoEventThread @@ -41,6 +42,11 @@ SCAN_INTERVAL_SECONDS = timedelta(seconds=30) +SSL_CONTEXT = ssl.create_default_context() +SSL_CONTEXT.set_ciphers("DEFAULT") +SSL_CONTEXT.check_hostname = False +SSL_CONTEXT.verify_mode = ssl.CERT_NONE + _LOGGER: logging.Logger = logging.getLogger(__package__) @@ -82,9 +88,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): for platform in PLATFORMS: if entry.options.get(platform, True): coordinator.platforms.append(platform) - hass.async_create_task( - hass.config_entries.async_forward_entry_setup(entry, platform) - ) + await hass.config_entries.async_forward_entry_setups(entry, [platform]) entry.add_update_listener(async_reload_entry) @@ -102,11 +106,7 @@ def __init__(self, hass: HomeAssistant, events: list, address: str, port: int, r password: str, name: str, channel: int) -> None: """Initialize the coordinator.""" # Self signed certs are used over HTTPS so we'll disable SSL verification - ssl_context = ssl.create_default_context() - ssl_context.set_ciphers("DEFAULT") - ssl_context.check_hostname = False - ssl_context.verify_mode = ssl.CERT_NONE - connector = TCPConnector(enable_cleanup_closed=True, ssl=ssl_context) + connector = TCPConnector(enable_cleanup_closed=True, ssl=SSL_CONTEXT) self._session = ClientSession(connector=connector) # The client used to communicate with Dahua devices diff --git a/custom_components/dahua/client.py b/custom_components/dahua/client.py index bd2eefa..50c27ff 100755 --- a/custom_components/dahua/client.py +++ b/custom_components/dahua/client.py @@ -629,10 +629,7 @@ async def async_get_disarming_linkage(self) -> dict: """ url = "/cgi-bin/configManager.cgi?action=getConfig&name=DisableLinkage" - try: - return await self.get(url) - except aiohttp.ClientResponseError as e: - return {"table.DisableLinkage.Enable": "false"} + return await self.get(url) async def async_access_control_open_door(self, door_id: int = 1) -> dict: """ diff --git a/custom_components/dahua/config_flow.py b/custom_components/dahua/config_flow.py index c2ebd62..1fe1837 100755 --- a/custom_components/dahua/config_flow.py +++ b/custom_components/dahua/config_flow.py @@ -30,6 +30,12 @@ https://developers.home-assistant.io/docs/data_entry_flow_index/ """ +SSL_CONTEXT = ssl.create_default_context() +#SSL_CONTEXT.minimum_version = ssl.TLSVersion.TLSv1_2 +SSL_CONTEXT.set_ciphers("DEFAULT") +SSL_CONTEXT.check_hostname = False +SSL_CONTEXT.verify_mode = ssl.CERT_NONE + _LOGGER: logging.Logger = logging.getLogger(__package__) DEFAULT_EVENTS = ["VideoMotion", "CrossLineDetection", "AlarmLocal", "VideoLoss", "VideoBlind", "AudioMutation", @@ -177,11 +183,7 @@ async def _show_config_form_name(self, user_input): # pylint: disable=unused-ar async def _test_credentials(self, username, password, address, port, rtsp_port, channel): """Return name and serialNumber if credentials is valid.""" # Self signed certs are used over HTTPS so we'll disable SSL verification - ssl_context = ssl.create_default_context() - ssl_context.set_ciphers("DEFAULT") - ssl_context.check_hostname = False - ssl_context.verify_mode = ssl.CERT_NONE - connector = TCPConnector(enable_cleanup_closed=True, ssl=ssl_context) + connector = TCPConnector(enable_cleanup_closed=True, ssl=SSL_CONTEXT) session = ClientSession(connector=connector) try: client = DahuaClient(username, password, address, port, rtsp_port, session)