Bump pydaikin to 2.0.1, catch HTTPForbidden exception (#35466)

This commit is contained in:
Fredrik Erlandsson 2020-05-11 19:39:20 +02:00 committed by GitHub
parent 3de4bc56b5
commit 93f8d21bc0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 60 additions and 35 deletions

View file

@ -3,7 +3,7 @@ import asyncio
import logging
from uuid import uuid4
from aiohttp import ClientError
from aiohttp import ClientError, web_exceptions
from async_timeout import timeout
from pydaikin.daikin_base import Appliance
import voluptuous as vol
@ -15,6 +15,14 @@ from .const import CONF_KEY, CONF_UUID, KEY_IP, KEY_MAC, TIMEOUT
_LOGGER = logging.getLogger(__name__)
DATA_SCHEMA = vol.Schema(
{
vol.Required(CONF_HOST): str,
vol.Optional(CONF_KEY): str,
vol.Optional(CONF_PASSWORD): str,
}
)
@config_entries.HANDLERS.register("daikin")
class FlowHandler(config_entries.ConfigFlow):
@ -43,7 +51,6 @@ class FlowHandler(config_entries.ConfigFlow):
async def _create_device(self, host, key=None, password=None):
"""Create device."""
# BRP07Cxx devices needs uuid together with key
if key:
uuid = str(uuid4())
@ -64,13 +71,25 @@ class FlowHandler(config_entries.ConfigFlow):
password=password,
)
except asyncio.TimeoutError:
return self.async_abort(reason="device_timeout")
return self.async_show_form(
step_id="user",
data_schema=DATA_SCHEMA,
errors={"base": "device_timeout"},
)
except web_exceptions.HTTPForbidden:
return self.async_show_form(
step_id="user", data_schema=DATA_SCHEMA, errors={"base": "forbidden"},
)
except ClientError:
_LOGGER.exception("ClientError")
return self.async_abort(reason="device_fail")
return self.async_show_form(
step_id="user", data_schema=DATA_SCHEMA, errors={"base": "device_fail"},
)
except Exception: # pylint: disable=broad-except
_LOGGER.exception("Unexpected error creating device")
return self.async_abort(reason="device_fail")
return self.async_show_form(
step_id="user", data_schema=DATA_SCHEMA, errors={"base": "device_fail"},
)
mac = device.mac
return self._create_entry(host, mac, key, uuid, password)
@ -78,16 +97,7 @@ class FlowHandler(config_entries.ConfigFlow):
async def async_step_user(self, user_input=None):
"""User initiated config flow."""
if user_input is None:
return self.async_show_form(
step_id="user",
data_schema=vol.Schema(
{
vol.Required(CONF_HOST): str,
vol.Optional(CONF_KEY): str,
vol.Optional(CONF_PASSWORD): str,
}
),
)
return self.async_show_form(step_id="user", data_schema=DATA_SCHEMA,)
return await self._create_device(
user_input[CONF_HOST],
user_input.get(CONF_KEY),

View file

@ -3,7 +3,7 @@
"name": "Daikin AC",
"config_flow": true,
"documentation": "https://www.home-assistant.io/integrations/daikin",
"requirements": ["pydaikin==2.0.0"],
"requirements": ["pydaikin==2.0.1"],
"codeowners": ["@fredrike"],
"quality_scale": "platinum"
}

View file

@ -5,16 +5,19 @@
"title": "Configure Daikin AC",
"description": "Enter IP address of your Daikin AC.",
"data": {
"host": "Host",
"key": "Authentication key (only used by BRP072C/Zena devices)",
"password": "Device password (only used by SKYFi devices)"
}
"host": "[%key:common::config_flow::data::host%]",
"key": "[%key:common::config_flow::data::api_key%]",
"password": "[%key:common::config_flow::data::password%]"
}
}
},
"abort": {
"device_timeout": "Timeout connecting to the device.",
"device_fail": "Unexpected error creating device.",
"already_configured": "Device is already configured"
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]"
},
"error": {
"device_fail": "[%key:common::config_flow::error::unknown%]",
"forbidden": "[%key:common::config_flow::error::invalid_auth%]",
"device_timeout": "[%key:common::config_flow::error::cannot_connect%]"
}
}
}

