From a579eef66c777479814a8ee4eab2e02979e9ec56 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Mon, 23 Sep 2024 14:16:13 +0200 Subject: [PATCH] Move tesla_wall_connector base entity to separate module (#126529) --- .../tesla_wall_connector/__init__.py | 47 +---------------- .../tesla_wall_connector/binary_sensor.py | 7 +-- .../components/tesla_wall_connector/entity.py | 50 +++++++++++++++++++ .../components/tesla_wall_connector/sensor.py | 7 +-- 4 files changed, 55 insertions(+), 56 deletions(-) create mode 100644 homeassistant/components/tesla_wall_connector/entity.py diff --git a/homeassistant/components/tesla_wall_connector/__init__.py b/homeassistant/components/tesla_wall_connector/__init__.py index 28ddc15ade7..f4d04ca8cc6 100644 --- a/homeassistant/components/tesla_wall_connector/__init__.py +++ b/homeassistant/components/tesla_wall_connector/__init__.py @@ -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.""" diff --git a/homeassistant/components/tesla_wall_connector/binary_sensor.py b/homeassistant/components/tesla_wall_connector/binary_sensor.py index cf8fbf53b52..f7ef385b8ed 100644 --- a/homeassistant/components/tesla_wall_connector/binary_sensor.py +++ b/homeassistant/components/tesla_wall_connector/binary_sensor.py @@ -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__) diff --git a/homeassistant/components/tesla_wall_connector/entity.py b/homeassistant/components/tesla_wall_connector/entity.py new file mode 100644 index 00000000000..ea08a00e791 --- /dev/null +++ b/homeassistant/components/tesla_wall_connector/entity.py @@ -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", + ) diff --git a/homeassistant/components/tesla_wall_connector/sensor.py b/homeassistant/components/tesla_wall_connector/sensor.py index 077f70c5370..a50c81c912e 100644 --- a/homeassistant/components/tesla_wall_connector/sensor.py +++ b/homeassistant/components/tesla_wall_connector/sensor.py @@ -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__)