From b90c620c5e8b3f2868b2065e74fe8819260ef8ea Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Fri, 26 Mar 2021 04:18:46 +0100 Subject: [PATCH] Address huisbaasje review comments (#48313) * Address huisbaasje review comments * Update homeassistant/components/huisbaasje/config_flow.py Co-authored-by: Martin Hjelmare Co-authored-by: Martin Hjelmare --- .../components/huisbaasje/__init__.py | 2 +- .../components/huisbaasje/config_flow.py | 27 +++++++++---------- homeassistant/components/huisbaasje/const.py | 9 ++++--- .../components/huisbaasje/strings.json | 3 +-- .../components/huisbaasje/test_config_flow.py | 2 +- 5 files changed, 20 insertions(+), 23 deletions(-) diff --git a/homeassistant/components/huisbaasje/__init__.py b/homeassistant/components/huisbaasje/__init__.py index 8cd2681c8da..f06ad444cc0 100644 --- a/homeassistant/components/huisbaasje/__init__.py +++ b/homeassistant/components/huisbaasje/__init__.py @@ -141,7 +141,7 @@ def _get_cumulative_value( :param source_type: The source of energy (electricity or gas) :param period_type: The period for which cumulative value should be given. """ - if source_type in current_measurements.keys(): + if source_type in current_measurements: if ( period_type in current_measurements[source_type] and current_measurements[source_type][period_type] is not None diff --git a/homeassistant/components/huisbaasje/config_flow.py b/homeassistant/components/huisbaasje/config_flow.py index 59e4840529d..c9a4750ade5 100644 --- a/homeassistant/components/huisbaasje/config_flow.py +++ b/homeassistant/components/huisbaasje/config_flow.py @@ -32,9 +32,18 @@ class HuisbaasjeConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): try: user_id = await self._validate_input(user_input) - - _LOGGER.info("Input for Huisbaasje is valid!") - + except HuisbaasjeConnectionException as exception: + _LOGGER.warning(exception) + errors["base"] = "cannot_connect" + except HuisbaasjeException as exception: + _LOGGER.warning(exception) + errors["base"] = "invalid_auth" + except AbortFlow: + raise + except Exception: # pylint: disable=broad-except + _LOGGER.exception("Unexpected exception") + errors["base"] = "unknown" + else: # Set user id as unique id await self.async_set_unique_id(user_id) self._abort_if_unique_id_configured() @@ -48,17 +57,6 @@ class HuisbaasjeConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): CONF_PASSWORD: user_input[CONF_PASSWORD], }, ) - except HuisbaasjeConnectionException as exception: - _LOGGER.warning(exception) - errors["base"] = "connection_exception" - except HuisbaasjeException as exception: - _LOGGER.warning(exception) - errors["base"] = "invalid_auth" - except AbortFlow as exception: - raise exception - except Exception: # pylint: disable=broad-except - _LOGGER.exception("Unexpected exception") - errors["base"] = "unknown" return await self._show_setup_form(user_input, errors) @@ -72,7 +70,6 @@ class HuisbaasjeConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): Data has the keys from DATA_SCHEMA with values provided by the user. """ - username = user_input[CONF_USERNAME] password = user_input[CONF_PASSWORD] diff --git a/homeassistant/components/huisbaasje/const.py b/homeassistant/components/huisbaasje/const.py index 07ad84567e5..abac03e6182 100644 --- a/homeassistant/components/huisbaasje/const.py +++ b/homeassistant/components/huisbaasje/const.py @@ -9,6 +9,7 @@ from huisbaasje.const import ( ) from homeassistant.const import ( + DEVICE_CLASS_ENERGY, DEVICE_CLASS_POWER, ENERGY_KILO_WATT_HOUR, TIME_HOURS, @@ -70,34 +71,34 @@ SENSORS_INFO = [ }, { "name": "Huisbaasje Energy Today", + "device_class": DEVICE_CLASS_ENERGY, "unit_of_measurement": ENERGY_KILO_WATT_HOUR, "source_type": SOURCE_TYPE_ELECTRICITY, "sensor_type": SENSOR_TYPE_THIS_DAY, - "icon": "mdi:counter", "precision": 1, }, { "name": "Huisbaasje Energy This Week", + "device_class": DEVICE_CLASS_ENERGY, "unit_of_measurement": ENERGY_KILO_WATT_HOUR, "source_type": SOURCE_TYPE_ELECTRICITY, "sensor_type": SENSOR_TYPE_THIS_WEEK, - "icon": "mdi:counter", "precision": 1, }, { "name": "Huisbaasje Energy This Month", + "device_class": DEVICE_CLASS_ENERGY, "unit_of_measurement": ENERGY_KILO_WATT_HOUR, "source_type": SOURCE_TYPE_ELECTRICITY, "sensor_type": SENSOR_TYPE_THIS_MONTH, - "icon": "mdi:counter", "precision": 1, }, { "name": "Huisbaasje Energy This Year", + "device_class": DEVICE_CLASS_ENERGY, "unit_of_measurement": ENERGY_KILO_WATT_HOUR, "source_type": SOURCE_TYPE_ELECTRICITY, "sensor_type": SENSOR_TYPE_THIS_YEAR, - "icon": "mdi:counter", "precision": 1, }, { diff --git a/homeassistant/components/huisbaasje/strings.json b/homeassistant/components/huisbaasje/strings.json index f126ac0afff..169b9a0e901 100644 --- a/homeassistant/components/huisbaasje/strings.json +++ b/homeassistant/components/huisbaasje/strings.json @@ -10,8 +10,7 @@ }, "error": { "invalid_auth": "[%key:common::config_flow::error::invalid_auth%]", - "unauthenticated_exception": "[%key:common::config_flow::error::invalid_auth%]", - "connection_exception": "[%key:common::config_flow::error::cannot_connect%]", + "cannot_connect": "[%key:common::config_flow::error::cannot_connect%]", "unknown": "[%key:common::config_flow::error::unknown%]" }, "abort": { diff --git a/tests/components/huisbaasje/test_config_flow.py b/tests/components/huisbaasje/test_config_flow.py index 245ac2f8ddb..35e28b645eb 100644 --- a/tests/components/huisbaasje/test_config_flow.py +++ b/tests/components/huisbaasje/test_config_flow.py @@ -94,7 +94,7 @@ async def test_form_cannot_connect(hass): ) assert form_result["type"] == data_entry_flow.RESULT_TYPE_FORM - assert form_result["errors"] == {"base": "connection_exception"} + assert form_result["errors"] == {"base": "cannot_connect"} async def test_form_unknown_error(hass):