Skip to content

Commit

Permalink
adding support for custom ports
Browse files Browse the repository at this point in the history
  • Loading branch information
imhotep committed Nov 3, 2023
1 parent 598e49e commit 0cc98f1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
16 changes: 13 additions & 3 deletions custom_components/unifi_access/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from requests import request
from requests.exceptions import SSLError
from requests.exceptions import ConnectionError as ConnError
import logging

from homeassistant.helpers.update_coordinator import (
Expand Down Expand Up @@ -41,10 +42,16 @@ def __init__(self, host: str, verify_ssl: bool = False) -> None:
_LOGGER.warning(f"SSL Verification disabled for {host}")
urllib3.disable_warnings()

hostname = (
urlparse(host).hostname if urlparse(host).hostname else host.split(":")[0]
host_parts = host.split(":")
parsed_host = urlparse(host)

hostname = parsed_host.hostname if parsed_host.hostname else host_parts[0]
port = (
parsed_host.port
if parsed_host.port
else (host_parts[1] if len(host_parts) > 1 else UNIFI_ACCESS_API_PORT)
)
self.host = f"https://{hostname}:{UNIFI_ACCESS_API_PORT}"
self.host = f"https://{hostname}:{port}"
self._api_token = None
self._headers = {
"Accept": "application/json",
Expand Down Expand Up @@ -97,6 +104,9 @@ def authenticate(self, api_token: str) -> str:
except SSLError:
_LOGGER.error(f"Error validating SSL Certificate for {self.host}.")
return "ssl_error"
except ConnError:
_LOGGER.error(f"Cannot connect to {self.host}.")
return "cannot_connect"

return "ok"

Expand Down
2 changes: 2 additions & 0 deletions custom_components/unifi_access/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> dict[str,
)

match auth_response:
case "cannot_connect":
raise CannotConnect
case "api_error":
raise CannotConnect
case "api_auth_error":
Expand Down

0 comments on commit 0cc98f1

Please sign in to comment.