Move elmax base entity to separate module (#126481)

This commit is contained in:
epenet 2024-09-23 08:59:38 +02:00 committed by GitHub
parent 5a52e4c71d
commit a9b215357f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 46 additions and 40 deletions

View File

@ -25,9 +25,9 @@ from homeassistant.exceptions import HomeAssistantError, InvalidStateError
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType from homeassistant.helpers.typing import StateType
from .common import ElmaxEntity
from .const import DOMAIN from .const import DOMAIN
from .coordinator import ElmaxCoordinator from .coordinator import ElmaxCoordinator
from .entity import ElmaxEntity
async def async_setup_entry( async def async_setup_entry(

View File

@ -12,9 +12,9 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .common import ElmaxEntity
from .const import DOMAIN from .const import DOMAIN
from .coordinator import ElmaxCoordinator from .coordinator import ElmaxCoordinator
from .entity import ElmaxEntity
async def async_setup_entry( async def async_setup_entry(

View File

@ -4,15 +4,10 @@ from __future__ import annotations
import ssl import ssl
from elmax_api.model.endpoint import DeviceEndpoint
from elmax_api.model.panel import PanelEntry from elmax_api.model.panel import PanelEntry
from packaging import version from packaging import version
from homeassistant.helpers.device_registry import DeviceInfo from .const import ELMAX_LOCAL_API_PATH, MIN_APIV2_SUPPORTED_VERSION
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import DOMAIN, ELMAX_LOCAL_API_PATH, MIN_APIV2_SUPPORTED_VERSION
from .coordinator import ElmaxCoordinator
def get_direct_api_url(host: str, port: int, use_ssl: bool) -> str: def get_direct_api_url(host: str, port: int, use_ssl: bool) -> str:
@ -47,33 +42,3 @@ class DirectPanel(PanelEntry):
def get_name_by_user(self, username: str) -> str: def get_name_by_user(self, username: str) -> str:
"""Return the panel name.""" """Return the panel name."""
return f"Direct Panel {self.hash}" return f"Direct Panel {self.hash}"
class ElmaxEntity(CoordinatorEntity[ElmaxCoordinator]):
"""Wrapper for Elmax entities."""
def __init__(
self,
elmax_device: DeviceEndpoint,
panel_version: str,
coordinator: ElmaxCoordinator,
) -> None:
"""Construct the object."""
super().__init__(coordinator=coordinator)
self._device = elmax_device
self._attr_unique_id = elmax_device.endpoint_id
self._attr_name = elmax_device.name
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, coordinator.panel_entry.hash)},
name=coordinator.panel_entry.get_name_by_user(
coordinator.http_client.get_authenticated_username()
),
manufacturer="Elmax",
model=panel_version,
sw_version=panel_version,
)
@property
def available(self) -> bool:
"""Return if entity is available."""
return super().available and self.coordinator.panel_entry.online

View File

@ -13,9 +13,9 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .common import ElmaxEntity
from .const import DOMAIN from .const import DOMAIN
from .coordinator import ElmaxCoordinator from .coordinator import ElmaxCoordinator
from .entity import ElmaxEntity
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)

View File

@ -0,0 +1,41 @@
"""Elmax integration common classes and utilities."""
from __future__ import annotations
from elmax_api.model.endpoint import DeviceEndpoint
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import DOMAIN
from .coordinator import ElmaxCoordinator
class ElmaxEntity(CoordinatorEntity[ElmaxCoordinator]):
"""Wrapper for Elmax entities."""
def __init__(
self,
elmax_device: DeviceEndpoint,
panel_version: str,
coordinator: ElmaxCoordinator,
) -> None:
"""Construct the object."""
super().__init__(coordinator=coordinator)
self._device = elmax_device
self._attr_unique_id = elmax_device.endpoint_id
self._attr_name = elmax_device.name
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, coordinator.panel_entry.hash)},
name=coordinator.panel_entry.get_name_by_user(
coordinator.http_client.get_authenticated_username()
),
manufacturer="Elmax",
model=panel_version,
sw_version=panel_version,
)
@property
def available(self) -> bool:
"""Return if entity is available."""
return super().available and self.coordinator.panel_entry.online

View File

@ -12,9 +12,9 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .common import ElmaxEntity
from .const import DOMAIN from .const import DOMAIN
from .coordinator import ElmaxCoordinator from .coordinator import ElmaxCoordinator
from .entity import ElmaxEntity
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)