mirror of
https://github.com/home-assistant/core.git
synced 2025-07-27 15:17:35 +00:00
Fix code quality issues for HomeWizard (#84134)
* Remove unused constant * Reuse fetch check for retrieving device information * Remove else block * Patch integration setup in test * use isinstance to detect return type, instead of tuple * Raise exception when recoverable error has been triggered to make code cleaner * Use error code to split message and localization * Actually log things
This commit is contained in:
parent
570824100c
commit
d8cbff65f1
@ -7,12 +7,14 @@ from typing import Any, cast
|
|||||||
|
|
||||||
from homewizard_energy import HomeWizardEnergy
|
from homewizard_energy import HomeWizardEnergy
|
||||||
from homewizard_energy.errors import DisabledError, RequestError, UnsupportedError
|
from homewizard_energy.errors import DisabledError, RequestError, UnsupportedError
|
||||||
|
from homewizard_energy.models import Device
|
||||||
from voluptuous import Required, Schema
|
from voluptuous import Required, Schema
|
||||||
|
|
||||||
from homeassistant import config_entries
|
from homeassistant import config_entries
|
||||||
from homeassistant.components import persistent_notification, zeroconf
|
from homeassistant.components import persistent_notification, zeroconf
|
||||||
from homeassistant.const import CONF_IP_ADDRESS
|
from homeassistant.const import CONF_IP_ADDRESS
|
||||||
from homeassistant.data_entry_flow import AbortFlow, FlowResult
|
from homeassistant.data_entry_flow import AbortFlow, FlowResult
|
||||||
|
from homeassistant.exceptions import HomeAssistantError
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
CONF_API_ENABLED,
|
CONF_API_ENABLED,
|
||||||
@ -73,19 +75,17 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
errors=None,
|
errors=None,
|
||||||
)
|
)
|
||||||
|
|
||||||
error = await self._async_try_connect(user_input[CONF_IP_ADDRESS])
|
# Fetch device information
|
||||||
if error is not None:
|
try:
|
||||||
|
device_info = await self._async_try_connect(user_input[CONF_IP_ADDRESS])
|
||||||
|
except RecoverableError as ex:
|
||||||
|
_LOGGER.error(ex)
|
||||||
return self.async_show_form(
|
return self.async_show_form(
|
||||||
step_id="user",
|
step_id="user",
|
||||||
data_schema=data_schema,
|
data_schema=data_schema,
|
||||||
errors={"base": error},
|
errors={"base": ex.error_code},
|
||||||
)
|
)
|
||||||
|
|
||||||
# Fetch device information
|
|
||||||
api = HomeWizardEnergy(user_input[CONF_IP_ADDRESS])
|
|
||||||
device_info = await api.device()
|
|
||||||
await api.close()
|
|
||||||
|
|
||||||
# Sets unique ID and aborts if it is already exists
|
# Sets unique ID and aborts if it is already exists
|
||||||
await self._async_set_and_check_unique_id(
|
await self._async_set_and_check_unique_id(
|
||||||
{
|
{
|
||||||
@ -153,12 +153,13 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
"""Confirm discovery."""
|
"""Confirm discovery."""
|
||||||
if user_input is not None:
|
if user_input is not None:
|
||||||
|
|
||||||
# Check connection
|
try:
|
||||||
error = await self._async_try_connect(str(self.config[CONF_IP_ADDRESS]))
|
await self._async_try_connect(str(self.config[CONF_IP_ADDRESS]))
|
||||||
if error is not None:
|
except RecoverableError as ex:
|
||||||
|
_LOGGER.error(ex)
|
||||||
return self.async_show_form(
|
return self.async_show_form(
|
||||||
step_id="discovery_confirm",
|
step_id="discovery_confirm",
|
||||||
errors={"base": error},
|
errors={"base": ex.error_code},
|
||||||
)
|
)
|
||||||
|
|
||||||
return self.async_create_entry(
|
return self.async_create_entry(
|
||||||
@ -197,11 +198,13 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
if user_input is not None:
|
if user_input is not None:
|
||||||
assert self.entry is not None
|
assert self.entry is not None
|
||||||
|
|
||||||
error = await self._async_try_connect(self.entry.data[CONF_IP_ADDRESS])
|
try:
|
||||||
if error is not None:
|
await self._async_try_connect(self.entry.data[CONF_IP_ADDRESS])
|
||||||
|
except RecoverableError as ex:
|
||||||
|
_LOGGER.error(ex)
|
||||||
return self.async_show_form(
|
return self.async_show_form(
|
||||||
step_id="reauth_confirm",
|
step_id="reauth_confirm",
|
||||||
errors={"base": error},
|
errors={"base": ex.error_code},
|
||||||
)
|
)
|
||||||
|
|
||||||
await self.hass.config_entries.async_reload(self.entry.entry_id)
|
await self.hass.config_entries.async_reload(self.entry.entry_id)
|
||||||
@ -212,7 +215,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
)
|
)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def _async_try_connect(ip_address: str) -> str | None:
|
async def _async_try_connect(ip_address: str) -> Device:
|
||||||
"""Try to connect."""
|
"""Try to connect."""
|
||||||
|
|
||||||
_LOGGER.debug("config_flow _async_try_connect")
|
_LOGGER.debug("config_flow _async_try_connect")
|
||||||
@ -222,19 +225,21 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
energy_api = HomeWizardEnergy(ip_address)
|
energy_api = HomeWizardEnergy(ip_address)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
await energy_api.device()
|
return await energy_api.device()
|
||||||
|
|
||||||
except DisabledError:
|
except DisabledError as ex:
|
||||||
_LOGGER.error("API disabled, API must be enabled in the app")
|
raise RecoverableError(
|
||||||
return "api_not_enabled"
|
"API disabled, API must be enabled in the app", "api_not_enabled"
|
||||||
|
) from ex
|
||||||
|
|
||||||
except UnsupportedError as ex:
|
except UnsupportedError as ex:
|
||||||
_LOGGER.error("API version unsuppored")
|
_LOGGER.error("API version unsuppored")
|
||||||
raise AbortFlow("unsupported_api_version") from ex
|
raise AbortFlow("unsupported_api_version") from ex
|
||||||
|
|
||||||
except RequestError as ex:
|
except RequestError as ex:
|
||||||
_LOGGER.exception(ex)
|
raise RecoverableError(
|
||||||
return "network_error"
|
"Device unreachable or unexpected response", "network_error"
|
||||||
|
) from ex
|
||||||
|
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
_LOGGER.exception(ex)
|
_LOGGER.exception(ex)
|
||||||
@ -243,8 +248,6 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
finally:
|
finally:
|
||||||
await energy_api.close()
|
await energy_api.close()
|
||||||
|
|
||||||
return None
|
|
||||||
|
|
||||||
async def _async_set_and_check_unique_id(self, entry_info: dict[str, Any]) -> None:
|
async def _async_set_and_check_unique_id(self, entry_info: dict[str, Any]) -> None:
|
||||||
"""Validate if entry exists."""
|
"""Validate if entry exists."""
|
||||||
|
|
||||||
@ -256,3 +259,12 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
self._abort_if_unique_id_configured(
|
self._abort_if_unique_id_configured(
|
||||||
updates={CONF_IP_ADDRESS: entry_info[CONF_IP_ADDRESS]}
|
updates={CONF_IP_ADDRESS: entry_info[CONF_IP_ADDRESS]}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class RecoverableError(HomeAssistantError):
|
||||||
|
"""Raised when a connection has been failed but can be retried."""
|
||||||
|
|
||||||
|
def __init__(self, message: str, error_code: str) -> None:
|
||||||
|
"""Init RecoverableError."""
|
||||||
|
super().__init__(message)
|
||||||
|
self.error_code = error_code
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
"""Update coordinator for HomeWizard."""
|
"""Update coordinator for HomeWizard."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from datetime import timedelta
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from homewizard_energy import HomeWizardEnergy
|
from homewizard_energy import HomeWizardEnergy
|
||||||
@ -16,8 +15,6 @@ from .const import DOMAIN, UPDATE_INTERVAL, DeviceResponseEntry
|
|||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
MAX_UPDATE_INTERVAL = timedelta(minutes=30)
|
|
||||||
|
|
||||||
|
|
||||||
class HWEnergyDeviceUpdateCoordinator(DataUpdateCoordinator[DeviceResponseEntry]):
|
class HWEnergyDeviceUpdateCoordinator(DataUpdateCoordinator[DeviceResponseEntry]):
|
||||||
"""Gather data for the energy device."""
|
"""Gather data for the energy device."""
|
||||||
@ -73,7 +70,6 @@ class HWEnergyDeviceUpdateCoordinator(DataUpdateCoordinator[DeviceResponseEntry]
|
|||||||
|
|
||||||
raise UpdateFailed(ex) from ex
|
raise UpdateFailed(ex) from ex
|
||||||
|
|
||||||
else:
|
self.api_disabled = False
|
||||||
self.api_disabled = False
|
|
||||||
|
|
||||||
return data
|
return data
|
||||||
|
@ -385,6 +385,9 @@ async def test_reauth_flow(hass, aioclient_mock):
|
|||||||
|
|
||||||
device = get_mock_device()
|
device = get_mock_device()
|
||||||
with patch(
|
with patch(
|
||||||
|
"homeassistant.components.homewizard.async_setup_entry",
|
||||||
|
return_value=True,
|
||||||
|
), patch(
|
||||||
"homeassistant.components.homewizard.config_flow.HomeWizardEnergy",
|
"homeassistant.components.homewizard.config_flow.HomeWizardEnergy",
|
||||||
return_value=device,
|
return_value=device,
|
||||||
):
|
):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user