Allow HomeWizard devices with disabled api to show up in discovery (#65295)

pull/66221/head
Duco Sebel 2022-02-08 23:51:28 +01:00 committed by GitHub
parent 5ebc02cef6
commit f2fe091979
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 27 deletions

View File

@ -14,7 +14,14 @@ from homeassistant.components import persistent_notification, zeroconf
from homeassistant.const import CONF_IP_ADDRESS
from homeassistant.data_entry_flow import AbortFlow, FlowResult
from .const import CONF_PRODUCT_NAME, CONF_PRODUCT_TYPE, CONF_SERIAL, DOMAIN
from .const import (
CONF_API_ENABLED,
CONF_PATH,
CONF_PRODUCT_NAME,
CONF_PRODUCT_TYPE,
CONF_SERIAL,
DOMAIN,
)
_LOGGER = logging.getLogger(__name__)
@ -28,7 +35,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Initialize the HomeWizard config flow."""
self.config: dict[str, str | int] = {}
async def async_step_import(self, import_config: dict) -> FlowResult:
async def async_step_import(self, import_config: dict[str, Any]) -> FlowResult:
"""Handle a flow initiated by older `homewizard_energy` component."""
_LOGGER.debug("config_flow async_step_import")
@ -97,40 +104,33 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
# Validate doscovery entry
if (
"api_enabled" not in discovery_info.properties
or "path" not in discovery_info.properties
or "product_name" not in discovery_info.properties
or "product_type" not in discovery_info.properties
or "serial" not in discovery_info.properties
CONF_API_ENABLED not in discovery_info.properties
or CONF_PATH not in discovery_info.properties
or CONF_PRODUCT_NAME not in discovery_info.properties
or CONF_PRODUCT_TYPE not in discovery_info.properties
or CONF_SERIAL not in discovery_info.properties
):
return self.async_abort(reason="invalid_discovery_parameters")
if (discovery_info.properties["path"]) != "/api/v1":
if (discovery_info.properties[CONF_PATH]) != "/api/v1":
return self.async_abort(reason="unsupported_api_version")
if (discovery_info.properties["api_enabled"]) != "1":
return self.async_abort(reason="api_not_enabled")
# Sets unique ID and aborts if it is already exists
await self._async_set_and_check_unique_id(
{
CONF_IP_ADDRESS: discovery_info.host,
CONF_PRODUCT_TYPE: discovery_info.properties["product_type"],
CONF_SERIAL: discovery_info.properties["serial"],
CONF_PRODUCT_TYPE: discovery_info.properties[CONF_PRODUCT_TYPE],
CONF_SERIAL: discovery_info.properties[CONF_SERIAL],
}
)
# Check connection and fetch
device_info: dict[str, Any] = await self._async_try_connect_and_fetch(
discovery_info.host
)
# Pass parameters
self.config = {
CONF_API_ENABLED: discovery_info.properties[CONF_API_ENABLED],
CONF_IP_ADDRESS: discovery_info.host,
CONF_PRODUCT_TYPE: device_info[CONF_PRODUCT_TYPE],
CONF_PRODUCT_NAME: device_info[CONF_PRODUCT_NAME],
CONF_SERIAL: device_info[CONF_SERIAL],
CONF_PRODUCT_TYPE: discovery_info.properties[CONF_PRODUCT_TYPE],
CONF_PRODUCT_NAME: discovery_info.properties[CONF_PRODUCT_NAME],
CONF_SERIAL: discovery_info.properties[CONF_SERIAL],
}
return await self.async_step_discovery_confirm()
@ -139,6 +139,12 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
) -> FlowResult:
"""Confirm discovery."""
if user_input is not None:
if (self.config[CONF_API_ENABLED]) != "1":
raise AbortFlow(reason="api_not_enabled")
# Check connection
await self._async_try_connect_and_fetch(str(self.config[CONF_IP_ADDRESS]))
return self.async_create_entry(
title=f"{self.config[CONF_PRODUCT_NAME]} ({self.config[CONF_SERIAL]})",
data={

View File

@ -14,11 +14,13 @@ DOMAIN = "homewizard"
PLATFORMS = [Platform.SENSOR, Platform.SWITCH]
# Platform config.
CONF_SERIAL = "serial"
CONF_API_ENABLED = "api_enabled"
CONF_DATA = "data"
CONF_DEVICE = "device"
CONF_PATH = "path"
CONF_PRODUCT_NAME = "product_name"
CONF_PRODUCT_TYPE = "product_type"
CONF_DEVICE = "device"
CONF_DATA = "data"
CONF_SERIAL = "serial"
UPDATE_INTERVAL = timedelta(seconds=5)

View File

@ -8,7 +8,11 @@ from homeassistant import config_entries
from homeassistant.components import zeroconf
from homeassistant.components.homewizard.const import DOMAIN
from homeassistant.const import CONF_IP_ADDRESS
from homeassistant.data_entry_flow import RESULT_TYPE_ABORT, RESULT_TYPE_CREATE_ENTRY
from homeassistant.data_entry_flow import (
RESULT_TYPE_ABORT,
RESULT_TYPE_CREATE_ENTRY,
RESULT_TYPE_FORM,
)
from .generator import get_mock_device
@ -77,9 +81,19 @@ async def test_discovery_flow_works(hass, aioclient_mock):
with patch(
"homeassistant.components.homewizard.async_setup_entry",
return_value=True,
):
), patch("aiohwenergy.HomeWizardEnergy", return_value=get_mock_device()):
result = await hass.config_entries.flow.async_configure(
flow["flow_id"], user_input={}
flow["flow_id"], user_input=None
)
assert result["type"] == RESULT_TYPE_FORM
assert result["step_id"] == "discovery_confirm"
with patch(
"homeassistant.components.homewizard.async_setup_entry",
return_value=True,
), patch("aiohwenergy.HomeWizardEnergy", return_value=get_mock_device()):
result = await hass.config_entries.flow.async_configure(
flow["flow_id"], user_input={"ip_address": "192.168.43.183"}
)
assert result["type"] == RESULT_TYPE_CREATE_ENTRY
@ -145,6 +159,16 @@ async def test_discovery_disabled_api(hass, aioclient_mock):
data=service_info,
)
assert result["type"] == RESULT_TYPE_FORM
with patch(
"homeassistant.components.homewizard.async_setup_entry",
return_value=True,
), patch("aiohwenergy.HomeWizardEnergy", return_value=get_mock_device()):
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={"ip_address": "192.168.43.183"}
)
assert result["type"] == RESULT_TYPE_ABORT
assert result["reason"] == "api_not_enabled"