Handle HTTPError on nuki integration (#80801)

fix(nuki): handle requests errors
This commit is contained in:
Pascal Reeb 2022-10-30 16:36:19 +01:00 committed by GitHub
parent 225be6fc2e
commit 5d282db439
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 21 deletions

View File

@ -8,13 +8,13 @@ from pynuki.bridge import InvalidCredentialsException
from requests.exceptions import RequestException from requests.exceptions import RequestException
import voluptuous as vol import voluptuous as vol
from homeassistant import config_entries, exceptions from homeassistant import config_entries
from homeassistant.components import dhcp from homeassistant.components import dhcp
from homeassistant.const import CONF_HOST, CONF_PORT, CONF_TOKEN from homeassistant.const import CONF_HOST, CONF_PORT, CONF_TOKEN
from homeassistant.data_entry_flow import FlowResult from homeassistant.data_entry_flow import FlowResult
from .const import DEFAULT_PORT, DEFAULT_TIMEOUT, DOMAIN from .const import DEFAULT_PORT, DEFAULT_TIMEOUT, DOMAIN
from .helpers import parse_id from .helpers import CannotConnect, InvalidAuth, parse_id
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -153,11 +153,3 @@ class NukiConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
return self.async_show_form( return self.async_show_form(
step_id="user", data_schema=data_schema, errors=errors step_id="user", data_schema=data_schema, errors=errors
) )
class CannotConnect(exceptions.HomeAssistantError):
"""Error to indicate we cannot connect."""
class InvalidAuth(exceptions.HomeAssistantError):
"""Error to indicate there is invalid auth."""

View File

@ -1,6 +1,15 @@
"""nuki integration helpers.""" """nuki integration helpers."""
from homeassistant import exceptions
def parse_id(hardware_id): def parse_id(hardware_id):
"""Parse Nuki ID.""" """Parse Nuki ID."""
return hex(hardware_id).split("x")[-1].upper() return hex(hardware_id).split("x")[-1].upper()
class CannotConnect(exceptions.HomeAssistantError):
"""Error to indicate we cannot connect."""
class InvalidAuth(exceptions.HomeAssistantError):
"""Error to indicate there is invalid auth."""

View File

@ -6,6 +6,7 @@ from typing import Any
from pynuki import NukiLock, NukiOpener from pynuki import NukiLock, NukiOpener
from pynuki.constants import MODE_OPENER_CONTINUOUS from pynuki.constants import MODE_OPENER_CONTINUOUS
from requests.exceptions import RequestException
import voluptuous as vol import voluptuous as vol
from homeassistant.components.lock import LockEntity, LockEntityFeature from homeassistant.components.lock import LockEntity, LockEntityFeature
@ -26,6 +27,7 @@ from .const import (
DOMAIN as NUKI_DOMAIN, DOMAIN as NUKI_DOMAIN,
ERROR_STATES, ERROR_STATES,
) )
from .helpers import CannotConnect
async def async_setup_entry( async def async_setup_entry(
@ -114,15 +116,24 @@ class NukiLockEntity(NukiDeviceEntity):
def lock(self, **kwargs: Any) -> None: def lock(self, **kwargs: Any) -> None:
"""Lock the device.""" """Lock the device."""
try:
self._nuki_device.lock() self._nuki_device.lock()
except RequestException as err:
raise CannotConnect from err
def unlock(self, **kwargs: Any) -> None: def unlock(self, **kwargs: Any) -> None:
"""Unlock the device.""" """Unlock the device."""
try:
self._nuki_device.unlock() self._nuki_device.unlock()
except RequestException as err:
raise CannotConnect from err
def open(self, **kwargs: Any) -> None: def open(self, **kwargs: Any) -> None:
"""Open the door latch.""" """Open the door latch."""
try:
self._nuki_device.unlatch() self._nuki_device.unlatch()
except RequestException as err:
raise CannotConnect from err
def lock_n_go(self, unlatch: bool) -> None: def lock_n_go(self, unlatch: bool) -> None:
"""Lock and go. """Lock and go.
@ -130,7 +141,10 @@ class NukiLockEntity(NukiDeviceEntity):
This will first unlock the door, then wait for 20 seconds (or another This will first unlock the door, then wait for 20 seconds (or another
amount of time depending on the lock settings) and relock. amount of time depending on the lock settings) and relock.
""" """
try:
self._nuki_device.lock_n_go(unlatch) self._nuki_device.lock_n_go(unlatch)
except RequestException as err:
raise CannotConnect from err
class NukiOpenerEntity(NukiDeviceEntity): class NukiOpenerEntity(NukiDeviceEntity):
@ -148,15 +162,24 @@ class NukiOpenerEntity(NukiDeviceEntity):
def lock(self, **kwargs: Any) -> None: def lock(self, **kwargs: Any) -> None:
"""Disable ring-to-open.""" """Disable ring-to-open."""
try:
self._nuki_device.deactivate_rto() self._nuki_device.deactivate_rto()
except RequestException as err:
raise CannotConnect from err
def unlock(self, **kwargs: Any) -> None: def unlock(self, **kwargs: Any) -> None:
"""Enable ring-to-open.""" """Enable ring-to-open."""
try:
self._nuki_device.activate_rto() self._nuki_device.activate_rto()
except RequestException as err:
raise CannotConnect from err
def open(self, **kwargs: Any) -> None: def open(self, **kwargs: Any) -> None:
"""Buzz open the door.""" """Buzz open the door."""
try:
self._nuki_device.electric_strike_actuation() self._nuki_device.electric_strike_actuation()
except RequestException as err:
raise CannotConnect from err
def lock_n_go(self, unlatch: bool) -> None: def lock_n_go(self, unlatch: bool) -> None:
"""Stub service.""" """Stub service."""
@ -168,7 +191,10 @@ class NukiOpenerEntity(NukiDeviceEntity):
rings the bell. This is similar to ring-to-open, except that it does rings the bell. This is similar to ring-to-open, except that it does
not automatically deactivate not automatically deactivate
""" """
try:
if enable: if enable:
self._nuki_device.activate_continuous_mode() self._nuki_device.activate_continuous_mode()
else: else:
self._nuki_device.deactivate_continuous_mode() self._nuki_device.deactivate_continuous_mode()
except RequestException as err:
raise CannotConnect from err