diff --git a/homeassistant/components/nissan_leaf/__init__.py b/homeassistant/components/nissan_leaf/__init__.py index 2cbec236261..865ae33b38c 100644 --- a/homeassistant/components/nissan_leaf/__init__.py +++ b/homeassistant/components/nissan_leaf/__init__.py @@ -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) diff --git a/homeassistant/components/nissan_leaf/binary_sensor.py b/homeassistant/components/nissan_leaf/binary_sensor.py index 3b15fabe382..7938b314deb 100644 --- a/homeassistant/components/nissan_leaf/binary_sensor.py +++ b/homeassistant/components/nissan_leaf/binary_sensor.py @@ -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__) diff --git a/homeassistant/components/nissan_leaf/button.py b/homeassistant/components/nissan_leaf/button.py index aa2bbbbca9b..6a5d051751b 100644 --- a/homeassistant/components/nissan_leaf/button.py +++ b/homeassistant/components/nissan_leaf/button.py @@ -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__) diff --git a/homeassistant/components/nissan_leaf/const.py b/homeassistant/components/nissan_leaf/const.py index 299576b86a7..22842fbbc72 100644 --- a/homeassistant/components/nissan_leaf/const.py +++ b/homeassistant/components/nissan_leaf/const.py @@ -34,3 +34,5 @@ RESTRICTED_BATTERY: Final = 2 MAX_RESPONSE_ATTEMPTS: Final = 3 PYCARWINGS2_SLEEP: Final = 40 + +SIGNAL_UPDATE_LEAF = "nissan_leaf_update" diff --git a/homeassistant/components/nissan_leaf/entity.py b/homeassistant/components/nissan_leaf/entity.py new file mode 100644 index 00000000000..73813c8931e --- /dev/null +++ b/homeassistant/components/nissan_leaf/entity.py @@ -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) diff --git a/homeassistant/components/nissan_leaf/sensor.py b/homeassistant/components/nissan_leaf/sensor.py index bde1719e9b1..71dda39db1a 100644 --- a/homeassistant/components/nissan_leaf/sensor.py +++ b/homeassistant/components/nissan_leaf/sensor.py @@ -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__) diff --git a/homeassistant/components/nissan_leaf/switch.py b/homeassistant/components/nissan_leaf/switch.py index 39f875ff95f..82a84567fec 100644 --- a/homeassistant/components/nissan_leaf/switch.py +++ b/homeassistant/components/nissan_leaf/switch.py @@ -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__)