From 92195ff77dafdd4e73dffcf3a5e700e2f83feaf3 Mon Sep 17 00:00:00 2001 From: Andre Lengwenus Date: Fri, 20 Dec 2024 17:10:37 +0100 Subject: [PATCH] Bump pypck to 0.8.1 (#133646) Co-authored-by: Robert Resch --- homeassistant/components/lcn/__init__.py | 39 ++++++++++++--------- homeassistant/components/lcn/config_flow.py | 5 ++- homeassistant/components/lcn/manifest.json | 2 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- tests/components/lcn/test_config_flow.py | 13 +++++-- tests/components/lcn/test_init.py | 31 +++++++++++----- 7 files changed, 63 insertions(+), 31 deletions(-) diff --git a/homeassistant/components/lcn/__init__.py b/homeassistant/components/lcn/__init__.py index 6dc6fb1ecc4..a10d08ad073 100644 --- a/homeassistant/components/lcn/__init__.py +++ b/homeassistant/components/lcn/__init__.py @@ -6,7 +6,14 @@ from functools import partial import logging import pypck -from pypck.connection import PchkConnectionManager +from pypck.connection import ( + PchkAuthenticationError, + PchkConnectionFailedError, + PchkConnectionManager, + PchkConnectionRefusedError, + PchkLcnNotConnectedError, + PchkLicenseError, +) from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( @@ -20,6 +27,7 @@ from homeassistant.const import ( Platform, ) from homeassistant.core import HomeAssistant +from homeassistant.exceptions import ConfigEntryNotReady from homeassistant.helpers import config_validation as cv, device_registry as dr from homeassistant.helpers.typing import ConfigType @@ -81,24 +89,21 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b settings=settings, connection_id=config_entry.entry_id, ) + try: # establish connection to PCHK server await lcn_connection.async_connect(timeout=15) - except pypck.connection.PchkAuthenticationError: - _LOGGER.warning('Authentication on PCHK "%s" failed', config_entry.title) - return False - except pypck.connection.PchkLicenseError: - _LOGGER.warning( - ( - 'Maximum number of connections on PCHK "%s" was ' - "reached. An additional license key is required" - ), - config_entry.title, - ) - return False - except TimeoutError: - _LOGGER.warning('Connection to PCHK "%s" failed', config_entry.title) - return False + except ( + PchkAuthenticationError, + PchkLicenseError, + PchkConnectionRefusedError, + PchkConnectionFailedError, + PchkLcnNotConnectedError, + ) as ex: + await lcn_connection.async_close() + raise ConfigEntryNotReady( + f"Unable to connect to {config_entry.title}: {ex}" + ) from ex _LOGGER.debug('LCN connected to "%s"', config_entry.title) hass.data[DOMAIN][config_entry.entry_id] = { @@ -106,6 +111,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b DEVICE_CONNECTIONS: {}, ADD_ENTITIES_CALLBACKS: {}, } + # Update config_entry with LCN device serials await async_update_config_entry(hass, config_entry) @@ -121,6 +127,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b input_received = partial( async_host_input_received, hass, config_entry, device_registry ) + lcn_connection.register_for_inputs(input_received) return True diff --git a/homeassistant/components/lcn/config_flow.py b/homeassistant/components/lcn/config_flow.py index 008265e62ae..a1be32704f7 100644 --- a/homeassistant/components/lcn/config_flow.py +++ b/homeassistant/components/lcn/config_flow.py @@ -96,7 +96,10 @@ async def validate_connection(data: ConfigType) -> str | None: host_name, ) error = "license_error" - except (TimeoutError, ConnectionRefusedError): + except ( + pypck.connection.PchkConnectionFailedError, + pypck.connection.PchkConnectionRefusedError, + ): _LOGGER.warning('Connection to PCHK "%s" failed', host_name) error = "connection_refused" diff --git a/homeassistant/components/lcn/manifest.json b/homeassistant/components/lcn/manifest.json index 695a35df871..f5eb1654588 100644 --- a/homeassistant/components/lcn/manifest.json +++ b/homeassistant/components/lcn/manifest.json @@ -8,5 +8,5 @@ "documentation": "https://www.home-assistant.io/integrations/lcn", "iot_class": "local_push", "loggers": ["pypck"], - "requirements": ["pypck==0.7.24", "lcn-frontend==0.2.2"] + "requirements": ["pypck==0.8.1", "lcn-frontend==0.2.2"] } diff --git a/requirements_all.txt b/requirements_all.txt index 06cf4145b48..62bc0528605 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -2174,7 +2174,7 @@ pypalazzetti==0.1.15 pypca==0.0.7 # homeassistant.components.lcn -pypck==0.7.24 +pypck==0.8.1 # homeassistant.components.pjlink pypjlink2==1.2.1 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 3b9edd9e7c1..d4917df83b7 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -1767,7 +1767,7 @@ pyownet==0.10.0.post1 pypalazzetti==0.1.15 # homeassistant.components.lcn -pypck==0.7.24 +pypck==0.8.1 # homeassistant.components.pjlink pypjlink2==1.2.1 diff --git a/tests/components/lcn/test_config_flow.py b/tests/components/lcn/test_config_flow.py index b7967c247ec..478f2c0949e 100644 --- a/tests/components/lcn/test_config_flow.py +++ b/tests/components/lcn/test_config_flow.py @@ -2,7 +2,12 @@ from unittest.mock import patch -from pypck.connection import PchkAuthenticationError, PchkLicenseError +from pypck.connection import ( + PchkAuthenticationError, + PchkConnectionFailedError, + PchkConnectionRefusedError, + PchkLicenseError, +) import pytest from homeassistant import config_entries, data_entry_flow @@ -98,7 +103,8 @@ async def test_step_user_existing_host( [ (PchkAuthenticationError, {CONF_BASE: "authentication_error"}), (PchkLicenseError, {CONF_BASE: "license_error"}), - (TimeoutError, {CONF_BASE: "connection_refused"}), + (PchkConnectionFailedError, {CONF_BASE: "connection_refused"}), + (PchkConnectionRefusedError, {CONF_BASE: "connection_refused"}), ], ) async def test_step_user_error( @@ -149,7 +155,8 @@ async def test_step_reconfigure(hass: HomeAssistant, entry: MockConfigEntry) -> [ (PchkAuthenticationError, {CONF_BASE: "authentication_error"}), (PchkLicenseError, {CONF_BASE: "license_error"}), - (TimeoutError, {CONF_BASE: "connection_refused"}), + (PchkConnectionFailedError, {CONF_BASE: "connection_refused"}), + (PchkConnectionRefusedError, {CONF_BASE: "connection_refused"}), ], ) async def test_step_reconfigure_error( diff --git a/tests/components/lcn/test_init.py b/tests/components/lcn/test_init.py index 2327635e356..bffa91d14ef 100644 --- a/tests/components/lcn/test_init.py +++ b/tests/components/lcn/test_init.py @@ -2,7 +2,13 @@ from unittest.mock import Mock, patch -from pypck.connection import PchkAuthenticationError, PchkLicenseError +from pypck.connection import ( + PchkAuthenticationError, + PchkConnectionFailedError, + PchkConnectionRefusedError, + PchkLcnNotConnectedError, + PchkLicenseError, +) import pytest from homeassistant import config_entries @@ -84,21 +90,30 @@ async def test_async_setup_entry_update( @pytest.mark.parametrize( - "exception", [PchkAuthenticationError, PchkLicenseError, TimeoutError] + "exception", + [ + PchkAuthenticationError, + PchkLicenseError, + PchkConnectionRefusedError, + PchkConnectionFailedError, + PchkLcnNotConnectedError, + ], ) -async def test_async_setup_entry_raises_authentication_error( +async def test_async_setup_entry_fails( hass: HomeAssistant, entry: MockConfigEntry, exception: Exception ) -> None: - """Test that an authentication error is handled properly.""" - with patch( - "homeassistant.components.lcn.PchkConnectionManager.async_connect", - side_effect=exception, + """Test that an error is handled properly.""" + with ( + patch( + "homeassistant.components.lcn.PchkConnectionManager.async_connect", + side_effect=exception, + ), ): entry.add_to_hass(hass) await hass.config_entries.async_setup(entry.entry_id) await hass.async_block_till_done() - assert entry.state is ConfigEntryState.SETUP_ERROR + assert entry.state is ConfigEntryState.SETUP_RETRY @patch("homeassistant.components.lcn.PchkConnectionManager", MockPchkConnectionManager)