From 7cf2d1759dde088105f77ca61dba8e58e3474b83 Mon Sep 17 00:00:00 2001 From: On Freund Date: Sun, 24 Jul 2022 00:44:48 +0300 Subject: [PATCH] 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 --- homeassistant/components/risco/__init__.py | 4 +- homeassistant/components/risco/config_flow.py | 4 +- homeassistant/components/risco/manifest.json | 2 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- .../risco/test_alarm_control_panel.py | 40 ++++++++--- tests/components/risco/test_binary_sensor.py | 8 +-- tests/components/risco/test_config_flow.py | 66 ++++++------------- tests/components/risco/test_sensor.py | 8 +-- tests/components/risco/util.py | 12 ++-- 10 files changed, 71 insertions(+), 77 deletions(-) diff --git a/homeassistant/components/risco/__init__.py b/homeassistant/components/risco/__init__.py index 1932548a907..fea3ee63aac 100644 --- a/homeassistant/components/risco/__init__.py +++ b/homeassistant/components/risco/__init__.py @@ -2,7 +2,7 @@ from datetime import timedelta import logging -from pyrisco import CannotConnectError, OperationError, RiscoAPI, UnauthorizedError +from pyrisco import CannotConnectError, OperationError, RiscoCloud, UnauthorizedError from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( @@ -30,7 +30,7 @@ _LOGGER = logging.getLogger(__name__) async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up Risco from a config entry.""" 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: await risco.login(async_get_clientsession(hass)) except CannotConnectError as error: diff --git a/homeassistant/components/risco/config_flow.py b/homeassistant/components/risco/config_flow.py index e2e139a19e0..5f8f40cb5f7 100644 --- a/homeassistant/components/risco/config_flow.py +++ b/homeassistant/components/risco/config_flow.py @@ -3,7 +3,7 @@ from __future__ import annotations import logging -from pyrisco import CannotConnectError, RiscoAPI, UnauthorizedError +from pyrisco import CannotConnectError, RiscoCloud, UnauthorizedError import voluptuous as vol 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. """ - risco = RiscoAPI(data[CONF_USERNAME], data[CONF_PASSWORD], data[CONF_PIN]) + risco = RiscoCloud(data[CONF_USERNAME], data[CONF_PASSWORD], data[CONF_PIN]) try: await risco.login(async_get_clientsession(hass)) diff --git a/homeassistant/components/risco/manifest.json b/homeassistant/components/risco/manifest.json index 736adcf0c35..a7c07af3e18 100644 --- a/homeassistant/components/risco/manifest.json +++ b/homeassistant/components/risco/manifest.json @@ -3,7 +3,7 @@ "name": "Risco", "config_flow": true, "documentation": "https://www.home-assistant.io/integrations/risco", - "requirements": ["pyrisco==0.3.1"], + "requirements": ["pyrisco==0.5.0"], "codeowners": ["@OnFreund"], "quality_scale": "platinum", "iot_class": "cloud_polling", diff --git a/requirements_all.txt b/requirements_all.txt index 80ca6871b48..4309d2f34c5 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1785,7 +1785,7 @@ pyrecswitch==1.0.2 pyrepetierng==0.1.0 # homeassistant.components.risco -pyrisco==0.3.1 +pyrisco==0.5.0 # homeassistant.components.rituals_perfume_genie pyrituals==0.0.6 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 6d0e6f54715..4d8e790b50f 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -1222,7 +1222,7 @@ pyps4-2ndscreen==1.3.1 pyqwikswitch==0.93 # homeassistant.components.risco -pyrisco==0.3.1 +pyrisco==0.5.0 # homeassistant.components.rituals_perfume_genie pyrituals==0.0.6 diff --git a/tests/components/risco/test_alarm_control_panel.py b/tests/components/risco/test_alarm_control_panel.py index 70ec7844624..4a82656147d 100644 --- a/tests/components/risco/test_alarm_control_panel.py +++ b/tests/components/risco/test_alarm_control_panel.py @@ -99,7 +99,7 @@ def two_part_alarm(): "partitions", new_callable=PropertyMock(return_value=partition_mocks), ), patch( - "homeassistant.components.risco.RiscoAPI.get_state", + "homeassistant.components.risco.RiscoCloud.get_state", return_value=alarm_mock, ): yield alarm_mock @@ -109,7 +109,7 @@ async def test_cannot_connect(hass): """Test connection error.""" with patch( - "homeassistant.components.risco.RiscoAPI.login", + "homeassistant.components.risco.RiscoCloud.login", side_effect=CannotConnectError, ): config_entry = MockConfigEntry(domain=DOMAIN, data=TEST_CONFIG) @@ -125,7 +125,7 @@ async def test_unauthorized(hass): """Test unauthorized error.""" with patch( - "homeassistant.components.risco.RiscoAPI.login", + "homeassistant.components.risco.RiscoCloud.login", side_effect=UnauthorizedError, ): 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( 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) set_mock.assert_awaited_once_with(partition_id, *args) @@ -236,7 +236,7 @@ async def _test_service_call( async def _test_no_service_call( 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) 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" ) 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( - 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 ) 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( - 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): await _test_no_service_call( diff --git a/tests/components/risco/test_binary_sensor.py b/tests/components/risco/test_binary_sensor.py index 7f68db7939d..a7c11c9cb00 100644 --- a/tests/components/risco/test_binary_sensor.py +++ b/tests/components/risco/test_binary_sensor.py @@ -20,7 +20,7 @@ async def test_cannot_connect(hass): """Test connection error.""" with patch( - "homeassistant.components.risco.RiscoAPI.login", + "homeassistant.components.risco.RiscoCloud.login", side_effect=CannotConnectError, ): config_entry = MockConfigEntry(domain=DOMAIN, data=TEST_CONFIG) @@ -36,7 +36,7 @@ async def test_unauthorized(hass): """Test unauthorized error.""" with patch( - "homeassistant.components.risco.RiscoAPI.login", + "homeassistant.components.risco.RiscoCloud.login", side_effect=UnauthorizedError, ): 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 """Test bypassing a zone.""" 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} 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 """Test unbypassing a zone.""" 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} await hass.services.async_call( diff --git a/tests/components/risco/test_config_flow.py b/tests/components/risco/test_config_flow.py index 8dd7c4bf7f7..8d04f478e44 100644 --- a/tests/components/risco/test_config_flow.py +++ b/tests/components/risco/test_config_flow.py @@ -51,13 +51,13 @@ async def test_form(hass): assert result["errors"] == {} with patch( - "homeassistant.components.risco.config_flow.RiscoAPI.login", + "homeassistant.components.risco.config_flow.RiscoCloud.login", return_value=True, ), 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), ), patch( - "homeassistant.components.risco.config_flow.RiscoAPI.close" + "homeassistant.components.risco.config_flow.RiscoCloud.close" ) as mock_close, patch( "homeassistant.components.risco.async_setup_entry", return_value=True, @@ -74,61 +74,33 @@ async def test_form(hass): mock_close.assert_awaited_once() -async def test_form_invalid_auth(hass): - """Test we handle invalid auth.""" +@pytest.mark.parametrize( + "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( DOMAIN, context={"source": config_entries.SOURCE_USER} ) with patch( - "homeassistant.components.risco.config_flow.RiscoAPI.login", - side_effect=UnauthorizedError, - ), patch("homeassistant.components.risco.config_flow.RiscoAPI.close") as mock_close: + "homeassistant.components.risco.config_flow.RiscoCloud.login", + side_effect=exception, + ), patch( + "homeassistant.components.risco.config_flow.RiscoCloud.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": "invalid_auth"} 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["errors"] == {"base": "cannot_connect"} - 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() + assert result2["errors"] == {"base": error} async def test_form_already_exists(hass): diff --git a/tests/components/risco/test_sensor.py b/tests/components/risco/test_sensor.py index 24efbabf087..8fb4daf8624 100644 --- a/tests/components/risco/test_sensor.py +++ b/tests/components/risco/test_sensor.py @@ -113,7 +113,7 @@ async def test_cannot_connect(hass): """Test connection error.""" with patch( - "homeassistant.components.risco.RiscoAPI.login", + "homeassistant.components.risco.RiscoCloud.login", side_effect=CannotConnectError, ): config_entry = MockConfigEntry(domain=DOMAIN, data=TEST_CONFIG) @@ -130,7 +130,7 @@ async def test_unauthorized(hass): """Test unauthorized error.""" with patch( - "homeassistant.components.risco.RiscoAPI.login", + "homeassistant.components.risco.RiscoCloud.login", side_effect=UnauthorizedError, ): 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) with patch( - "homeassistant.components.risco.RiscoAPI.site_uuid", + "homeassistant.components.risco.RiscoCloud.site_uuid", new_callable=PropertyMock(return_value=TEST_SITE_UUID), ), patch( "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) with patch( - "homeassistant.components.risco.RiscoAPI.get_events", return_value=[] + "homeassistant.components.risco.RiscoCloud.get_events", return_value=[] ) as events_mock, patch( "homeassistant.components.risco.Store.async_load", return_value={LAST_EVENT_TIMESTAMP_KEY: TEST_EVENTS[0].time}, diff --git a/tests/components/risco/util.py b/tests/components/risco/util.py index 8b918f32c12..3fa81586d27 100644 --- a/tests/components/risco/util.py +++ b/tests/components/risco/util.py @@ -23,18 +23,18 @@ async def setup_risco(hass, events=[], options={}): config_entry.add_to_hass(hass) with patch( - "homeassistant.components.risco.RiscoAPI.login", + "homeassistant.components.risco.RiscoCloud.login", return_value=True, ), patch( - "homeassistant.components.risco.RiscoAPI.site_uuid", + "homeassistant.components.risco.RiscoCloud.site_uuid", new_callable=PropertyMock(return_value=TEST_SITE_UUID), ), patch( - "homeassistant.components.risco.RiscoAPI.site_name", + "homeassistant.components.risco.RiscoCloud.site_name", new_callable=PropertyMock(return_value=TEST_SITE_NAME), ), patch( - "homeassistant.components.risco.RiscoAPI.close" + "homeassistant.components.risco.RiscoCloud.close" ), patch( - "homeassistant.components.risco.RiscoAPI.get_events", + "homeassistant.components.risco.RiscoCloud.get_events", return_value=events, ): await hass.config_entries.async_setup(config_entry.entry_id) @@ -68,7 +68,7 @@ def two_zone_alarm(): "zones", new_callable=PropertyMock(return_value=zone_mocks), ), patch( - "homeassistant.components.risco.RiscoAPI.get_state", + "homeassistant.components.risco.RiscoCloud.get_state", return_value=alarm_mock, ): yield alarm_mock