From ae3d038baa54200744b34770a347a7feb9f191dd Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 17 Jan 2021 11:38:30 -0600 Subject: [PATCH] Update gogogate2 to be async (#42066) --- homeassistant/components/gogogate2/common.py | 2 +- .../components/gogogate2/config_flow.py | 4 +-- homeassistant/components/gogogate2/cover.py | 8 ++--- .../components/gogogate2/manifest.json | 2 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- .../components/gogogate2/test_config_flow.py | 6 ++-- tests/components/gogogate2/test_cover.py | 32 +++++++++---------- tests/components/gogogate2/test_init.py | 4 +-- 9 files changed, 29 insertions(+), 33 deletions(-) diff --git a/homeassistant/components/gogogate2/common.py b/homeassistant/components/gogogate2/common.py index d17dd7033fe..2817c351013 100644 --- a/homeassistant/components/gogogate2/common.py +++ b/homeassistant/components/gogogate2/common.py @@ -70,7 +70,7 @@ def get_data_update_coordinator( async def async_update_data(): try: - return await hass.async_add_executor_job(api.info) + return await api.async_info() except Exception as exception: raise UpdateFailed( f"Error communicating with API: {exception}" diff --git a/homeassistant/components/gogogate2/config_flow.py b/homeassistant/components/gogogate2/config_flow.py index 5779ccaaa65..0c3f1b3653c 100644 --- a/homeassistant/components/gogogate2/config_flow.py +++ b/homeassistant/components/gogogate2/config_flow.py @@ -60,9 +60,7 @@ class Gogogate2FlowHandler(ConfigFlow, domain=DOMAIN): if user_input: api = get_api(user_input) try: - data: AbstractInfoResponse = await self.hass.async_add_executor_job( - api.info - ) + data: AbstractInfoResponse = await api.async_info() data_dict = dataclasses.asdict(data) title = data_dict.get( "gogogatename", data_dict.get("ismartgatename", "Cover") diff --git a/homeassistant/components/gogogate2/cover.py b/homeassistant/components/gogogate2/cover.py index 08520ad46ee..8b83073d0c8 100644 --- a/homeassistant/components/gogogate2/cover.py +++ b/homeassistant/components/gogogate2/cover.py @@ -129,15 +129,11 @@ class DeviceCover(CoordinatorEntity, CoverEntity): async def async_open_cover(self, **kwargs): """Open the door.""" - await self.hass.async_add_executor_job( - self._api.open_door, self._get_door().door_id - ) + await self._api.async_open_door(self._get_door().door_id) async def async_close_cover(self, **kwargs): """Close the door.""" - await self.hass.async_add_executor_job( - self._api.close_door, self._get_door().door_id - ) + await self._api.async_close_door(self._get_door().door_id) @property def state_attributes(self): diff --git a/homeassistant/components/gogogate2/manifest.json b/homeassistant/components/gogogate2/manifest.json index 893294da25e..b21eeace466 100644 --- a/homeassistant/components/gogogate2/manifest.json +++ b/homeassistant/components/gogogate2/manifest.json @@ -3,7 +3,7 @@ "name": "Gogogate2 and iSmartGate", "config_flow": true, "documentation": "https://www.home-assistant.io/integrations/gogogate2", - "requirements": ["gogogate2-api==2.0.3"], + "requirements": ["gogogate2-api==3.0.0"], "codeowners": ["@vangorra"], "homekit": { "models": [ diff --git a/requirements_all.txt b/requirements_all.txt index a3caeb9ee7a..130f2d072f8 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -669,7 +669,7 @@ gntp==1.0.3 goalzero==0.1.4 # homeassistant.components.gogogate2 -gogogate2-api==2.0.3 +gogogate2-api==3.0.0 # homeassistant.components.google google-api-python-client==1.6.4 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 8eef334761c..5d58e282d55 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -346,7 +346,7 @@ glances_api==0.2.0 goalzero==0.1.4 # homeassistant.components.gogogate2 -gogogate2-api==2.0.3 +gogogate2-api==3.0.0 # homeassistant.components.google google-api-python-client==1.6.4 diff --git a/tests/components/gogogate2/test_config_flow.py b/tests/components/gogogate2/test_config_flow.py index ba8a119553c..2d19ee70609 100644 --- a/tests/components/gogogate2/test_config_flow.py +++ b/tests/components/gogogate2/test_config_flow.py @@ -37,7 +37,9 @@ async def test_auth_fail( gogogate2api_mock.return_value = api api.reset_mock() - api.info.side_effect = ApiError(GogoGate2ApiErrorCode.CREDENTIALS_INCORRECT, "blah") + api.async_info.side_effect = ApiError( + GogoGate2ApiErrorCode.CREDENTIALS_INCORRECT, "blah" + ) result = await hass.config_entries.flow.async_init( "gogogate2", context={"source": SOURCE_USER} ) @@ -57,7 +59,7 @@ async def test_auth_fail( } api.reset_mock() - api.info.side_effect = Exception("Generic connection error.") + api.async_info.side_effect = Exception("Generic connection error.") result = await hass.config_entries.flow.async_init( "gogogate2", context={"source": SOURCE_USER} ) diff --git a/tests/components/gogogate2/test_cover.py b/tests/components/gogogate2/test_cover.py index 8f58224bb99..31810cb67d0 100644 --- a/tests/components/gogogate2/test_cover.py +++ b/tests/components/gogogate2/test_cover.py @@ -183,7 +183,7 @@ def _mocked_ismartgate_closed_door_response(): async def test_import_fail(gogogate2api_mock, hass: HomeAssistant) -> None: """Test the failure to import.""" api = MagicMock(spec=GogoGate2Api) - api.info.side_effect = ApiError(22, "Error") + api.async_info.side_effect = ApiError(22, "Error") gogogate2api_mock.return_value = api hass_config = { @@ -216,11 +216,11 @@ async def test_import( ) -> None: """Test importing of file based config.""" api0 = MagicMock(spec=GogoGate2Api) - api0.info.return_value = _mocked_gogogate_open_door_response() + api0.async_info.return_value = _mocked_gogogate_open_door_response() gogogate2api_mock.return_value = api0 api1 = MagicMock(spec=ISmartGateApi) - api1.info.return_value = _mocked_ismartgate_closed_door_response() + api1.async_info.return_value = _mocked_ismartgate_closed_door_response() ismartgateapi_mock.return_value = api1 hass_config = { @@ -320,8 +320,8 @@ async def test_open_close_update(gogogate2api_mock, hass: HomeAssistant) -> None ) api = MagicMock(GogoGate2Api) - api.activate.return_value = GogoGate2ActivateResponse(result=True) - api.info.return_value = info_response(DoorStatus.OPENED) + api.async_activate.return_value = GogoGate2ActivateResponse(result=True) + api.async_info.return_value = info_response(DoorStatus.OPENED) gogogate2api_mock.return_value = api config_entry = MockConfigEntry( @@ -340,7 +340,7 @@ async def test_open_close_update(gogogate2api_mock, hass: HomeAssistant) -> None await hass.async_block_till_done() assert hass.states.get("cover.door1").state == STATE_OPEN - api.info.return_value = info_response(DoorStatus.CLOSED) + api.async_info.return_value = info_response(DoorStatus.CLOSED) await hass.services.async_call( COVER_DOMAIN, "close_cover", @@ -349,9 +349,9 @@ async def test_open_close_update(gogogate2api_mock, hass: HomeAssistant) -> None async_fire_time_changed(hass, utcnow() + timedelta(hours=2)) await hass.async_block_till_done() assert hass.states.get("cover.door1").state == STATE_CLOSED - api.close_door.assert_called_with(1) + api.async_close_door.assert_called_with(1) - api.info.return_value = info_response(DoorStatus.OPENED) + api.async_info.return_value = info_response(DoorStatus.OPENED) await hass.services.async_call( COVER_DOMAIN, "open_cover", @@ -360,9 +360,9 @@ async def test_open_close_update(gogogate2api_mock, hass: HomeAssistant) -> None async_fire_time_changed(hass, utcnow() + timedelta(hours=2)) await hass.async_block_till_done() assert hass.states.get("cover.door1").state == STATE_OPEN - api.open_door.assert_called_with(1) + api.async_open_door.assert_called_with(1) - api.info.return_value = info_response(DoorStatus.UNDEFINED) + api.async_info.return_value = info_response(DoorStatus.UNDEFINED) async_fire_time_changed(hass, utcnow() + timedelta(hours=2)) await hass.async_block_till_done() assert hass.states.get("cover.door1").state == STATE_UNKNOWN @@ -377,7 +377,7 @@ async def test_availability(ismartgateapi_mock, hass: HomeAssistant) -> None: closed_door_response = _mocked_ismartgate_closed_door_response() api = MagicMock(ISmartGateApi) - api.info.return_value = closed_door_response + api.async_info.return_value = closed_door_response ismartgateapi_mock.return_value = api config_entry = MockConfigEntry( @@ -405,14 +405,14 @@ async def test_availability(ismartgateapi_mock, hass: HomeAssistant) -> None: == DEVICE_CLASS_GATE ) - api.info.side_effect = Exception("Error") + api.async_info.side_effect = Exception("Error") async_fire_time_changed(hass, utcnow() + timedelta(hours=2)) await hass.async_block_till_done() assert hass.states.get("cover.door1").state == STATE_UNAVAILABLE - api.info.side_effect = None - api.info.return_value = closed_door_response + api.async_info.side_effect = None + api.async_info.return_value = closed_door_response async_fire_time_changed(hass, utcnow() + timedelta(hours=2)) await hass.async_block_till_done() assert hass.states.get("cover.door1").state == STATE_CLOSED @@ -426,7 +426,7 @@ async def test_device_info_ismartgate(ismartgateapi_mock, hass: HomeAssistant) - closed_door_response = _mocked_ismartgate_closed_door_response() api = MagicMock(ISmartGateApi) - api.info.return_value = closed_door_response + api.async_info.return_value = closed_door_response ismartgateapi_mock.return_value = api config_entry = MockConfigEntry( @@ -461,7 +461,7 @@ async def test_device_info_gogogate2(gogogate2api_mock, hass: HomeAssistant) -> closed_door_response = _mocked_gogogate_open_door_response() api = MagicMock(GogoGate2Api) - api.info.return_value = closed_door_response + api.async_info.return_value = closed_door_response gogogate2api_mock.return_value = api config_entry = MockConfigEntry( diff --git a/tests/components/gogogate2/test_init.py b/tests/components/gogogate2/test_init.py index d76108a9221..f51f08b2319 100644 --- a/tests/components/gogogate2/test_init.py +++ b/tests/components/gogogate2/test_init.py @@ -25,7 +25,7 @@ async def test_config_update(gogogate2api_mock, hass: HomeAssistant) -> None: """Test config setup where the config is updated.""" api = MagicMock(GogoGate2Api) - api.info.side_effect = Exception("Error") + api.async_info.side_effect = Exception("Error") gogogate2api_mock.return_value = api config_entry = MockConfigEntry( @@ -53,7 +53,7 @@ async def test_config_update(gogogate2api_mock, hass: HomeAssistant) -> None: async def test_config_no_update(ismartgateapi_mock, hass: HomeAssistant) -> None: """Test config setup where the data is not updated.""" api = MagicMock(GogoGate2Api) - api.info.side_effect = Exception("Error") + api.async_info.side_effect = Exception("Error") ismartgateapi_mock.return_value = api config_entry = MockConfigEntry(