Move nissan_leaf base entity to separate module (#126106)

pull/126128/head
epenet 2024-09-17 15:37:39 +02:00 committed by GitHub
parent 84c20745a8
commit 7fee61db84
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 69 additions and 52 deletions

View File

@ -17,14 +17,10 @@ from pycarwings2.responses import (
import voluptuous as vol
from homeassistant.const import CONF_PASSWORD, CONF_REGION, CONF_USERNAME, Platform
from homeassistant.core import CALLBACK_TYPE, HomeAssistant, ServiceCall, callback
from homeassistant.core import CALLBACK_TYPE, HomeAssistant, ServiceCall
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.discovery import load_platform
from homeassistant.helpers.dispatcher import (
async_dispatcher_connect,
async_dispatcher_send,
)
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.dispatcher import async_dispatcher_send
from homeassistant.helpers.event import async_track_point_in_utc_time
from homeassistant.helpers.typing import ConfigType
from homeassistant.util.dt import utcnow
@ -52,6 +48,7 @@ from .const import (
PYCARWINGS2_SLEEP,
RESTRICTED_BATTERY,
RESTRICTED_INTERVAL,
SIGNAL_UPDATE_LEAF,
)
_LOGGER = logging.getLogger(__name__)
@ -90,7 +87,6 @@ CONFIG_SCHEMA = vol.Schema(
PLATFORMS = [Platform.BINARY_SENSOR, Platform.BUTTON, Platform.SENSOR, Platform.SWITCH]
SIGNAL_UPDATE_LEAF = "nissan_leaf_update"
SERVICE_UPDATE_LEAF = "update"
SERVICE_START_CHARGE_LEAF = "start_charge"
@ -496,44 +492,3 @@ class LeafDataStore:
self._remove_listener = async_track_point_in_utc_time(
self.hass, self.async_update_data, update_at
)
class LeafEntity(Entity):
"""Base class for Nissan Leaf entity."""
def __init__(self, car: LeafDataStore) -> None:
"""Store LeafDataStore upon init."""
self.car = car
def log_registration(self) -> None:
"""Log registration."""
_LOGGER.debug(
"Registered %s integration for VIN %s",
self.__class__.__name__,
self.car.leaf.vin,
)
@property
def extra_state_attributes(self) -> dict[str, Any]:
"""Return default attributes for Nissan leaf entities."""
return {
"next_update": self.car.next_update,
"last_attempt": self.car.last_check,
"updated_on": self.car.last_battery_response,
"update_in_progress": self.car.request_in_progress,
"vin": self.car.leaf.vin,
}
async def async_added_to_hass(self) -> None:
"""Register callbacks."""
self.log_registration()
self.async_on_remove(
async_dispatcher_connect(
self.car.hass, SIGNAL_UPDATE_LEAF, self._update_callback
)
)
@callback
def _update_callback(self) -> None:
"""Update the state."""
self.async_schedule_update_ha_state(True)

View File

@ -12,8 +12,9 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from . import LeafDataStore, LeafEntity
from . import LeafDataStore
from .const import DATA_CHARGING, DATA_LEAF, DATA_PLUGGED_IN
from .entity import LeafEntity
_LOGGER = logging.getLogger(__name__)

View File

@ -9,7 +9,8 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from . import DATA_CHARGING, DATA_LEAF, LeafEntity
from . import DATA_CHARGING, DATA_LEAF
from .entity import LeafEntity
_LOGGER = logging.getLogger(__name__)

View File

@ -34,3 +34,5 @@ RESTRICTED_BATTERY: Final = 2
MAX_RESPONSE_ATTEMPTS: Final = 3
PYCARWINGS2_SLEEP: Final = 40
SIGNAL_UPDATE_LEAF = "nissan_leaf_update"

View File

@ -0,0 +1,56 @@
"""Support for the Nissan Leaf Carwings/Nissan Connect API."""
from __future__ import annotations
import logging
from typing import Any
from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import Entity
from . import LeafDataStore
from .const import SIGNAL_UPDATE_LEAF
_LOGGER = logging.getLogger(__name__)
class LeafEntity(Entity):
"""Base class for Nissan Leaf entity."""
def __init__(self, car: LeafDataStore) -> None:
"""Store LeafDataStore upon init."""
self.car = car
def log_registration(self) -> None:
"""Log registration."""
_LOGGER.debug(
"Registered %s integration for VIN %s",
self.__class__.__name__,
self.car.leaf.vin,
)
@property
def extra_state_attributes(self) -> dict[str, Any]:
"""Return default attributes for Nissan leaf entities."""
return {
"next_update": self.car.next_update,
"last_attempt": self.car.last_check,
"updated_on": self.car.last_battery_response,
"update_in_progress": self.car.request_in_progress,
"vin": self.car.leaf.vin,
}
async def async_added_to_hass(self) -> None:
"""Register callbacks."""
self.log_registration()
self.async_on_remove(
async_dispatcher_connect(
self.car.hass, SIGNAL_UPDATE_LEAF, self._update_callback
)
)
@callback
def _update_callback(self) -> None:
"""Update the state."""
self.async_schedule_update_ha_state(True)

View File

@ -13,7 +13,7 @@ from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType, StateTyp
from homeassistant.util.unit_conversion import DistanceConverter
from homeassistant.util.unit_system import US_CUSTOMARY_SYSTEM
from . import LeafDataStore, LeafEntity
from . import LeafDataStore
from .const import (
DATA_BATTERY,
DATA_CHARGING,
@ -21,6 +21,7 @@ from .const import (
DATA_RANGE_AC,
DATA_RANGE_AC_OFF,
)
from .entity import LeafEntity
_LOGGER = logging.getLogger(__name__)

View File

@ -10,8 +10,9 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from . import LeafDataStore, LeafEntity
from . import LeafDataStore
from .const import DATA_CLIMATE, DATA_LEAF
from .entity import LeafEntity
_LOGGER = logging.getLogger(__name__)