Update gogogate2 to be async (#42066)

This commit is contained in:
J. Nick Koston 2021-01-17 11:38:30 -06:00 committed by GitHub
parent a50fba4e0b
commit ae3d038baa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 29 additions and 33 deletions

View file

@ -70,7 +70,7 @@ def get_data_update_coordinator(
async def async_update_data(): async def async_update_data():
try: try:
return await hass.async_add_executor_job(api.info) return await api.async_info()
except Exception as exception: except Exception as exception:
raise UpdateFailed( raise UpdateFailed(
f"Error communicating with API: {exception}" f"Error communicating with API: {exception}"

View file

@ -60,9 +60,7 @@ class Gogogate2FlowHandler(ConfigFlow, domain=DOMAIN):
if user_input: if user_input:
api = get_api(user_input) api = get_api(user_input)
try: try:
data: AbstractInfoResponse = await self.hass.async_add_executor_job( data: AbstractInfoResponse = await api.async_info()
api.info
)
data_dict = dataclasses.asdict(data) data_dict = dataclasses.asdict(data)
title = data_dict.get( title = data_dict.get(
"gogogatename", data_dict.get("ismartgatename", "Cover") "gogogatename", data_dict.get("ismartgatename", "Cover")

View file

@ -129,15 +129,11 @@ class DeviceCover(CoordinatorEntity, CoverEntity):
async def async_open_cover(self, **kwargs): async def async_open_cover(self, **kwargs):
"""Open the door.""" """Open the door."""
await self.hass.async_add_executor_job( await self._api.async_open_door(self._get_door().door_id)
self._api.open_door, self._get_door().door_id
)
async def async_close_cover(self, **kwargs): async def async_close_cover(self, **kwargs):
"""Close the door.""" """Close the door."""
await self.hass.async_add_executor_job( await self._api.async_close_door(self._get_door().door_id)
self._api.close_door, self._get_door().door_id
)
@property @property
def state_attributes(self): def state_attributes(self):

View file

@ -3,7 +3,7 @@
"name": "Gogogate2 and iSmartGate", "name": "Gogogate2 and iSmartGate",
"config_flow": true, "config_flow": true,
"documentation": "https://www.home-assistant.io/integrations/gogogate2", "documentation": "https://www.home-assistant.io/integrations/gogogate2",
"requirements": ["gogogate2-api==2.0.3"], "requirements": ["gogogate2-api==3.0.0"],
"codeowners": ["@vangorra"], "codeowners": ["@vangorra"],
"homekit": { "homekit": {
"models": [ "models": [

View file

@ -669,7 +669,7 @@ gntp==1.0.3
goalzero==0.1.4 goalzero==0.1.4
# homeassistant.components.gogogate2 # homeassistant.components.gogogate2
gogogate2-api==2.0.3 gogogate2-api==3.0.0
# homeassistant.components.google # homeassistant.components.google
google-api-python-client==1.6.4 google-api-python-client==1.6.4

View file

@ -346,7 +346,7 @@ glances_api==0.2.0
goalzero==0.1.4 goalzero==0.1.4
# homeassistant.components.gogogate2 # homeassistant.components.gogogate2
gogogate2-api==2.0.3 gogogate2-api==3.0.0
# homeassistant.components.google # homeassistant.components.google
google-api-python-client==1.6.4 google-api-python-client==1.6.4

View file

@ -37,7 +37,9 @@ async def test_auth_fail(
gogogate2api_mock.return_value = api gogogate2api_mock.return_value = api
api.reset_mock() 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( result = await hass.config_entries.flow.async_init(
"gogogate2", context={"source": SOURCE_USER} "gogogate2", context={"source": SOURCE_USER}
) )
@ -57,7 +59,7 @@ async def test_auth_fail(
} }
api.reset_mock() 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( result = await hass.config_entries.flow.async_init(
"gogogate2", context={"source": SOURCE_USER} "gogogate2", context={"source": SOURCE_USER}
) )

View file

@ -183,7 +183,7 @@ def _mocked_ismartgate_closed_door_response():
async def test_import_fail(gogogate2api_mock, hass: HomeAssistant) -> None: async def test_import_fail(gogogate2api_mock, hass: HomeAssistant) -> None:
"""Test the failure to import.""" """Test the failure to import."""
api = MagicMock(spec=GogoGate2Api) api = MagicMock(spec=GogoGate2Api)
api.info.side_effect = ApiError(22, "Error") api.async_info.side_effect = ApiError(22, "Error")
gogogate2api_mock.return_value = api gogogate2api_mock.return_value = api
hass_config = { hass_config = {
@ -216,11 +216,11 @@ async def test_import(
) -> None: ) -> None:
"""Test importing of file based config.""" """Test importing of file based config."""
api0 = MagicMock(spec=GogoGate2Api) 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 gogogate2api_mock.return_value = api0
api1 = MagicMock(spec=ISmartGateApi) 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 ismartgateapi_mock.return_value = api1
hass_config = { hass_config = {
@ -320,8 +320,8 @@ async def test_open_close_update(gogogate2api_mock, hass: HomeAssistant) -> None
) )
api = MagicMock(GogoGate2Api) api = MagicMock(GogoGate2Api)
api.activate.return_value = GogoGate2ActivateResponse(result=True) api.async_activate.return_value = GogoGate2ActivateResponse(result=True)
api.info.return_value = info_response(DoorStatus.OPENED) api.async_info.return_value = info_response(DoorStatus.OPENED)
gogogate2api_mock.return_value = api gogogate2api_mock.return_value = api
config_entry = MockConfigEntry( config_entry = MockConfigEntry(
@ -340,7 +340,7 @@ async def test_open_close_update(gogogate2api_mock, hass: HomeAssistant) -> None
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.states.get("cover.door1").state == STATE_OPEN 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( await hass.services.async_call(
COVER_DOMAIN, COVER_DOMAIN,
"close_cover", "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)) async_fire_time_changed(hass, utcnow() + timedelta(hours=2))
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.states.get("cover.door1").state == STATE_CLOSED 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( await hass.services.async_call(
COVER_DOMAIN, COVER_DOMAIN,
"open_cover", "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)) async_fire_time_changed(hass, utcnow() + timedelta(hours=2))
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.states.get("cover.door1").state == STATE_OPEN 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)) async_fire_time_changed(hass, utcnow() + timedelta(hours=2))
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.states.get("cover.door1").state == STATE_UNKNOWN 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() closed_door_response = _mocked_ismartgate_closed_door_response()
api = MagicMock(ISmartGateApi) api = MagicMock(ISmartGateApi)
api.info.return_value = closed_door_response api.async_info.return_value = closed_door_response
ismartgateapi_mock.return_value = api ismartgateapi_mock.return_value = api
config_entry = MockConfigEntry( config_entry = MockConfigEntry(
@ -405,14 +405,14 @@ async def test_availability(ismartgateapi_mock, hass: HomeAssistant) -> None:
== DEVICE_CLASS_GATE == 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)) async_fire_time_changed(hass, utcnow() + timedelta(hours=2))
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.states.get("cover.door1").state == STATE_UNAVAILABLE assert hass.states.get("cover.door1").state == STATE_UNAVAILABLE
api.info.side_effect = None api.async_info.side_effect = None
api.info.return_value = closed_door_response api.async_info.return_value = closed_door_response
async_fire_time_changed(hass, utcnow() + timedelta(hours=2)) async_fire_time_changed(hass, utcnow() + timedelta(hours=2))
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.states.get("cover.door1").state == STATE_CLOSED 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() closed_door_response = _mocked_ismartgate_closed_door_response()
api = MagicMock(ISmartGateApi) api = MagicMock(ISmartGateApi)
api.info.return_value = closed_door_response api.async_info.return_value = closed_door_response
ismartgateapi_mock.return_value = api ismartgateapi_mock.return_value = api
config_entry = MockConfigEntry( 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() closed_door_response = _mocked_gogogate_open_door_response()
api = MagicMock(GogoGate2Api) api = MagicMock(GogoGate2Api)
api.info.return_value = closed_door_response api.async_info.return_value = closed_door_response
gogogate2api_mock.return_value = api gogogate2api_mock.return_value = api
config_entry = MockConfigEntry( config_entry = MockConfigEntry(

View file

@ -25,7 +25,7 @@ async def test_config_update(gogogate2api_mock, hass: HomeAssistant) -> None:
"""Test config setup where the config is updated.""" """Test config setup where the config is updated."""
api = MagicMock(GogoGate2Api) api = MagicMock(GogoGate2Api)
api.info.side_effect = Exception("Error") api.async_info.side_effect = Exception("Error")
gogogate2api_mock.return_value = api gogogate2api_mock.return_value = api
config_entry = MockConfigEntry( 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: async def test_config_no_update(ismartgateapi_mock, hass: HomeAssistant) -> None:
"""Test config setup where the data is not updated.""" """Test config setup where the data is not updated."""
api = MagicMock(GogoGate2Api) api = MagicMock(GogoGate2Api)
api.info.side_effect = Exception("Error") api.async_info.side_effect = Exception("Error")
ismartgateapi_mock.return_value = api ismartgateapi_mock.return_value = api
config_entry = MockConfigEntry( config_entry = MockConfigEntry(