From 54d570a9cf1657f9e3af450398180b1d3ed0d9d6 Mon Sep 17 00:00:00 2001 From: Jussi Rosenberg Date: Mon, 16 Jan 2023 13:54:29 +0200 Subject: [PATCH] Bump pykoplenti to 1.0.0 (#83251) * Update Kostal integration to use maintained lib * Update Kostal integration to use pykoplenti * Update kostal_plenticore tests for new lib * Fix tests config_flow & diagnostics after changes --- .../components/kostal_plenticore/__init__.py | 4 ++-- .../kostal_plenticore/config_flow.py | 6 +++--- .../components/kostal_plenticore/helper.py | 18 ++++++----------- .../kostal_plenticore/manifest.json | 2 +- .../components/kostal_plenticore/number.py | 2 +- requirements_all.txt | 6 +++--- requirements_test_all.txt | 6 +++--- .../components/kostal_plenticore/conftest.py | 2 +- .../kostal_plenticore/test_config_flow.py | 20 +++++++++---------- .../kostal_plenticore/test_diagnostics.py | 4 ++-- .../kostal_plenticore/test_number.py | 20 +++++++++---------- .../kostal_plenticore/test_select.py | 2 +- 12 files changed, 43 insertions(+), 49 deletions(-) diff --git a/homeassistant/components/kostal_plenticore/__init__.py b/homeassistant/components/kostal_plenticore/__init__.py index 24e8ab9f0d3..b7e4c86f772 100644 --- a/homeassistant/components/kostal_plenticore/__init__.py +++ b/homeassistant/components/kostal_plenticore/__init__.py @@ -1,7 +1,7 @@ """The Kostal Plenticore Solar Inverter integration.""" import logging -from kostal.plenticore import PlenticoreApiException +from pykoplenti import ApiException from homeassistant.config_entries import ConfigEntry from homeassistant.const import Platform @@ -39,7 +39,7 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: plenticore = hass.data[DOMAIN].pop(entry.entry_id) try: await plenticore.async_unload() - except PlenticoreApiException as err: + except ApiException as err: _LOGGER.error("Error logging out from inverter: %s", err) return unload_ok diff --git a/homeassistant/components/kostal_plenticore/config_flow.py b/homeassistant/components/kostal_plenticore/config_flow.py index 359efef651a..cbbaeefd85d 100644 --- a/homeassistant/components/kostal_plenticore/config_flow.py +++ b/homeassistant/components/kostal_plenticore/config_flow.py @@ -3,7 +3,7 @@ import asyncio import logging from aiohttp.client_exceptions import ClientError -from kostal.plenticore import PlenticoreApiClient, PlenticoreAuthenticationException +from pykoplenti import ApiClient, AuthenticationException import voluptuous as vol from homeassistant import config_entries @@ -30,7 +30,7 @@ async def test_connection(hass: HomeAssistant, data) -> str: """ session = async_get_clientsession(hass) - async with PlenticoreApiClient(session, data["host"]) as client: + async with ApiClient(session, data["host"]) as client: await client.login(data["password"]) values = await client.get_setting_values("scb:network", "Hostname") @@ -52,7 +52,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): try: hostname = await test_connection(self.hass, user_input) - except PlenticoreAuthenticationException as ex: + except AuthenticationException as ex: errors[CONF_PASSWORD] = "invalid_auth" _LOGGER.error("Error response: %s", ex) except (ClientError, asyncio.TimeoutError): diff --git a/homeassistant/components/kostal_plenticore/helper.py b/homeassistant/components/kostal_plenticore/helper.py index 52ff3a7480b..51544e49409 100644 --- a/homeassistant/components/kostal_plenticore/helper.py +++ b/homeassistant/components/kostal_plenticore/helper.py @@ -9,11 +9,7 @@ import logging from typing import Any, TypeVar, cast from aiohttp.client_exceptions import ClientError -from kostal.plenticore import ( - PlenticoreApiClient, - PlenticoreApiException, - PlenticoreAuthenticationException, -) +from pykoplenti import ApiClient, ApiException, AuthenticationException from homeassistant.const import CONF_HOST, CONF_PASSWORD, EVENT_HOMEASSISTANT_STOP from homeassistant.core import HomeAssistant @@ -48,18 +44,16 @@ class Plenticore: return self.config_entry.data[CONF_HOST] @property - def client(self) -> PlenticoreApiClient: + def client(self) -> ApiClient: """Return the Plenticore API client.""" return self._client async def async_setup(self) -> bool: """Set up Plenticore API client.""" - self._client = PlenticoreApiClient( - async_get_clientsession(self.hass), host=self.host - ) + self._client = ApiClient(async_get_clientsession(self.hass), host=self.host) try: await self._client.login(self.config_entry.data[CONF_PASSWORD]) - except PlenticoreAuthenticationException as err: + except AuthenticationException as err: _LOGGER.error( "Authentication exception connecting to %s: %s", self.host, err ) @@ -135,7 +129,7 @@ class DataUpdateCoordinatorMixin: try: return await client.get_setting_values(module_id, data_id) - except PlenticoreApiException: + except ApiException: return None async def async_write_data(self, module_id: str, value: dict[str, str]) -> bool: @@ -149,7 +143,7 @@ class DataUpdateCoordinatorMixin: try: await client.set_setting_values(module_id, value) - except PlenticoreApiException: + except ApiException: return False else: return True diff --git a/homeassistant/components/kostal_plenticore/manifest.json b/homeassistant/components/kostal_plenticore/manifest.json index 71f71cae993..edbee8f6fbe 100644 --- a/homeassistant/components/kostal_plenticore/manifest.json +++ b/homeassistant/components/kostal_plenticore/manifest.json @@ -3,7 +3,7 @@ "name": "Kostal Plenticore Solar Inverter", "config_flow": true, "documentation": "https://www.home-assistant.io/integrations/kostal_plenticore", - "requirements": ["kostal_plenticore==0.2.0"], + "requirements": ["pykoplenti==1.0.0"], "codeowners": ["@stegm"], "iot_class": "local_polling", "loggers": ["kostal"] diff --git a/homeassistant/components/kostal_plenticore/number.py b/homeassistant/components/kostal_plenticore/number.py index 2b0726e6255..3acb80030cf 100644 --- a/homeassistant/components/kostal_plenticore/number.py +++ b/homeassistant/components/kostal_plenticore/number.py @@ -5,7 +5,7 @@ from dataclasses import dataclass from datetime import timedelta import logging -from kostal.plenticore import SettingsData +from pykoplenti import SettingsData from homeassistant.components.number import ( NumberDeviceClass, diff --git a/requirements_all.txt b/requirements_all.txt index 9a71bfe20f9..868e6303a51 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1017,9 +1017,6 @@ kiwiki-client==0.1.1 # homeassistant.components.konnected konnected==1.2.0 -# homeassistant.components.kostal_plenticore -kostal_plenticore==0.2.0 - # homeassistant.components.kraken krakenex==2.1.0 @@ -1713,6 +1710,9 @@ pykmtronic==0.3.0 # homeassistant.components.kodi pykodi==0.2.7 +# homeassistant.components.kostal_plenticore +pykoplenti==1.0.0 + # homeassistant.components.kraken pykrakenapi==0.1.8 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index f1a7befbce8..881130217fd 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -764,9 +764,6 @@ kegtron-ble==0.4.0 # homeassistant.components.konnected konnected==1.2.0 -# homeassistant.components.kostal_plenticore -kostal_plenticore==0.2.0 - # homeassistant.components.kraken krakenex==2.1.0 @@ -1229,6 +1226,9 @@ pykmtronic==0.3.0 # homeassistant.components.kodi pykodi==0.2.7 +# homeassistant.components.kostal_plenticore +pykoplenti==1.0.0 + # homeassistant.components.kraken pykrakenapi==0.1.8 diff --git a/tests/components/kostal_plenticore/conftest.py b/tests/components/kostal_plenticore/conftest.py index 4e789a34198..f0e7752d7c0 100644 --- a/tests/components/kostal_plenticore/conftest.py +++ b/tests/components/kostal_plenticore/conftest.py @@ -4,7 +4,7 @@ from __future__ import annotations from collections.abc import Generator from unittest.mock import AsyncMock, MagicMock, patch -from kostal.plenticore import MeData, VersionData +from pykoplenti import MeData, VersionData import pytest from homeassistant.components.kostal_plenticore.helper import Plenticore diff --git a/tests/components/kostal_plenticore/test_config_flow.py b/tests/components/kostal_plenticore/test_config_flow.py index 17ff8ef3e03..dc7f9014b06 100644 --- a/tests/components/kostal_plenticore/test_config_flow.py +++ b/tests/components/kostal_plenticore/test_config_flow.py @@ -2,7 +2,7 @@ import asyncio from unittest.mock import ANY, AsyncMock, MagicMock, patch -from kostal.plenticore import PlenticoreAuthenticationException +from pykoplenti import AuthenticationException from homeassistant import config_entries from homeassistant.components.kostal_plenticore.const import DOMAIN @@ -20,7 +20,7 @@ async def test_formx(hass): assert result["errors"] == {} with patch( - "homeassistant.components.kostal_plenticore.config_flow.PlenticoreApiClient" + "homeassistant.components.kostal_plenticore.config_flow.ApiClient" ) as mock_api_class, patch( "homeassistant.components.kostal_plenticore.async_setup_entry", return_value=True, @@ -32,7 +32,7 @@ async def test_formx(hass): return_value={"scb:network": {"Hostname": "scb"}} ) - # mock of the return instance of PlenticoreApiClient + # mock of the return instance of ApiClient mock_api = MagicMock() mock_api.__aenter__.return_value = mock_api_ctx mock_api.__aexit__ = AsyncMock() @@ -70,15 +70,15 @@ async def test_form_invalid_auth(hass): ) with patch( - "homeassistant.components.kostal_plenticore.config_flow.PlenticoreApiClient" + "homeassistant.components.kostal_plenticore.config_flow.ApiClient" ) as mock_api_class: # mock of the context manager instance mock_api_ctx = MagicMock() mock_api_ctx.login = AsyncMock( - side_effect=PlenticoreAuthenticationException(404, "invalid user"), + side_effect=AuthenticationException(404, "invalid user"), ) - # mock of the return instance of PlenticoreApiClient + # mock of the return instance of ApiClient mock_api = MagicMock() mock_api.__aenter__.return_value = mock_api_ctx mock_api.__aexit__.return_value = None @@ -104,7 +104,7 @@ async def test_form_cannot_connect(hass): ) with patch( - "homeassistant.components.kostal_plenticore.config_flow.PlenticoreApiClient" + "homeassistant.components.kostal_plenticore.config_flow.ApiClient" ) as mock_api_class: # mock of the context manager instance mock_api_ctx = MagicMock() @@ -112,7 +112,7 @@ async def test_form_cannot_connect(hass): side_effect=asyncio.TimeoutError(), ) - # mock of the return instance of PlenticoreApiClient + # mock of the return instance of ApiClient mock_api = MagicMock() mock_api.__aenter__.return_value = mock_api_ctx mock_api.__aexit__.return_value = None @@ -138,7 +138,7 @@ async def test_form_unexpected_error(hass): ) with patch( - "homeassistant.components.kostal_plenticore.config_flow.PlenticoreApiClient" + "homeassistant.components.kostal_plenticore.config_flow.ApiClient" ) as mock_api_class: # mock of the context manager instance mock_api_ctx = MagicMock() @@ -146,7 +146,7 @@ async def test_form_unexpected_error(hass): side_effect=Exception(), ) - # mock of the return instance of PlenticoreApiClient + # mock of the return instance of ApiClient mock_api = MagicMock() mock_api.__aenter__.return_value = mock_api_ctx mock_api.__aexit__.return_value = None diff --git a/tests/components/kostal_plenticore/test_diagnostics.py b/tests/components/kostal_plenticore/test_diagnostics.py index 1f249aa3798..14a25ff6c60 100644 --- a/tests/components/kostal_plenticore/test_diagnostics.py +++ b/tests/components/kostal_plenticore/test_diagnostics.py @@ -1,6 +1,6 @@ """Test Kostal Plenticore diagnostics.""" from aiohttp import ClientSession -from kostal.plenticore import SettingsData +from pykoplenti import SettingsData from homeassistant.components.diagnostics import REDACTED from homeassistant.components.kostal_plenticore.helper import Plenticore @@ -57,7 +57,7 @@ async def test_entry_diagnostics( }, "client": { "version": "Version(api_version=0.2.0, hostname=scb, name=PUCK RESTful API, sw_version=01.16.05025)", - "me": "Me(locked=False, active=True, authenticated=True, permissions=[] anonymous=False role=USER)", + "me": "Me(locked=False, active=True, authenticated=True, permissions=[], anonymous=False, role=USER)", "available_process_data": {"devices:local": ["HomeGrid_P", "HomePv_P"]}, "available_settings_data": { "devices:local": [ diff --git a/tests/components/kostal_plenticore/test_number.py b/tests/components/kostal_plenticore/test_number.py index a27b880b5d7..baa6aa8c34e 100644 --- a/tests/components/kostal_plenticore/test_number.py +++ b/tests/components/kostal_plenticore/test_number.py @@ -4,7 +4,7 @@ from collections.abc import Generator from datetime import timedelta from unittest.mock import patch -from kostal.plenticore import PlenticoreApiClient, SettingsData +from pykoplenti import ApiClient, SettingsData import pytest from homeassistant.components.number import ( @@ -23,17 +23,17 @@ from tests.common import MockConfigEntry, async_fire_time_changed @pytest.fixture -def mock_plenticore_client() -> Generator[PlenticoreApiClient, None, None]: - """Return a patched PlenticoreApiClient.""" +def mock_plenticore_client() -> Generator[ApiClient, None, None]: + """Return a patched ApiClient.""" with patch( - "homeassistant.components.kostal_plenticore.helper.PlenticoreApiClient", + "homeassistant.components.kostal_plenticore.helper.ApiClient", autospec=True, ) as plenticore_client_class: yield plenticore_client_class.return_value @pytest.fixture -def mock_get_setting_values(mock_plenticore_client: PlenticoreApiClient) -> list: +def mock_get_setting_values(mock_plenticore_client: ApiClient) -> list: """Add a setting value to the given Plenticore client. Returns a list with setting values which can be extended by test cases. @@ -88,7 +88,7 @@ def mock_get_setting_values(mock_plenticore_client: PlenticoreApiClient) -> list async def test_setup_all_entries( hass: HomeAssistant, mock_config_entry: MockConfigEntry, - mock_plenticore_client: PlenticoreApiClient, + mock_plenticore_client: ApiClient, mock_get_setting_values: list, entity_registry_enabled_by_default, ): @@ -107,7 +107,7 @@ async def test_setup_all_entries( async def test_setup_no_entries( hass: HomeAssistant, mock_config_entry: MockConfigEntry, - mock_plenticore_client: PlenticoreApiClient, + mock_plenticore_client: ApiClient, mock_get_setting_values: list, entity_registry_enabled_by_default, ): @@ -128,7 +128,7 @@ async def test_setup_no_entries( async def test_number_has_value( hass: HomeAssistant, mock_config_entry: MockConfigEntry, - mock_plenticore_client: PlenticoreApiClient, + mock_plenticore_client: ApiClient, mock_get_setting_values: list, entity_registry_enabled_by_default, ): @@ -153,7 +153,7 @@ async def test_number_has_value( async def test_number_is_unavailable( hass: HomeAssistant, mock_config_entry: MockConfigEntry, - mock_plenticore_client: PlenticoreApiClient, + mock_plenticore_client: ApiClient, mock_get_setting_values: list, entity_registry_enabled_by_default, ): @@ -174,7 +174,7 @@ async def test_number_is_unavailable( async def test_set_value( hass: HomeAssistant, mock_config_entry: MockConfigEntry, - mock_plenticore_client: PlenticoreApiClient, + mock_plenticore_client: ApiClient, mock_get_setting_values: list, entity_registry_enabled_by_default, ): diff --git a/tests/components/kostal_plenticore/test_select.py b/tests/components/kostal_plenticore/test_select.py index 6023b015483..b892c0a457a 100644 --- a/tests/components/kostal_plenticore/test_select.py +++ b/tests/components/kostal_plenticore/test_select.py @@ -1,5 +1,5 @@ """Test the Kostal Plenticore Solar Inverter select platform.""" -from kostal.plenticore import SettingsData +from pykoplenti import SettingsData from homeassistant.components.kostal_plenticore.helper import Plenticore from homeassistant.core import HomeAssistant