Move tesla_wall_connector base entity to separate module (#126529)

This commit is contained in:
epenet 2024-09-23 14:16:13 +02:00 committed by GitHub
parent 46f9e86f6a
commit a579eef66c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 55 additions and 56 deletions

View File

@ -2,11 +2,9 @@
from __future__ import annotations from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass from dataclasses import dataclass
from datetime import timedelta from datetime import timedelta
import logging import logging
from typing import Any
from tesla_wall_connector import WallConnector from tesla_wall_connector import WallConnector
from tesla_wall_connector.exceptions import ( from tesla_wall_connector.exceptions import (
@ -20,19 +18,13 @@ from homeassistant.const import CONF_HOST, CONF_SCAN_INTERVAL, Platform
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
UpdateFailed,
)
from .const import ( from .const import (
DEFAULT_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL,
DOMAIN, DOMAIN,
WALLCONNECTOR_DATA_LIFETIME, WALLCONNECTOR_DATA_LIFETIME,
WALLCONNECTOR_DATA_VITALS, WALLCONNECTOR_DATA_VITALS,
WALLCONNECTOR_DEVICE_NAME,
) )
PLATFORMS: list[Platform] = [Platform.BINARY_SENSOR, Platform.SENSOR] PLATFORMS: list[Platform] = [Platform.BINARY_SENSOR, Platform.SENSOR]
@ -123,43 +115,6 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
return unload_ok return unload_ok
def get_unique_id(serial_number: str, key: str) -> str:
"""Get a unique entity name."""
return f"{serial_number}-{key}"
class WallConnectorEntity(CoordinatorEntity):
"""Base class for Wall Connector entities."""
_attr_has_entity_name = True
def __init__(self, wall_connector_data: WallConnectorData) -> None:
"""Initialize WallConnector Entity."""
self.wall_connector_data = wall_connector_data
self._attr_unique_id = get_unique_id(
wall_connector_data.serial_number, self.entity_description.key
)
super().__init__(wall_connector_data.update_coordinator)
@property
def device_info(self) -> DeviceInfo:
"""Return information about the device."""
return DeviceInfo(
identifiers={(DOMAIN, self.wall_connector_data.serial_number)},
name=WALLCONNECTOR_DEVICE_NAME,
model=self.wall_connector_data.part_number,
sw_version=self.wall_connector_data.firmware_version,
manufacturer="Tesla",
)
@dataclass(frozen=True)
class WallConnectorLambdaValueGetterMixin:
"""Mixin with a function pointer for getting sensor value."""
value_fn: Callable[[dict], Any]
@dataclass @dataclass
class WallConnectorData: class WallConnectorData:
"""Data for the Tesla Wall Connector integration.""" """Data for the Tesla Wall Connector integration."""

View File

@ -13,12 +13,9 @@ from homeassistant.const import EntityCategory
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 . import ( from . import WallConnectorData
WallConnectorData,
WallConnectorEntity,
WallConnectorLambdaValueGetterMixin,
)
from .const import DOMAIN, WALLCONNECTOR_DATA_VITALS from .const import DOMAIN, WALLCONNECTOR_DATA_VITALS
from .entity import WallConnectorEntity, WallConnectorLambdaValueGetterMixin
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)

View File

@ -0,0 +1,50 @@
"""The Tesla Wall Connector integration."""
from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass
from typing import Any
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import WallConnectorData
from .const import DOMAIN, WALLCONNECTOR_DEVICE_NAME
@dataclass(frozen=True)
class WallConnectorLambdaValueGetterMixin:
"""Mixin with a function pointer for getting sensor value."""
value_fn: Callable[[dict], Any]
def _get_unique_id(serial_number: str, key: str) -> str:
"""Get a unique entity name."""
return f"{serial_number}-{key}"
class WallConnectorEntity(CoordinatorEntity):
"""Base class for Wall Connector entities."""
_attr_has_entity_name = True
def __init__(self, wall_connector_data: WallConnectorData) -> None:
"""Initialize WallConnector Entity."""
self.wall_connector_data = wall_connector_data
self._attr_unique_id = _get_unique_id(
wall_connector_data.serial_number, self.entity_description.key
)
super().__init__(wall_connector_data.update_coordinator)
@property
def device_info(self) -> DeviceInfo:
"""Return information about the device."""
return DeviceInfo(
identifiers={(DOMAIN, self.wall_connector_data.serial_number)},
name=WALLCONNECTOR_DEVICE_NAME,
model=self.wall_connector_data.part_number,
sw_version=self.wall_connector_data.firmware_version,
manufacturer="Tesla",
)

View File

@ -21,12 +21,9 @@ from homeassistant.const import (
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 . import ( from . import WallConnectorData
WallConnectorData,
WallConnectorEntity,
WallConnectorLambdaValueGetterMixin,
)
from .const import DOMAIN, WALLCONNECTOR_DATA_LIFETIME, WALLCONNECTOR_DATA_VITALS from .const import DOMAIN, WALLCONNECTOR_DATA_LIFETIME, WALLCONNECTOR_DATA_VITALS
from .entity import WallConnectorEntity, WallConnectorLambdaValueGetterMixin
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)