Move tesla_wall_connector base entity to separate module (#126529)
This commit is contained in:
parent
46f9e86f6a
commit
a579eef66c
4 changed files with 55 additions and 56 deletions
|
@ -2,11 +2,9 @@
|
|||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Callable
|
||||
from dataclasses import dataclass
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
from tesla_wall_connector import WallConnector
|
||||
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.exceptions import ConfigEntryNotReady
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
from homeassistant.helpers.device_registry import DeviceInfo
|
||||
from homeassistant.helpers.update_coordinator import (
|
||||
CoordinatorEntity,
|
||||
DataUpdateCoordinator,
|
||||
UpdateFailed,
|
||||
)
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||
|
||||
from .const import (
|
||||
DEFAULT_SCAN_INTERVAL,
|
||||
DOMAIN,
|
||||
WALLCONNECTOR_DATA_LIFETIME,
|
||||
WALLCONNECTOR_DATA_VITALS,
|
||||
WALLCONNECTOR_DEVICE_NAME,
|
||||
)
|
||||
|
||||
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
|
||||
|
||||
|
||||
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
|
||||
class WallConnectorData:
|
||||
"""Data for the Tesla Wall Connector integration."""
|
||||
|
|
|
@ -13,12 +13,9 @@ from homeassistant.const import EntityCategory
|
|||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import (
|
||||
WallConnectorData,
|
||||
WallConnectorEntity,
|
||||
WallConnectorLambdaValueGetterMixin,
|
||||
)
|
||||
from . import WallConnectorData
|
||||
from .const import DOMAIN, WALLCONNECTOR_DATA_VITALS
|
||||
from .entity import WallConnectorEntity, WallConnectorLambdaValueGetterMixin
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
|
50
homeassistant/components/tesla_wall_connector/entity.py
Normal file
50
homeassistant/components/tesla_wall_connector/entity.py
Normal 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",
|
||||
)
|
|
@ -21,12 +21,9 @@ from homeassistant.const import (
|
|||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import (
|
||||
WallConnectorData,
|
||||
WallConnectorEntity,
|
||||
WallConnectorLambdaValueGetterMixin,
|
||||
)
|
||||
from . import WallConnectorData
|
||||
from .const import DOMAIN, WALLCONNECTOR_DATA_LIFETIME, WALLCONNECTOR_DATA_VITALS
|
||||
from .entity import WallConnectorEntity, WallConnectorLambdaValueGetterMixin
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue