mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 05:07:41 +00:00
Handle uncaught exceptions during update in Aladdin_connect (#89889)
* Handle uncaught errors during update * Remove unnecssary patch * Update tests/components/aladdin_connect/test_cover.py Co-authored-by: Franck Nijhof <frenck@frenck.nl> * Update tests/components/aladdin_connect/test_cover.py Co-authored-by: Franck Nijhof <frenck@frenck.nl> * Remove unasserted statement * Blocking is True - one more --------- Co-authored-by: Franck Nijhof <frenck@frenck.nl>
This commit is contained in:
parent
c0387a655c
commit
b399e5c8b7
@ -4,7 +4,7 @@ from __future__ import annotations
|
|||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from AIOAladdinConnect import AladdinConnectClient
|
from AIOAladdinConnect import AladdinConnectClient, session_manager
|
||||||
|
|
||||||
from homeassistant.components.cover import CoverDeviceClass, CoverEntity
|
from homeassistant.components.cover import CoverDeviceClass, CoverEntity
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
@ -46,7 +46,7 @@ class AladdinDevice(CoverEntity):
|
|||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the Aladdin Connect cover."""
|
"""Initialize the Aladdin Connect cover."""
|
||||||
self._acc = acc
|
self._acc = acc
|
||||||
|
self._entry_id = entry.entry_id
|
||||||
self._device_id = device["device_id"]
|
self._device_id = device["device_id"]
|
||||||
self._number = device["door_number"]
|
self._number = device["door_number"]
|
||||||
self._name = device["name"]
|
self._name = device["name"]
|
||||||
@ -85,7 +85,18 @@ class AladdinDevice(CoverEntity):
|
|||||||
|
|
||||||
async def async_update(self) -> None:
|
async def async_update(self) -> None:
|
||||||
"""Update status of cover."""
|
"""Update status of cover."""
|
||||||
await self._acc.get_doors(self._serial)
|
try:
|
||||||
|
await self._acc.get_doors(self._serial)
|
||||||
|
self._attr_available = True
|
||||||
|
|
||||||
|
except session_manager.ConnectionError:
|
||||||
|
self._attr_available = False
|
||||||
|
|
||||||
|
except session_manager.InvalidPasswordError:
|
||||||
|
self._attr_available = False
|
||||||
|
await self.hass.async_create_task(
|
||||||
|
self.hass.config_entries.async_reload(self._entry_id)
|
||||||
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_closed(self) -> bool | None:
|
def is_closed(self) -> bool | None:
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
"""Test the Aladdin Connect Cover."""
|
"""Test the Aladdin Connect Cover."""
|
||||||
from unittest.mock import AsyncMock, MagicMock, patch
|
from unittest.mock import AsyncMock, MagicMock, patch
|
||||||
|
|
||||||
|
from AIOAladdinConnect import session_manager
|
||||||
|
|
||||||
from homeassistant.components.aladdin_connect.const import DOMAIN
|
from homeassistant.components.aladdin_connect.const import DOMAIN
|
||||||
from homeassistant.components.aladdin_connect.cover import SCAN_INTERVAL
|
from homeassistant.components.aladdin_connect.cover import SCAN_INTERVAL
|
||||||
from homeassistant.components.cover import DOMAIN as COVER_DOMAIN
|
from homeassistant.components.cover import DOMAIN as COVER_DOMAIN
|
||||||
@ -13,6 +15,7 @@ from homeassistant.const import (
|
|||||||
STATE_CLOSING,
|
STATE_CLOSING,
|
||||||
STATE_OPEN,
|
STATE_OPEN,
|
||||||
STATE_OPENING,
|
STATE_OPENING,
|
||||||
|
STATE_UNAVAILABLE,
|
||||||
STATE_UNKNOWN,
|
STATE_UNKNOWN,
|
||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
@ -97,8 +100,10 @@ async def test_cover_operation(
|
|||||||
|
|
||||||
assert await async_setup_component(hass, "homeassistant", {})
|
assert await async_setup_component(hass, "homeassistant", {})
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
mock_aladdinconnect_api.async_get_door_status = AsyncMock(return_value=STATE_OPEN)
|
mock_aladdinconnect_api.async_get_door_status = AsyncMock(return_value=STATE_OPEN)
|
||||||
mock_aladdinconnect_api.get_door_status.return_value = STATE_OPEN
|
mock_aladdinconnect_api.get_door_status.return_value = STATE_OPEN
|
||||||
|
|
||||||
with patch(
|
with patch(
|
||||||
"homeassistant.components.aladdin_connect.AladdinConnectClient",
|
"homeassistant.components.aladdin_connect.AladdinConnectClient",
|
||||||
return_value=mock_aladdinconnect_api,
|
return_value=mock_aladdinconnect_api,
|
||||||
@ -116,27 +121,22 @@ async def test_cover_operation(
|
|||||||
{ATTR_ENTITY_ID: "cover.home"},
|
{ATTR_ENTITY_ID: "cover.home"},
|
||||||
blocking=True,
|
blocking=True,
|
||||||
)
|
)
|
||||||
await hass.async_block_till_done()
|
|
||||||
assert hass.states.get("cover.home").state == STATE_OPEN
|
assert hass.states.get("cover.home").state == STATE_OPEN
|
||||||
|
|
||||||
mock_aladdinconnect_api.async_get_door_status = AsyncMock(return_value=STATE_CLOSED)
|
mock_aladdinconnect_api.async_get_door_status = AsyncMock(return_value=STATE_CLOSED)
|
||||||
mock_aladdinconnect_api.get_door_status.return_value = STATE_CLOSED
|
mock_aladdinconnect_api.get_door_status.return_value = STATE_CLOSED
|
||||||
with patch(
|
|
||||||
"homeassistant.components.aladdin_connect.AladdinConnectClient",
|
await hass.services.async_call(
|
||||||
return_value=mock_aladdinconnect_api,
|
COVER_DOMAIN,
|
||||||
):
|
SERVICE_CLOSE_COVER,
|
||||||
await hass.services.async_call(
|
{ATTR_ENTITY_ID: "cover.home"},
|
||||||
COVER_DOMAIN,
|
blocking=True,
|
||||||
SERVICE_CLOSE_COVER,
|
)
|
||||||
{ATTR_ENTITY_ID: "cover.home"},
|
async_fire_time_changed(
|
||||||
blocking=True,
|
hass,
|
||||||
)
|
utcnow() + SCAN_INTERVAL,
|
||||||
await hass.async_block_till_done()
|
)
|
||||||
async_fire_time_changed(
|
await hass.async_block_till_done()
|
||||||
hass,
|
|
||||||
utcnow() + SCAN_INTERVAL,
|
|
||||||
)
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
||||||
assert hass.states.get("cover.home").state == STATE_CLOSED
|
assert hass.states.get("cover.home").state == STATE_CLOSED
|
||||||
|
|
||||||
@ -145,15 +145,11 @@ async def test_cover_operation(
|
|||||||
)
|
)
|
||||||
mock_aladdinconnect_api.get_door_status.return_value = STATE_CLOSING
|
mock_aladdinconnect_api.get_door_status.return_value = STATE_CLOSING
|
||||||
|
|
||||||
with patch(
|
async_fire_time_changed(
|
||||||
"homeassistant.components.aladdin_connect.AladdinConnectClient",
|
hass,
|
||||||
return_value=mock_aladdinconnect_api,
|
utcnow() + SCAN_INTERVAL,
|
||||||
):
|
)
|
||||||
async_fire_time_changed(
|
await hass.async_block_till_done()
|
||||||
hass,
|
|
||||||
utcnow() + SCAN_INTERVAL,
|
|
||||||
)
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
assert hass.states.get("cover.home").state == STATE_CLOSING
|
assert hass.states.get("cover.home").state == STATE_CLOSING
|
||||||
|
|
||||||
mock_aladdinconnect_api.async_get_door_status = AsyncMock(
|
mock_aladdinconnect_api.async_get_door_status = AsyncMock(
|
||||||
@ -161,34 +157,47 @@ async def test_cover_operation(
|
|||||||
)
|
)
|
||||||
mock_aladdinconnect_api.get_door_status.return_value = STATE_OPENING
|
mock_aladdinconnect_api.get_door_status.return_value = STATE_OPENING
|
||||||
|
|
||||||
with patch(
|
async_fire_time_changed(
|
||||||
"homeassistant.components.aladdin_connect.AladdinConnectClient",
|
hass,
|
||||||
return_value=mock_aladdinconnect_api,
|
utcnow() + SCAN_INTERVAL,
|
||||||
):
|
)
|
||||||
async_fire_time_changed(
|
await hass.async_block_till_done()
|
||||||
hass,
|
|
||||||
utcnow() + SCAN_INTERVAL,
|
|
||||||
)
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
assert hass.states.get("cover.home").state == STATE_OPENING
|
assert hass.states.get("cover.home").state == STATE_OPENING
|
||||||
|
|
||||||
mock_aladdinconnect_api.async_get_door_status = AsyncMock(return_value=None)
|
mock_aladdinconnect_api.async_get_door_status = AsyncMock(return_value=None)
|
||||||
mock_aladdinconnect_api.get_door_status.return_value = None
|
mock_aladdinconnect_api.get_door_status.return_value = None
|
||||||
with patch(
|
|
||||||
"homeassistant.components.aladdin_connect.AladdinConnectClient",
|
await hass.services.async_call(
|
||||||
return_value=mock_aladdinconnect_api,
|
COVER_DOMAIN,
|
||||||
):
|
SERVICE_CLOSE_COVER,
|
||||||
await hass.services.async_call(
|
{ATTR_ENTITY_ID: "cover.home"},
|
||||||
COVER_DOMAIN,
|
blocking=True,
|
||||||
SERVICE_CLOSE_COVER,
|
)
|
||||||
{ATTR_ENTITY_ID: "cover.home"},
|
async_fire_time_changed(
|
||||||
blocking=True,
|
hass,
|
||||||
)
|
utcnow() + SCAN_INTERVAL,
|
||||||
await hass.async_block_till_done()
|
)
|
||||||
async_fire_time_changed(
|
await hass.async_block_till_done()
|
||||||
hass,
|
|
||||||
utcnow() + SCAN_INTERVAL,
|
|
||||||
)
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
||||||
assert hass.states.get("cover.home").state == STATE_UNKNOWN
|
assert hass.states.get("cover.home").state == STATE_UNKNOWN
|
||||||
|
|
||||||
|
mock_aladdinconnect_api.get_doors.side_effect = session_manager.ConnectionError
|
||||||
|
|
||||||
|
async_fire_time_changed(
|
||||||
|
hass,
|
||||||
|
utcnow() + SCAN_INTERVAL,
|
||||||
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
assert hass.states.get("cover.home").state == STATE_UNAVAILABLE
|
||||||
|
|
||||||
|
mock_aladdinconnect_api.get_doors.side_effect = session_manager.InvalidPasswordError
|
||||||
|
mock_aladdinconnect_api.login.return_value = False
|
||||||
|
mock_aladdinconnect_api.login.side_effect = session_manager.InvalidPasswordError
|
||||||
|
|
||||||
|
async_fire_time_changed(
|
||||||
|
hass,
|
||||||
|
utcnow() + SCAN_INTERVAL,
|
||||||
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert hass.states.get("cover.home").state == STATE_UNAVAILABLE
|
||||||
|
Loading…
x
Reference in New Issue
Block a user