core/homeassistant/components/elmax/common.py

45 lines
1.4 KiB
Python
Raw Normal View History

"""Elmax integration common classes and utilities."""
from __future__ import annotations
Add local API support to elmax (#94392) * Add support for local (lan) panel integration * Fix merge conflicts * Remove executable flag from non-executable files * Fix tests * Update homeassistant/components/elmax/__init__.py Shorten comment Co-authored-by: Erik Montnemery <erik@montnemery.com> * Fix typehint * Rename DummyPanel into DirectPanel * Update homeassistant/components/elmax/__init__.py Rewording Co-authored-by: Erik Montnemery <erik@montnemery.com> * Update homeassistant/components/elmax/__init__.py Rewording Co-authored-by: Erik Montnemery <erik@montnemery.com> * Refactor option step into menu step * Change requirement statement * Refactor dictionary key entries to use existing constants * Align step names to new constants * Align step names to new constants amd align tests * Align step names to new constants amd align tests * Align step names to new constants * Simplify logic to handle entire entry instead of a portion of the state * Simplify working mode checks * Add data_description dictionary to better explain SSL and FOLLOW_MDSN options * Add support for local (lan) panel integration * Fix merge conflicts * Remove executable flag from non-executable files * Fix tests * Update homeassistant/components/elmax/__init__.py Shorten comment Co-authored-by: Erik Montnemery <erik@montnemery.com> * Fix typehint * Rename DummyPanel into DirectPanel * Update homeassistant/components/elmax/__init__.py Rewording Co-authored-by: Erik Montnemery <erik@montnemery.com> * Update homeassistant/components/elmax/__init__.py Rewording Co-authored-by: Erik Montnemery <erik@montnemery.com> * Refactor option step into menu step * Change requirement statement * Refactor dictionary key entries to use existing constants * Align step names to new constants * Align step names to new constants amd align tests * Align step names to new constants amd align tests * Align step names to new constants * Simplify logic to handle entire entry instead of a portion of the state * Simplify working mode checks * Add data_description dictionary to better explain SSL and FOLLOW_MDSN options * Add newline at end of file * Remove CONF_ELMAX_MODE_DIRECT_FOLLOW_MDNS option * Fix Ruff pre-check --------- Co-authored-by: Erik Montnemery <erik@montnemery.com>
2024-03-04 10:39:13 +00:00
import ssl
from elmax_api.model.panel import PanelEntry
Add local API support to elmax (#94392) * Add support for local (lan) panel integration * Fix merge conflicts * Remove executable flag from non-executable files * Fix tests * Update homeassistant/components/elmax/__init__.py Shorten comment Co-authored-by: Erik Montnemery <erik@montnemery.com> * Fix typehint * Rename DummyPanel into DirectPanel * Update homeassistant/components/elmax/__init__.py Rewording Co-authored-by: Erik Montnemery <erik@montnemery.com> * Update homeassistant/components/elmax/__init__.py Rewording Co-authored-by: Erik Montnemery <erik@montnemery.com> * Refactor option step into menu step * Change requirement statement * Refactor dictionary key entries to use existing constants * Align step names to new constants * Align step names to new constants amd align tests * Align step names to new constants amd align tests * Align step names to new constants * Simplify logic to handle entire entry instead of a portion of the state * Simplify working mode checks * Add data_description dictionary to better explain SSL and FOLLOW_MDSN options * Add support for local (lan) panel integration * Fix merge conflicts * Remove executable flag from non-executable files * Fix tests * Update homeassistant/components/elmax/__init__.py Shorten comment Co-authored-by: Erik Montnemery <erik@montnemery.com> * Fix typehint * Rename DummyPanel into DirectPanel * Update homeassistant/components/elmax/__init__.py Rewording Co-authored-by: Erik Montnemery <erik@montnemery.com> * Update homeassistant/components/elmax/__init__.py Rewording Co-authored-by: Erik Montnemery <erik@montnemery.com> * Refactor option step into menu step * Change requirement statement * Refactor dictionary key entries to use existing constants * Align step names to new constants * Align step names to new constants amd align tests * Align step names to new constants amd align tests * Align step names to new constants * Simplify logic to handle entire entry instead of a portion of the state * Simplify working mode checks * Add data_description dictionary to better explain SSL and FOLLOW_MDSN options * Add newline at end of file * Remove CONF_ELMAX_MODE_DIRECT_FOLLOW_MDNS option * Fix Ruff pre-check --------- Co-authored-by: Erik Montnemery <erik@montnemery.com>
2024-03-04 10:39:13 +00:00
from packaging import version
from .const import ELMAX_LOCAL_API_PATH, MIN_APIV2_SUPPORTED_VERSION
Add local API support to elmax (#94392) * Add support for local (lan) panel integration * Fix merge conflicts * Remove executable flag from non-executable files * Fix tests * Update homeassistant/components/elmax/__init__.py Shorten comment Co-authored-by: Erik Montnemery <erik@montnemery.com> * Fix typehint * Rename DummyPanel into DirectPanel * Update homeassistant/components/elmax/__init__.py Rewording Co-authored-by: Erik Montnemery <erik@montnemery.com> * Update homeassistant/components/elmax/__init__.py Rewording Co-authored-by: Erik Montnemery <erik@montnemery.com> * Refactor option step into menu step * Change requirement statement * Refactor dictionary key entries to use existing constants * Align step names to new constants * Align step names to new constants amd align tests * Align step names to new constants amd align tests * Align step names to new constants * Simplify logic to handle entire entry instead of a portion of the state * Simplify working mode checks * Add data_description dictionary to better explain SSL and FOLLOW_MDSN options * Add support for local (lan) panel integration * Fix merge conflicts * Remove executable flag from non-executable files * Fix tests * Update homeassistant/components/elmax/__init__.py Shorten comment Co-authored-by: Erik Montnemery <erik@montnemery.com> * Fix typehint * Rename DummyPanel into DirectPanel * Update homeassistant/components/elmax/__init__.py Rewording Co-authored-by: Erik Montnemery <erik@montnemery.com> * Update homeassistant/components/elmax/__init__.py Rewording Co-authored-by: Erik Montnemery <erik@montnemery.com> * Refactor option step into menu step * Change requirement statement * Refactor dictionary key entries to use existing constants * Align step names to new constants * Align step names to new constants amd align tests * Align step names to new constants amd align tests * Align step names to new constants * Simplify logic to handle entire entry instead of a portion of the state * Simplify working mode checks * Add data_description dictionary to better explain SSL and FOLLOW_MDSN options * Add newline at end of file * Remove CONF_ELMAX_MODE_DIRECT_FOLLOW_MDNS option * Fix Ruff pre-check --------- Co-authored-by: Erik Montnemery <erik@montnemery.com>
2024-03-04 10:39:13 +00:00
def get_direct_api_url(host: str, port: int, use_ssl: bool) -> str:
"""Return the direct API url given the base URI."""
schema = "https" if use_ssl else "http"
return f"{schema}://{host}:{port}/{ELMAX_LOCAL_API_PATH}"
def build_direct_ssl_context(cadata: str) -> ssl.SSLContext:
"""Create a custom SSL context for direct-api verification."""
context = ssl.SSLContext(protocol=ssl.PROTOCOL_TLS_CLIENT)
context.check_hostname = False
context.verify_mode = ssl.CERT_REQUIRED
context.load_verify_locations(cadata=cadata)
return context
def check_local_version_supported(api_version: str | None) -> bool:
"""Check whether the given API version is supported."""
if api_version is None:
return False
return version.parse(api_version) >= version.parse(MIN_APIV2_SUPPORTED_VERSION)
class DirectPanel(PanelEntry):
"""Helper class for wrapping a directly accessed Elmax Panel."""
def __init__(self, panel_uri):
"""Construct the object."""
super().__init__(panel_uri, True, {})
def get_name_by_user(self, username: str) -> str:
"""Return the panel name."""
return f"Direct Panel {self.hash}"