Skip to content

Commit

Permalink
refactoring regression fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
maciej-or committed Nov 10, 2024
1 parent 8475c28 commit 4009133
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
5 changes: 3 additions & 2 deletions custom_components/hikvision_next/diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from homeassistant.helpers.device_registry import DeviceEntry

from . import HikvisionConfigEntry
from .isapi import ISAPIForbiddenError, ISAPIUnauthorizedError
from .isapi.const import GET, STREAM_TYPE


Expand Down Expand Up @@ -114,9 +115,9 @@ async def get_isapi_data(isapi, endpoint: str) -> dict:
try:
response = await isapi.request(GET, endpoint)
entry["response"] = anonymise_data(response)
except HTTPStatusError as ex:
except (HTTPStatusError, ISAPIUnauthorizedError, ISAPIForbiddenError) as ex:
entry["status_code"] = ex.response.status_code
except Exception as ex:
except Exception as ex: # noqa: BLE001
entry["error"] = ex
return entry

Expand Down
16 changes: 9 additions & 7 deletions custom_components/hikvision_next/isapi/isapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -723,10 +723,10 @@ async def request(
except HTTPStatusError as ex:
_LOGGER.info("--- [%s] %s\n%s", method, full_url, ex)
if ex.response.status_code == HTTPStatus.UNAUTHORIZED:
raise ISAPIUnauthorizedError(full_url)
raise ISAPIUnauthorizedError(ex) from ex
if ex.response.status_code == HTTPStatus.FORBIDDEN and not self.pending_initialization:
raise ISAPIForbiddenError(full_url)
elif self.pending_initialization:
raise ISAPIForbiddenError(ex) from ex
if self.pending_initialization:
# supress http errors during initialization
return {}
raise
Expand Down Expand Up @@ -762,14 +762,16 @@ def __init__(self, event: EventInfo, mutex_issues: []) -> None:
class ISAPIUnauthorizedError(Exception):
"""HTTP Error 401."""

def __init__(self, url) -> None:
def __init__(self, ex: HTTPStatusError, *args) -> None:
"""Initialize exception."""
self.message = f"Unauthorized request {url}, check username and password."
self.message = f"Unauthorized request {ex.request.url}, check username and password."
self.response = ex.response


class ISAPIForbiddenError(Exception):
"""HTTP Error 403."""

def __init__(self, url) -> None:
def __init__(self, ex: HTTPStatusError, *args) -> None:
"""Initialize exception."""
self.message = f"Forbidden request {url}, check user permissions."
self.message = f"Forbidden request {ex.request.url}, check user permissions."
self.response = ex.response
4 changes: 2 additions & 2 deletions custom_components/hikvision_next/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,15 @@ def is_on(self) -> bool | None:
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn on."""
try:
await self.coordinator.device.set_port_state(self._port_no, True)
await self.coordinator.device.set_output_port_state(self._port_no, True)
except Exception as ex:
raise ex
finally:
await self.coordinator.async_request_refresh()

async def async_turn_off(self, **kwargs: Any) -> None:
try:
await self.coordinator.device.set_port_state(self._port_no, False)
await self.coordinator.device.set_output_port_state(self._port_no, False)
except Exception as ex:
raise ex
finally:
Expand Down

0 comments on commit 4009133

Please sign in to comment.