View file

@ -1263,7 +1263,7 @@ pycsspeechtts==1.0.3
# pycups==1.9.73
# homeassistant.components.daikin
pydaikin==2.0.0
pydaikin==2.0.1
# homeassistant.components.danfoss_air
pydanfossair==0.1.0

View file

@ -533,7 +533,7 @@ pychromecast==5.1.0
pycoolmasternet==0.0.4
# homeassistant.components.daikin
pydaikin==2.0.0
pydaikin==2.0.1
# homeassistant.components.deconz
pydeconz==70

View file

@ -2,12 +2,18 @@
"""Tests for the Daikin config flow."""
import asyncio
from aiohttp import ClientError
from aiohttp.web_exceptions import HTTPForbidden
import pytest
from homeassistant import data_entry_flow
from homeassistant.components.daikin import config_flow
from homeassistant.components.daikin.const import KEY_IP, KEY_MAC
from homeassistant.const import CONF_HOST
from homeassistant.data_entry_flow import (
RESULT_TYPE_ABORT,
RESULT_TYPE_CREATE_ENTRY,
RESULT_TYPE_FORM,
)
from tests.async_mock import PropertyMock, patch
from tests.common import MockConfigEntry
@ -42,11 +48,11 @@ async def test_user(hass, mock_daikin):
flow = init_config_flow(hass)
result = await flow.async_step_user()
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["type"] == RESULT_TYPE_FORM
assert result["step_id"] == "user"
result = await flow.async_step_user({CONF_HOST: HOST})
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["type"] == RESULT_TYPE_CREATE_ENTRY
assert result["title"] == HOST
assert result["data"][CONF_HOST] == HOST
assert result["data"][KEY_MAC] == MAC
@ -58,7 +64,7 @@ async def test_abort_if_already_setup(hass, mock_daikin):
MockConfigEntry(domain="daikin", data={KEY_MAC: MAC}).add_to_hass(hass)
result = await flow.async_step_user({CONF_HOST: HOST})
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["type"] == RESULT_TYPE_ABORT
assert result["reason"] == "already_configured"
@ -67,11 +73,11 @@ async def test_import(hass, mock_daikin):
flow = init_config_flow(hass)
result = await flow.async_step_import({})
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["type"] == RESULT_TYPE_FORM
assert result["step_id"] == "user"
result = await flow.async_step_import({CONF_HOST: HOST})
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["type"] == RESULT_TYPE_CREATE_ENTRY
assert result["title"] == HOST
assert result["data"][CONF_HOST] == HOST
assert result["data"][KEY_MAC] == MAC
@ -82,7 +88,7 @@ async def test_discovery(hass, mock_daikin):
flow = init_config_flow(hass)
result = await flow.async_step_discovery({KEY_IP: HOST, KEY_MAC: MAC})
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["type"] == RESULT_TYPE_CREATE_ENTRY
assert result["title"] == HOST
assert result["data"][CONF_HOST] == HOST
assert result["data"][KEY_MAC] == MAC
@ -90,7 +96,12 @@ async def test_discovery(hass, mock_daikin):
@pytest.mark.parametrize(
"s_effect,reason",
[(asyncio.TimeoutError, "device_timeout"), (Exception, "device_fail")],
[
(asyncio.TimeoutError, "device_timeout"),
(HTTPForbidden, "forbidden"),
(ClientError, "device_fail"),
(Exception, "device_fail"),
],
)
async def test_device_abort(hass, mock_daikin, s_effect, reason):
"""Test device abort."""
@ -98,5 +109,6 @@ async def test_device_abort(hass, mock_daikin, s_effect, reason):
mock_daikin.factory.side_effect = s_effect
result = await flow.async_step_user({CONF_HOST: HOST})
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["reason"] == reason
assert result["type"] == RESULT_TYPE_FORM
assert result["errors"] == {"base": reason}
assert result["step_id"] == "user"