From ee32e0eb3f8017bc1fc5578d73ee753e059ed686 Mon Sep 17 00:00:00 2001 From: Dennis Schroer Date: Thu, 29 Sep 2022 19:25:23 +0200 Subject: [PATCH] Update huisbaasje-client 0.1.0 to energyflip-client 0.2.0 (#79233) --- .../components/huisbaasje/__init__.py | 20 ++-- .../components/huisbaasje/config_flow.py | 17 +-- homeassistant/components/huisbaasje/const.py | 2 +- .../components/huisbaasje/manifest.json | 2 +- requirements_all.txt | 6 +- requirements_test_all.txt | 6 +- .../components/huisbaasje/test_config_flow.py | 108 +++++++++++++++--- tests/components/huisbaasje/test_init.py | 16 +-- tests/components/huisbaasje/test_sensor.py | 12 +- 9 files changed, 131 insertions(+), 58 deletions(-) diff --git a/homeassistant/components/huisbaasje/__init__.py b/homeassistant/components/huisbaasje/__init__.py index fa810d823ca..a3d8863f566 100644 --- a/homeassistant/components/huisbaasje/__init__.py +++ b/homeassistant/components/huisbaasje/__init__.py @@ -3,7 +3,7 @@ from datetime import timedelta import logging import async_timeout -from huisbaasje import Huisbaasje, HuisbaasjeException +from energyflip import EnergyFlip, EnergyFlipException from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform @@ -31,7 +31,7 @@ _LOGGER = logging.getLogger(__name__) async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up Huisbaasje from a config entry.""" # Create the Huisbaasje client - huisbaasje = Huisbaasje( + energyflip = EnergyFlip( username=entry.data[CONF_USERNAME], password=entry.data[CONF_PASSWORD], source_types=SOURCE_TYPES, @@ -40,13 +40,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: # Attempt authentication. If this fails, an exception is thrown try: - await huisbaasje.authenticate() - except HuisbaasjeException as exception: + await energyflip.authenticate() + except EnergyFlipException as exception: _LOGGER.error("Authentication failed: %s", str(exception)) return False async def async_update_data(): - return await async_update_huisbaasje(huisbaasje) + return await async_update_huisbaasje(energyflip) # Create a coordinator for polling updates coordinator = DataUpdateCoordinator( @@ -80,17 +80,17 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: return unload_ok -async def async_update_huisbaasje(huisbaasje): +async def async_update_huisbaasje(energyflip): """Update the data by performing a request to Huisbaasje.""" try: # Note: asyncio.TimeoutError and aiohttp.ClientError are already # handled by the data update coordinator. async with async_timeout.timeout(FETCH_TIMEOUT): - if not huisbaasje.is_authenticated(): + if not energyflip.is_authenticated(): _LOGGER.warning("Huisbaasje is unauthenticated. Reauthenticating") - await huisbaasje.authenticate() + await energyflip.authenticate() - current_measurements = await huisbaasje.current_measurements() + current_measurements = await energyflip.current_measurements() return { source_type: { @@ -112,7 +112,7 @@ async def async_update_huisbaasje(huisbaasje): } for source_type in SOURCE_TYPES } - except HuisbaasjeException as exception: + except EnergyFlipException as exception: raise UpdateFailed(f"Error communicating with API: {exception}") from exception diff --git a/homeassistant/components/huisbaasje/config_flow.py b/homeassistant/components/huisbaasje/config_flow.py index 4139b0d75c5..fc3a1c06a15 100644 --- a/homeassistant/components/huisbaasje/config_flow.py +++ b/homeassistant/components/huisbaasje/config_flow.py @@ -1,7 +1,7 @@ """Config flow for Huisbaasje integration.""" import logging -from huisbaasje import Huisbaasje, HuisbaasjeConnectionException, HuisbaasjeException +from energyflip import EnergyFlip, EnergyFlipConnectionException, EnergyFlipException import voluptuous as vol from homeassistant import config_entries @@ -31,10 +31,10 @@ class HuisbaasjeConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): try: user_id = await self._validate_input(user_input) - except HuisbaasjeConnectionException as exception: + except EnergyFlipConnectionException as exception: _LOGGER.warning(exception) errors["base"] = "cannot_connect" - except HuisbaasjeException as exception: + except EnergyFlipException as exception: _LOGGER.warning(exception) errors["base"] = "invalid_auth" except AbortFlow: @@ -72,9 +72,12 @@ class HuisbaasjeConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): username = user_input[CONF_USERNAME] password = user_input[CONF_PASSWORD] - huisbaasje = Huisbaasje(username, password) + energyflip = EnergyFlip(username, password) - # Attempt authentication. If this fails, an HuisbaasjeException will be thrown - await huisbaasje.authenticate() + # Attempt authentication. If this fails, an EnergyFlipException will be thrown + await energyflip.authenticate() - return huisbaasje.get_user_id() + # Request customer overview. This also sets the user id on the client + await energyflip.customer_overview() + + return energyflip.get_user_id() diff --git a/homeassistant/components/huisbaasje/const.py b/homeassistant/components/huisbaasje/const.py index 637ebd03a17..481f11b2a36 100644 --- a/homeassistant/components/huisbaasje/const.py +++ b/homeassistant/components/huisbaasje/const.py @@ -1,5 +1,5 @@ """Constants for the Huisbaasje integration.""" -from huisbaasje.const import ( +from energyflip.const import ( SOURCE_TYPE_ELECTRICITY, SOURCE_TYPE_ELECTRICITY_IN, SOURCE_TYPE_ELECTRICITY_IN_LOW, diff --git a/homeassistant/components/huisbaasje/manifest.json b/homeassistant/components/huisbaasje/manifest.json index bf3155ed9b8..2963a82512b 100644 --- a/homeassistant/components/huisbaasje/manifest.json +++ b/homeassistant/components/huisbaasje/manifest.json @@ -3,7 +3,7 @@ "name": "Huisbaasje", "config_flow": true, "documentation": "https://www.home-assistant.io/integrations/huisbaasje", - "requirements": ["huisbaasje-client==0.1.0"], + "requirements": ["energyflip-client==0.2.1"], "codeowners": ["@dennisschroer"], "iot_class": "cloud_polling", "loggers": ["huisbaasje"] diff --git a/requirements_all.txt b/requirements_all.txt index 93daecfa29c..e0e7f4261ea 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -622,6 +622,9 @@ elmax_api==0.0.2 # homeassistant.components.emulated_roku emulated_roku==0.2.1 +# homeassistant.components.huisbaasje +energyflip-client==0.2.1 + # homeassistant.components.enocean enocean==0.50 @@ -882,9 +885,6 @@ httplib2==0.20.4 # homeassistant.components.huawei_lte huawei-lte-api==1.6.1 -# homeassistant.components.huisbaasje -huisbaasje-client==0.1.0 - # homeassistant.components.hydrawise hydrawiser==0.2 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index ffa67eb0f02..cf7b95efd29 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -475,6 +475,9 @@ elmax_api==0.0.2 # homeassistant.components.emulated_roku emulated_roku==0.2.1 +# homeassistant.components.huisbaasje +energyflip-client==0.2.1 + # homeassistant.components.enocean enocean==0.50 @@ -659,9 +662,6 @@ httplib2==0.20.4 # homeassistant.components.huawei_lte huawei-lte-api==1.6.1 -# homeassistant.components.huisbaasje -huisbaasje-client==0.1.0 - # homeassistant.components.hyperion hyperion-py==0.7.5 diff --git a/tests/components/huisbaasje/test_config_flow.py b/tests/components/huisbaasje/test_config_flow.py index 8aac11baf6d..e270079de8c 100644 --- a/tests/components/huisbaasje/test_config_flow.py +++ b/tests/components/huisbaasje/test_config_flow.py @@ -1,11 +1,13 @@ """Test the Huisbaasje config flow.""" from unittest.mock import patch -from homeassistant import config_entries, data_entry_flow -from homeassistant.components.huisbaasje.config_flow import ( - HuisbaasjeConnectionException, - HuisbaasjeException, +from energyflip import ( + EnergyFlipConnectionException, + EnergyFlipException, + EnergyFlipUnauthenticatedException, ) + +from homeassistant import config_entries, data_entry_flow from homeassistant.components.huisbaasje.const import DOMAIN from tests.common import MockConfigEntry @@ -21,9 +23,11 @@ async def test_form(hass): assert result["errors"] == {} with patch( - "huisbaasje.Huisbaasje.authenticate", return_value=None + "energyflip.EnergyFlip.authenticate", return_value=None ) as mock_authenticate, patch( - "huisbaasje.Huisbaasje.get_user_id", + "energyflip.EnergyFlip.customer_overview", return_value=None + ) as mock_customer_overview, patch( + "energyflip.EnergyFlip.get_user_id", return_value="test-id", ) as mock_get_user_id, patch( "homeassistant.components.huisbaasje.async_setup_entry", @@ -46,6 +50,7 @@ async def test_form(hass): "password": "test-password", } assert len(mock_authenticate.mock_calls) == 1 + assert len(mock_customer_overview.mock_calls) == 1 assert len(mock_get_user_id.mock_calls) == 1 assert len(mock_setup_entry.mock_calls) == 1 @@ -57,8 +62,8 @@ async def test_form_invalid_auth(hass): ) with patch( - "huisbaasje.Huisbaasje.authenticate", - side_effect=HuisbaasjeException, + "energyflip.EnergyFlip.authenticate", + side_effect=EnergyFlipException, ): form_result = await hass.config_entries.flow.async_configure( result["flow_id"], @@ -72,15 +77,15 @@ async def test_form_invalid_auth(hass): assert form_result["errors"] == {"base": "invalid_auth"} -async def test_form_cannot_connect(hass): - """Test we handle cannot connect error.""" +async def test_form_authenticate_cannot_connect(hass): + """Test we handle cannot connect error in authenticate.""" result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER} ) with patch( - "huisbaasje.Huisbaasje.authenticate", - side_effect=HuisbaasjeConnectionException, + "energyflip.EnergyFlip.authenticate", + side_effect=EnergyFlipConnectionException, ): form_result = await hass.config_entries.flow.async_configure( result["flow_id"], @@ -94,14 +99,80 @@ async def test_form_cannot_connect(hass): assert form_result["errors"] == {"base": "cannot_connect"} -async def test_form_unknown_error(hass): - """Test we handle an unknown error.""" +async def test_form_authenticate_unknown_error(hass): + """Test we handle an unknown error in authenticate.""" result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER} ) with patch( - "huisbaasje.Huisbaasje.authenticate", + "energyflip.EnergyFlip.authenticate", + side_effect=Exception, + ): + form_result = await hass.config_entries.flow.async_configure( + result["flow_id"], + { + "username": "test-username", + "password": "test-password", + }, + ) + + assert form_result["type"] == data_entry_flow.FlowResultType.FORM + assert form_result["errors"] == {"base": "unknown"} + + +async def test_form_customer_overview_cannot_connect(hass): + """Test we handle cannot connect error in customer_overview.""" + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": config_entries.SOURCE_USER} + ) + + with patch("energyflip.EnergyFlip.authenticate", return_value=None), patch( + "energyflip.EnergyFlip.customer_overview", + side_effect=EnergyFlipConnectionException, + ): + form_result = await hass.config_entries.flow.async_configure( + result["flow_id"], + { + "username": "test-username", + "password": "test-password", + }, + ) + + assert form_result["type"] == data_entry_flow.FlowResultType.FORM + assert form_result["errors"] == {"base": "cannot_connect"} + + +async def test_form_customer_overview_authentication_error(hass): + """Test we handle an unknown error in customer_overview.""" + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": config_entries.SOURCE_USER} + ) + + with patch("energyflip.EnergyFlip.authenticate", return_value=None), patch( + "energyflip.EnergyFlip.customer_overview", + side_effect=EnergyFlipUnauthenticatedException, + ): + form_result = await hass.config_entries.flow.async_configure( + result["flow_id"], + { + "username": "test-username", + "password": "test-password", + }, + ) + + assert form_result["type"] == data_entry_flow.FlowResultType.FORM + assert form_result["errors"] == {"base": "invalid_auth"} + + +async def test_form_customer_overview_unknown_error(hass): + """Test we handle an unknown error in customer_overview.""" + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": config_entries.SOURCE_USER} + ) + + with patch("energyflip.EnergyFlip.authenticate", return_value=None), patch( + "energyflip.EnergyFlip.customer_overview", side_effect=Exception, ): form_result = await hass.config_entries.flow.async_configure( @@ -133,10 +204,9 @@ async def test_form_entry_exists(hass): DOMAIN, context={"source": config_entries.SOURCE_USER} ) - with patch("huisbaasje.Huisbaasje.authenticate", return_value=None), patch( - "huisbaasje.Huisbaasje.get_user_id", - return_value="test-id", - ), patch( + with patch("energyflip.EnergyFlip.authenticate", return_value=None), patch( + "energyflip.EnergyFlip.customer_overview", return_value=None + ), patch("energyflip.EnergyFlip.get_user_id", return_value="test-id",), patch( "homeassistant.components.huisbaasje.async_setup_entry", return_value=True, ): diff --git a/tests/components/huisbaasje/test_init.py b/tests/components/huisbaasje/test_init.py index 859cfc4df83..30de00fd64f 100644 --- a/tests/components/huisbaasje/test_init.py +++ b/tests/components/huisbaasje/test_init.py @@ -1,7 +1,7 @@ """Test cases for the initialisation of the Huisbaasje integration.""" from unittest.mock import patch -from huisbaasje import HuisbaasjeException +from energyflip import EnergyFlipException from homeassistant.components import huisbaasje from homeassistant.config_entries import ConfigEntryState @@ -24,11 +24,11 @@ async def test_setup(hass: HomeAssistant): async def test_setup_entry(hass: HomeAssistant): """Test for successfully setting a config entry.""" with patch( - "huisbaasje.Huisbaasje.authenticate", return_value=None + "energyflip.EnergyFlip.authenticate", return_value=None ) as mock_authenticate, patch( - "huisbaasje.Huisbaasje.is_authenticated", return_value=True + "energyflip.EnergyFlip.is_authenticated", return_value=True ) as mock_is_authenticated, patch( - "huisbaasje.Huisbaasje.current_measurements", + "energyflip.EnergyFlip.current_measurements", return_value=MOCK_CURRENT_MEASUREMENTS, ) as mock_current_measurements: hass.config.components.add(huisbaasje.DOMAIN) @@ -68,7 +68,7 @@ async def test_setup_entry(hass: HomeAssistant): async def test_setup_entry_error(hass: HomeAssistant): """Test for successfully setting a config entry.""" with patch( - "huisbaasje.Huisbaasje.authenticate", side_effect=HuisbaasjeException + "energyflip.EnergyFlip.authenticate", side_effect=EnergyFlipException ) as mock_authenticate: hass.config.components.add(huisbaasje.DOMAIN) config_entry = MockConfigEntry( @@ -103,11 +103,11 @@ async def test_setup_entry_error(hass: HomeAssistant): async def test_unload_entry(hass: HomeAssistant): """Test for successfully unloading the config entry.""" with patch( - "huisbaasje.Huisbaasje.authenticate", return_value=None + "energyflip.EnergyFlip.authenticate", return_value=None ) as mock_authenticate, patch( - "huisbaasje.Huisbaasje.is_authenticated", return_value=True + "energyflip.EnergyFlip.is_authenticated", return_value=True ) as mock_is_authenticated, patch( - "huisbaasje.Huisbaasje.current_measurements", + "energyflip.EnergyFlip.current_measurements", return_value=MOCK_CURRENT_MEASUREMENTS, ) as mock_current_measurements: hass.config.components.add(huisbaasje.DOMAIN) diff --git a/tests/components/huisbaasje/test_sensor.py b/tests/components/huisbaasje/test_sensor.py index 84e1f71071c..43789988003 100644 --- a/tests/components/huisbaasje/test_sensor.py +++ b/tests/components/huisbaasje/test_sensor.py @@ -29,11 +29,11 @@ from tests.common import MockConfigEntry async def test_setup_entry(hass: HomeAssistant): """Test for successfully loading sensor states.""" with patch( - "huisbaasje.Huisbaasje.authenticate", return_value=None + "energyflip.EnergyFlip.authenticate", return_value=None ) as mock_authenticate, patch( - "huisbaasje.Huisbaasje.is_authenticated", return_value=True + "energyflip.EnergyFlip.is_authenticated", return_value=True ) as mock_is_authenticated, patch( - "huisbaasje.Huisbaasje.current_measurements", + "energyflip.EnergyFlip.current_measurements", return_value=MOCK_CURRENT_MEASUREMENTS, ) as mock_current_measurements: @@ -344,11 +344,11 @@ async def test_setup_entry(hass: HomeAssistant): async def test_setup_entry_absent_measurement(hass: HomeAssistant): """Test for successfully loading sensor states when response does not contain all measurements.""" with patch( - "huisbaasje.Huisbaasje.authenticate", return_value=None + "energyflip.EnergyFlip.authenticate", return_value=None ) as mock_authenticate, patch( - "huisbaasje.Huisbaasje.is_authenticated", return_value=True + "energyflip.EnergyFlip.is_authenticated", return_value=True ) as mock_is_authenticated, patch( - "huisbaasje.Huisbaasje.current_measurements", + "energyflip.EnergyFlip.current_measurements", return_value=MOCK_LIMITED_CURRENT_MEASUREMENTS, ) as mock_current_measurements: