Upgrade pyrisco to 0.5.0 (#75648)

* Upgrade to pyrisco 0.4.0

* Parametrized error tests in config flow

* Inline error parameters

* Switch to RiscoCloud
This commit is contained in:
On Freund 2022-07-24 00:44:48 +03:00 committed by GitHub
parent c5afaa2e6a
commit 7cf2d1759d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 71 additions and 77 deletions

View file

@ -2,7 +2,7 @@
from datetime import timedelta from datetime import timedelta
import logging import logging
from pyrisco import CannotConnectError, OperationError, RiscoAPI, UnauthorizedError from pyrisco import CannotConnectError, OperationError, RiscoCloud, UnauthorizedError
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
@ -30,7 +30,7 @@ _LOGGER = logging.getLogger(__name__)
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Risco from a config entry.""" """Set up Risco from a config entry."""
data = entry.data data = entry.data
risco = RiscoAPI(data[CONF_USERNAME], data[CONF_PASSWORD], data[CONF_PIN]) risco = RiscoCloud(data[CONF_USERNAME], data[CONF_PASSWORD], data[CONF_PIN])
try: try:
await risco.login(async_get_clientsession(hass)) await risco.login(async_get_clientsession(hass))
except CannotConnectError as error: except CannotConnectError as error:

View file

@ -3,7 +3,7 @@ from __future__ import annotations
import logging import logging
from pyrisco import CannotConnectError, RiscoAPI, UnauthorizedError from pyrisco import CannotConnectError, RiscoCloud, UnauthorizedError
import voluptuous as vol import voluptuous as vol
from homeassistant import config_entries, core from homeassistant import config_entries, core
@ -52,7 +52,7 @@ async def validate_input(hass: core.HomeAssistant, data):
Data has the keys from DATA_SCHEMA with values provided by the user. Data has the keys from DATA_SCHEMA with values provided by the user.
""" """
risco = RiscoAPI(data[CONF_USERNAME], data[CONF_PASSWORD], data[CONF_PIN]) risco = RiscoCloud(data[CONF_USERNAME], data[CONF_PASSWORD], data[CONF_PIN])
try: try:
await risco.login(async_get_clientsession(hass)) await risco.login(async_get_clientsession(hass))

View file

@ -3,7 +3,7 @@
"name": "Risco", "name": "Risco",
"config_flow": true, "config_flow": true,
"documentation": "https://www.home-assistant.io/integrations/risco", "documentation": "https://www.home-assistant.io/integrations/risco",
"requirements": ["pyrisco==0.3.1"], "requirements": ["pyrisco==0.5.0"],
"codeowners": ["@OnFreund"], "codeowners": ["@OnFreund"],
"quality_scale": "platinum", "quality_scale": "platinum",
"iot_class": "cloud_polling", "iot_class": "cloud_polling",

View file

@ -1785,7 +1785,7 @@ pyrecswitch==1.0.2
pyrepetierng==0.1.0 pyrepetierng==0.1.0
# homeassistant.components.risco # homeassistant.components.risco
pyrisco==0.3.1 pyrisco==0.5.0
# homeassistant.components.rituals_perfume_genie # homeassistant.components.rituals_perfume_genie
pyrituals==0.0.6 pyrituals==0.0.6

View file

@ -1222,7 +1222,7 @@ pyps4-2ndscreen==1.3.1
pyqwikswitch==0.93 pyqwikswitch==0.93
# homeassistant.components.risco # homeassistant.components.risco
pyrisco==0.3.1 pyrisco==0.5.0
# homeassistant.components.rituals_perfume_genie # homeassistant.components.rituals_perfume_genie
pyrituals==0.0.6 pyrituals==0.0.6

View file

@ -99,7 +99,7 @@ def two_part_alarm():
"partitions", "partitions",
new_callable=PropertyMock(return_value=partition_mocks), new_callable=PropertyMock(return_value=partition_mocks),
), patch( ), patch(
"homeassistant.components.risco.RiscoAPI.get_state", "homeassistant.components.risco.RiscoCloud.get_state",
return_value=alarm_mock, return_value=alarm_mock,
): ):
yield alarm_mock yield alarm_mock
@ -109,7 +109,7 @@ async def test_cannot_connect(hass):
"""Test connection error.""" """Test connection error."""
with patch( with patch(
"homeassistant.components.risco.RiscoAPI.login", "homeassistant.components.risco.RiscoCloud.login",
side_effect=CannotConnectError, side_effect=CannotConnectError,
): ):
config_entry = MockConfigEntry(domain=DOMAIN, data=TEST_CONFIG) config_entry = MockConfigEntry(domain=DOMAIN, data=TEST_CONFIG)
@ -125,7 +125,7 @@ async def test_unauthorized(hass):
"""Test unauthorized error.""" """Test unauthorized error."""
with patch( with patch(
"homeassistant.components.risco.RiscoAPI.login", "homeassistant.components.risco.RiscoCloud.login",
side_effect=UnauthorizedError, side_effect=UnauthorizedError,
): ):
config_entry = MockConfigEntry(domain=DOMAIN, data=TEST_CONFIG) config_entry = MockConfigEntry(domain=DOMAIN, data=TEST_CONFIG)
@ -228,7 +228,7 @@ async def test_states(hass, two_part_alarm):
async def _test_service_call( async def _test_service_call(
hass, service, method, entity_id, partition_id, *args, **kwargs hass, service, method, entity_id, partition_id, *args, **kwargs
): ):
with patch(f"homeassistant.components.risco.RiscoAPI.{method}") as set_mock: with patch(f"homeassistant.components.risco.RiscoCloud.{method}") as set_mock:
await _call_alarm_service(hass, service, entity_id, **kwargs) await _call_alarm_service(hass, service, entity_id, **kwargs)
set_mock.assert_awaited_once_with(partition_id, *args) set_mock.assert_awaited_once_with(partition_id, *args)
@ -236,7 +236,7 @@ async def _test_service_call(
async def _test_no_service_call( async def _test_no_service_call(
hass, service, method, entity_id, partition_id, **kwargs hass, service, method, entity_id, partition_id, **kwargs
): ):
with patch(f"homeassistant.components.risco.RiscoAPI.{method}") as set_mock: with patch(f"homeassistant.components.risco.RiscoCloud.{method}") as set_mock:
await _call_alarm_service(hass, service, entity_id, **kwargs) await _call_alarm_service(hass, service, entity_id, **kwargs)
set_mock.assert_not_awaited() set_mock.assert_not_awaited()
@ -302,10 +302,20 @@ async def test_sets_full_custom_mapping(hass, two_part_alarm):
hass, SERVICE_ALARM_ARM_NIGHT, "group_arm", SECOND_ENTITY_ID, 1, "C" hass, SERVICE_ALARM_ARM_NIGHT, "group_arm", SECOND_ENTITY_ID, 1, "C"
) )
await _test_service_call( await _test_service_call(
hass, SERVICE_ALARM_ARM_CUSTOM_BYPASS, "group_arm", FIRST_ENTITY_ID, 0, "D" hass,
SERVICE_ALARM_ARM_CUSTOM_BYPASS,
"group_arm",
FIRST_ENTITY_ID,
0,
"D",
) )
await _test_service_call( await _test_service_call(
hass, SERVICE_ALARM_ARM_CUSTOM_BYPASS, "group_arm", SECOND_ENTITY_ID, 1, "D" hass,
SERVICE_ALARM_ARM_CUSTOM_BYPASS,
"group_arm",
SECOND_ENTITY_ID,
1,
"D",
) )
@ -333,10 +343,22 @@ async def test_sets_with_correct_code(hass, two_part_alarm):
hass, SERVICE_ALARM_ARM_HOME, "partial_arm", SECOND_ENTITY_ID, 1, **code hass, SERVICE_ALARM_ARM_HOME, "partial_arm", SECOND_ENTITY_ID, 1, **code
) )
await _test_service_call( await _test_service_call(
hass, SERVICE_ALARM_ARM_NIGHT, "group_arm", FIRST_ENTITY_ID, 0, "C", **code hass,
SERVICE_ALARM_ARM_NIGHT,
"group_arm",
FIRST_ENTITY_ID,
0,
"C",
**code,
) )
await _test_service_call( await _test_service_call(
hass, SERVICE_ALARM_ARM_NIGHT, "group_arm", SECOND_ENTITY_ID, 1, "C", **code hass,
SERVICE_ALARM_ARM_NIGHT,
"group_arm",
SECOND_ENTITY_ID,
1,
"C",
**code,
) )
with pytest.raises(HomeAssistantError): with pytest.raises(HomeAssistantError):
await _test_no_service_call( await _test_no_service_call(

View file

@ -20,7 +20,7 @@ async def test_cannot_connect(hass):
"""Test connection error.""" """Test connection error."""
with patch( with patch(
"homeassistant.components.risco.RiscoAPI.login", "homeassistant.components.risco.RiscoCloud.login",
side_effect=CannotConnectError, side_effect=CannotConnectError,
): ):
config_entry = MockConfigEntry(domain=DOMAIN, data=TEST_CONFIG) config_entry = MockConfigEntry(domain=DOMAIN, data=TEST_CONFIG)
@ -36,7 +36,7 @@ async def test_unauthorized(hass):
"""Test unauthorized error.""" """Test unauthorized error."""
with patch( with patch(
"homeassistant.components.risco.RiscoAPI.login", "homeassistant.components.risco.RiscoCloud.login",
side_effect=UnauthorizedError, side_effect=UnauthorizedError,
): ):
config_entry = MockConfigEntry(domain=DOMAIN, data=TEST_CONFIG) config_entry = MockConfigEntry(domain=DOMAIN, data=TEST_CONFIG)
@ -106,7 +106,7 @@ async def test_states(hass, two_zone_alarm): # noqa: F811
async def test_bypass(hass, two_zone_alarm): # noqa: F811 async def test_bypass(hass, two_zone_alarm): # noqa: F811
"""Test bypassing a zone.""" """Test bypassing a zone."""
await setup_risco(hass) await setup_risco(hass)
with patch("homeassistant.components.risco.RiscoAPI.bypass_zone") as mock: with patch("homeassistant.components.risco.RiscoCloud.bypass_zone") as mock:
data = {"entity_id": FIRST_ENTITY_ID} data = {"entity_id": FIRST_ENTITY_ID}
await hass.services.async_call( await hass.services.async_call(
@ -119,7 +119,7 @@ async def test_bypass(hass, two_zone_alarm): # noqa: F811
async def test_unbypass(hass, two_zone_alarm): # noqa: F811 async def test_unbypass(hass, two_zone_alarm): # noqa: F811
"""Test unbypassing a zone.""" """Test unbypassing a zone."""
await setup_risco(hass) await setup_risco(hass)
with patch("homeassistant.components.risco.RiscoAPI.bypass_zone") as mock: with patch("homeassistant.components.risco.RiscoCloud.bypass_zone") as mock:
data = {"entity_id": FIRST_ENTITY_ID} data = {"entity_id": FIRST_ENTITY_ID}
await hass.services.async_call( await hass.services.async_call(

View file

@ -51,13 +51,13 @@ async def test_form(hass):
assert result["errors"] == {} assert result["errors"] == {}
with patch( with patch(
"homeassistant.components.risco.config_flow.RiscoAPI.login", "homeassistant.components.risco.config_flow.RiscoCloud.login",
return_value=True, return_value=True,
), patch( ), patch(
"homeassistant.components.risco.config_flow.RiscoAPI.site_name", "homeassistant.components.risco.config_flow.RiscoCloud.site_name",
new_callable=PropertyMock(return_value=TEST_SITE_NAME), new_callable=PropertyMock(return_value=TEST_SITE_NAME),
), patch( ), patch(
"homeassistant.components.risco.config_flow.RiscoAPI.close" "homeassistant.components.risco.config_flow.RiscoCloud.close"
) as mock_close, patch( ) as mock_close, patch(
"homeassistant.components.risco.async_setup_entry", "homeassistant.components.risco.async_setup_entry",
return_value=True, return_value=True,
@ -74,61 +74,33 @@ async def test_form(hass):
mock_close.assert_awaited_once() mock_close.assert_awaited_once()
async def test_form_invalid_auth(hass): @pytest.mark.parametrize(
"""Test we handle invalid auth.""" "exception, error",
[
(UnauthorizedError, "invalid_auth"),
(CannotConnectError, "cannot_connect"),
(Exception, "unknown"),
],
)
async def test_error(hass, exception, error):
"""Test we handle config flow errors."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER} DOMAIN, context={"source": config_entries.SOURCE_USER}
) )
with patch( with patch(
"homeassistant.components.risco.config_flow.RiscoAPI.login", "homeassistant.components.risco.config_flow.RiscoCloud.login",
side_effect=UnauthorizedError, side_effect=exception,
), patch("homeassistant.components.risco.config_flow.RiscoAPI.close") as mock_close: ), patch(
"homeassistant.components.risco.config_flow.RiscoCloud.close"
) as mock_close:
result2 = await hass.config_entries.flow.async_configure( result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], TEST_DATA result["flow_id"], TEST_DATA
) )
assert result2["type"] == "form"
assert result2["errors"] == {"base": "invalid_auth"}
mock_close.assert_awaited_once() mock_close.assert_awaited_once()
async def test_form_cannot_connect(hass):
"""Test we handle cannot connect error."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(
"homeassistant.components.risco.config_flow.RiscoAPI.login",
side_effect=CannotConnectError,
), patch("homeassistant.components.risco.config_flow.RiscoAPI.close") as mock_close:
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], TEST_DATA
)
assert result2["type"] == "form" assert result2["type"] == "form"
assert result2["errors"] == {"base": "cannot_connect"} assert result2["errors"] == {"base": error}
mock_close.assert_awaited_once()
async def test_form_exception(hass):
"""Test we handle unknown exception."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(
"homeassistant.components.risco.config_flow.RiscoAPI.login",
side_effect=Exception,
), patch("homeassistant.components.risco.config_flow.RiscoAPI.close") as mock_close:
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], TEST_DATA
)
assert result2["type"] == "form"
assert result2["errors"] == {"base": "unknown"}
mock_close.assert_awaited_once()
async def test_form_already_exists(hass): async def test_form_already_exists(hass):

View file

@ -113,7 +113,7 @@ async def test_cannot_connect(hass):
"""Test connection error.""" """Test connection error."""
with patch( with patch(
"homeassistant.components.risco.RiscoAPI.login", "homeassistant.components.risco.RiscoCloud.login",
side_effect=CannotConnectError, side_effect=CannotConnectError,
): ):
config_entry = MockConfigEntry(domain=DOMAIN, data=TEST_CONFIG) config_entry = MockConfigEntry(domain=DOMAIN, data=TEST_CONFIG)
@ -130,7 +130,7 @@ async def test_unauthorized(hass):
"""Test unauthorized error.""" """Test unauthorized error."""
with patch( with patch(
"homeassistant.components.risco.RiscoAPI.login", "homeassistant.components.risco.RiscoCloud.login",
side_effect=UnauthorizedError, side_effect=UnauthorizedError,
): ):
config_entry = MockConfigEntry(domain=DOMAIN, data=TEST_CONFIG) config_entry = MockConfigEntry(domain=DOMAIN, data=TEST_CONFIG)
@ -175,7 +175,7 @@ async def test_setup(hass, two_zone_alarm): # noqa: F811
assert not registry.async_is_registered(id) assert not registry.async_is_registered(id)
with patch( with patch(
"homeassistant.components.risco.RiscoAPI.site_uuid", "homeassistant.components.risco.RiscoCloud.site_uuid",
new_callable=PropertyMock(return_value=TEST_SITE_UUID), new_callable=PropertyMock(return_value=TEST_SITE_UUID),
), patch( ), patch(
"homeassistant.components.risco.Store.async_save", "homeassistant.components.risco.Store.async_save",
@ -191,7 +191,7 @@ async def test_setup(hass, two_zone_alarm): # noqa: F811
_check_state(hass, category, entity_id) _check_state(hass, category, entity_id)
with patch( with patch(
"homeassistant.components.risco.RiscoAPI.get_events", return_value=[] "homeassistant.components.risco.RiscoCloud.get_events", return_value=[]
) as events_mock, patch( ) as events_mock, patch(
"homeassistant.components.risco.Store.async_load", "homeassistant.components.risco.Store.async_load",
return_value={LAST_EVENT_TIMESTAMP_KEY: TEST_EVENTS[0].time}, return_value={LAST_EVENT_TIMESTAMP_KEY: TEST_EVENTS[0].time},

View file

@ -23,18 +23,18 @@ async def setup_risco(hass, events=[], options={}):
config_entry.add_to_hass(hass) config_entry.add_to_hass(hass)
with patch( with patch(
"homeassistant.components.risco.RiscoAPI.login", "homeassistant.components.risco.RiscoCloud.login",
return_value=True, return_value=True,
), patch( ), patch(
"homeassistant.components.risco.RiscoAPI.site_uuid", "homeassistant.components.risco.RiscoCloud.site_uuid",
new_callable=PropertyMock(return_value=TEST_SITE_UUID), new_callable=PropertyMock(return_value=TEST_SITE_UUID),
), patch( ), patch(
"homeassistant.components.risco.RiscoAPI.site_name", "homeassistant.components.risco.RiscoCloud.site_name",
new_callable=PropertyMock(return_value=TEST_SITE_NAME), new_callable=PropertyMock(return_value=TEST_SITE_NAME),
), patch( ), patch(
"homeassistant.components.risco.RiscoAPI.close" "homeassistant.components.risco.RiscoCloud.close"
), patch( ), patch(
"homeassistant.components.risco.RiscoAPI.get_events", "homeassistant.components.risco.RiscoCloud.get_events",
return_value=events, return_value=events,
): ):
await hass.config_entries.async_setup(config_entry.entry_id) await hass.config_entries.async_setup(config_entry.entry_id)
@ -68,7 +68,7 @@ def two_zone_alarm():
"zones", "zones",
new_callable=PropertyMock(return_value=zone_mocks), new_callable=PropertyMock(return_value=zone_mocks),
), patch( ), patch(
"homeassistant.components.risco.RiscoAPI.get_state", "homeassistant.components.risco.RiscoCloud.get_state",
return_value=alarm_mock, return_value=alarm_mock,
): ):
yield alarm_mock yield alarm_mock