Home Assistant Unofficial Reference 2024.12.1
common.py
Go to the documentation of this file.
1 """Elmax integration common classes and utilities."""
2 
3 from __future__ import annotations
4 
5 import ssl
6 
7 from elmax_api.model.panel import PanelEntry
8 from packaging import version
9 
10 from .const import ELMAX_LOCAL_API_PATH, MIN_APIV2_SUPPORTED_VERSION
11 
12 
13 def get_direct_api_url(host: str, port: int, use_ssl: bool) -> str:
14  """Return the direct API url given the base URI."""
15  schema = "https" if use_ssl else "http"
16  return f"{schema}://{host}:{port}/{ELMAX_LOCAL_API_PATH}"
17 
18 
19 def build_direct_ssl_context(cadata: str) -> ssl.SSLContext:
20  """Create a custom SSL context for direct-api verification."""
21  context = ssl.SSLContext(protocol=ssl.PROTOCOL_TLS_CLIENT)
22  context.check_hostname = False
23  context.verify_mode = ssl.CERT_REQUIRED
24  context.load_verify_locations(cadata=cadata)
25  return context
26 
27 
28 def check_local_version_supported(api_version: str | None) -> bool:
29  """Check whether the given API version is supported."""
30  if api_version is None:
31  return False
32  return version.parse(api_version) >= version.parse(MIN_APIV2_SUPPORTED_VERSION)
33 
34 
35 class DirectPanel(PanelEntry):
36  """Helper class for wrapping a directly accessed Elmax Panel."""
37 
38  def __init__(self, panel_uri) -> None:
39  """Construct the object."""
40  super().__init__(panel_uri, True, {})
41 
42  def get_name_by_user(self, username: str) -> str:
43  """Return the panel name."""
44  return f"Direct Panel {self.hash}"
str get_name_by_user(self, str username)
Definition: common.py:42
bool check_local_version_supported(str|None api_version)
Definition: common.py:28
ssl.SSLContext build_direct_ssl_context(str cadata)
Definition: common.py:19
str get_direct_api_url(str host, int port, bool use_ssl)
Definition: common.py:13