mirror of
https://github.com/home-assistant/core.git
synced 2026-12-03 04:08:19 -04:00
Handle unsupported Daikin adapters (#192360)
This commit is contained in:
@@ -5,7 +5,7 @@ import logging
|
||||
|
||||
from aiohttp import ClientConnectionError
|
||||
from pydaikin.daikin_base import Appliance
|
||||
from pydaikin.exceptions import DaikinException
|
||||
from pydaikin.exceptions import DaikinException, DaikinUnsupportedError
|
||||
from pydaikin.factory import DaikinFactory
|
||||
|
||||
from homeassistant.const import (
|
||||
@@ -16,7 +16,7 @@ from homeassistant.const import (
|
||||
Platform,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.exceptions import ConfigEntryNotReady
|
||||
from homeassistant.exceptions import ConfigEntryError, ConfigEntryNotReady
|
||||
from homeassistant.helpers import (
|
||||
config_validation as cv,
|
||||
device_registry as dr,
|
||||
@@ -70,11 +70,16 @@ async def async_setup_entry(hass: HomeAssistant, entry: DaikinConfigEntry) -> No
|
||||
except ClientConnectionError as err:
|
||||
_LOGGER.debug("ClientConnectionError to %s", host)
|
||||
raise ConfigEntryNotReady from err
|
||||
except DaikinUnsupportedError as err:
|
||||
_LOGGER.debug("Unsupported Daikin device at %s: %s", host, err)
|
||||
raise ConfigEntryError(
|
||||
translation_domain=DOMAIN,
|
||||
translation_key="unsupported_device",
|
||||
) from err
|
||||
except DaikinException as err:
|
||||
# pydaikin has no subclass hierarchy for transient vs permanent errors.
|
||||
# DaikinException during factory/init almost always means the device is not
|
||||
# yet ready (e.g. "Empty values." when the unit hasn't finished booting),
|
||||
# so treat all factory-time DaikinExceptions as transient.
|
||||
# so treat remaining factory-time DaikinExceptions as transient.
|
||||
_LOGGER.debug("DaikinException from %s: %s", host, err)
|
||||
raise ConfigEntryNotReady from err
|
||||
|
||||
|
||||
@@ -8,8 +8,8 @@ from uuid import uuid4
|
||||
from aiohttp import ClientError, web_exceptions
|
||||
import probatio
|
||||
from pydaikin.daikin_base import Appliance
|
||||
from pydaikin.discovery import Discovery
|
||||
from pydaikin.exceptions import DaikinException
|
||||
from pydaikin.discovery import Discovery, is_unsupported
|
||||
from pydaikin.exceptions import DaikinException, DaikinUnsupportedError
|
||||
from pydaikin.factory import DaikinFactory
|
||||
|
||||
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
|
||||
@@ -91,6 +91,8 @@ class FlowHandler(ConfigFlow, domain=DOMAIN):
|
||||
password=password,
|
||||
ssl_context=client_context_no_verify(),
|
||||
)
|
||||
except DaikinUnsupportedError:
|
||||
return self.async_abort(reason="unsupported_device")
|
||||
except TimeoutError, ClientError:
|
||||
self.host = None
|
||||
return self.async_show_form(
|
||||
@@ -148,7 +150,8 @@ class FlowHandler(ConfigFlow, domain=DOMAIN):
|
||||
"""Prepare configuration for a discovered Daikin device."""
|
||||
_LOGGER.debug("Zeroconf user_input: %s", discovery_info)
|
||||
devices = Discovery().poll(ip=discovery_info.host)
|
||||
if not devices:
|
||||
device = next(iter(devices), None)
|
||||
if device is None:
|
||||
_LOGGER.debug(
|
||||
(
|
||||
"Could not find MAC-address for %s, make sure the required UDP"
|
||||
@@ -157,7 +160,9 @@ class FlowHandler(ConfigFlow, domain=DOMAIN):
|
||||
discovery_info.host,
|
||||
)
|
||||
return self.async_abort(reason="cannot_connect")
|
||||
await self.async_set_unique_id(next(iter(devices))[KEY_MAC])
|
||||
if is_unsupported(device):
|
||||
return self.async_abort(reason="unsupported_device")
|
||||
await self.async_set_unique_id(device[KEY_MAC])
|
||||
self._abort_if_unique_id_configured()
|
||||
self.host = discovery_info.host
|
||||
return await self.async_step_user()
|
||||
|
||||
@@ -2,7 +2,8 @@
|
||||
"config": {
|
||||
"abort": {
|
||||
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]",
|
||||
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]"
|
||||
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
|
||||
"unsupported_device": "This Daikin adapter has no local control API. BRP069C4x/C8x require firmware 2.8.0 or later, while built-in adapters such as the Stylish FTXA20C2V1BW have no local API at all (2.6.3 is the latest firmware). Control the device through the Daikin cloud/Onecta app."
|
||||
},
|
||||
"error": {
|
||||
"api_password": "Invalid authentication, use either API Key or Password.",
|
||||
@@ -68,6 +69,9 @@
|
||||
"error_communicating": {
|
||||
"message": "Error communicating with Daikin device: {error}"
|
||||
},
|
||||
"unsupported_device": {
|
||||
"message": "This Daikin adapter has no local control API. Control the device through the Daikin cloud/Onecta app."
|
||||
},
|
||||
"zone_hvac_mode_unsupported": {
|
||||
"message": "Zone temperature can only be changed when the main climate mode is heat or cool."
|
||||
},
|
||||
|
||||
@@ -5,7 +5,7 @@ from ipaddress import ip_address
|
||||
from unittest.mock import AsyncMock, MagicMock, PropertyMock, patch
|
||||
|
||||
from aiohttp import ClientError, web_exceptions
|
||||
from pydaikin.exceptions import DaikinException
|
||||
from pydaikin.exceptions import DaikinException, DaikinUnsupportedError
|
||||
import pytest
|
||||
from pytest_homeassistant.common import MockConfigEntry
|
||||
|
||||
@@ -18,6 +18,15 @@ from homeassistant.helpers.service_info.zeroconf import ZeroconfServiceInfo
|
||||
|
||||
MAC = "AABBCCDDEEFF"
|
||||
HOST = "127.0.0.1"
|
||||
ZEROCONF_INFO = ZeroconfServiceInfo(
|
||||
ip_address=ip_address(HOST),
|
||||
ip_addresses=[ip_address(HOST)],
|
||||
hostname="mock_hostname",
|
||||
name="mock_name",
|
||||
port=None,
|
||||
properties={},
|
||||
type="mock_type",
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
@@ -55,6 +64,29 @@ def mock_daikin_discovery():
|
||||
yield Discovery
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_daikin_discovery_unsupported() -> Generator[MagicMock]:
|
||||
"""Mock pydaikin Discovery returning an unsupported device."""
|
||||
with patch("homeassistant.components.daikin.config_flow.Discovery") as Discovery:
|
||||
Discovery().poll.return_value = {
|
||||
"127.0.0.1": {
|
||||
"mac": MAC,
|
||||
"type": "GPF",
|
||||
"protocol": "DGC",
|
||||
"ver": "2_6_3",
|
||||
}
|
||||
}.values()
|
||||
yield Discovery
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_daikin_discovery_empty() -> Generator[MagicMock]:
|
||||
"""Mock pydaikin Discovery returning no devices."""
|
||||
with patch("homeassistant.components.daikin.config_flow.Discovery") as Discovery:
|
||||
Discovery().poll.return_value = {}.values()
|
||||
yield Discovery
|
||||
|
||||
|
||||
async def test_user(hass: HomeAssistant, mock_daikin) -> None:
|
||||
"""Test user config."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
@@ -138,6 +170,26 @@ async def test_device_abort(
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
|
||||
|
||||
async def test_user_unsupported_device(
|
||||
hass: HomeAssistant, mock_daikin: MagicMock
|
||||
) -> None:
|
||||
"""Test user flow aborts for a detected but unsupported device."""
|
||||
mock_daikin.side_effect = DaikinUnsupportedError("not supported")
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "user"
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={CONF_HOST: HOST},
|
||||
)
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "unsupported_device"
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("mock_daikin")
|
||||
async def test_api_password_abort(hass: HomeAssistant) -> None:
|
||||
"""Test device abort."""
|
||||
@@ -168,15 +220,7 @@ async def test_api_password_abort(hass: HomeAssistant) -> None:
|
||||
[
|
||||
(
|
||||
SOURCE_ZEROCONF,
|
||||
ZeroconfServiceInfo(
|
||||
ip_address=ip_address(HOST),
|
||||
ip_addresses=[ip_address(HOST)],
|
||||
hostname="mock_hostname",
|
||||
name="mock_name",
|
||||
port=None,
|
||||
properties={},
|
||||
type="mock_type",
|
||||
),
|
||||
ZEROCONF_INFO,
|
||||
MAC,
|
||||
),
|
||||
],
|
||||
@@ -217,3 +261,31 @@ async def test_discovery_zeroconf(
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "already_in_progress"
|
||||
|
||||
|
||||
async def test_discovery_zeroconf_unsupported_device(
|
||||
hass: HomeAssistant, mock_daikin_discovery_unsupported: MagicMock
|
||||
) -> None:
|
||||
"""Test zeroconf aborts for a detected but unsupported device."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_ZEROCONF},
|
||||
data=ZEROCONF_INFO,
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "unsupported_device"
|
||||
|
||||
|
||||
async def test_discovery_zeroconf_cannot_connect(
|
||||
hass: HomeAssistant, mock_daikin_discovery_empty: MagicMock
|
||||
) -> None:
|
||||
"""Test zeroconf aborts when no device is discovered."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_ZEROCONF},
|
||||
data=ZEROCONF_INFO,
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "cannot_connect"
|
||||
|
||||
@@ -5,7 +5,7 @@ from unittest.mock import AsyncMock, PropertyMock, patch
|
||||
|
||||
from aiohttp import ClientConnectionError
|
||||
from freezegun.api import FrozenDateTimeFactory
|
||||
from pydaikin.exceptions import DaikinException
|
||||
from pydaikin.exceptions import DaikinException, DaikinUnsupportedError
|
||||
import pytest
|
||||
from pytest_homeassistant.common import MockConfigEntry, async_fire_time_changed
|
||||
|
||||
@@ -263,3 +263,20 @@ async def test_daikin_exception_retries(hass: HomeAssistant, mock_daikin) -> Non
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert config_entry.state is ConfigEntryState.SETUP_RETRY
|
||||
|
||||
|
||||
async def test_daikin_unsupported_error(hass: HomeAssistant, mock_daikin) -> None:
|
||||
"""Test that a DaikinUnsupportedError during setup fails permanently."""
|
||||
config_entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
unique_id=MAC,
|
||||
data={CONF_HOST: HOST, KEY_MAC: MAC},
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
mock_daikin.side_effect = DaikinUnsupportedError("not supported")
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert config_entry.state is ConfigEntryState.SETUP_ERROR
|
||||
assert config_entry.error_reason_translation_key == "unsupported_device"
|
||||
|
||||
Reference in New Issue
Block a user