Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactoring regression fixes #242

Merged
merged 1 commit into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading