mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Move nissan_leaf base entity to separate module (#126106)
This commit is contained in:
parent
84c20745a8
commit
7fee61db84
@ -17,14 +17,10 @@ from pycarwings2.responses import (
|
|||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.const import CONF_PASSWORD, CONF_REGION, CONF_USERNAME, Platform
|
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
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.discovery import load_platform
|
from homeassistant.helpers.discovery import load_platform
|
||||||
from homeassistant.helpers.dispatcher import (
|
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||||
async_dispatcher_connect,
|
|
||||||
async_dispatcher_send,
|
|
||||||
)
|
|
||||||
from homeassistant.helpers.entity import Entity
|
|
||||||
from homeassistant.helpers.event import async_track_point_in_utc_time
|
from homeassistant.helpers.event import async_track_point_in_utc_time
|
||||||
from homeassistant.helpers.typing import ConfigType
|
from homeassistant.helpers.typing import ConfigType
|
||||||
from homeassistant.util.dt import utcnow
|
from homeassistant.util.dt import utcnow
|
||||||
@ -52,6 +48,7 @@ from .const import (
|
|||||||
PYCARWINGS2_SLEEP,
|
PYCARWINGS2_SLEEP,
|
||||||
RESTRICTED_BATTERY,
|
RESTRICTED_BATTERY,
|
||||||
RESTRICTED_INTERVAL,
|
RESTRICTED_INTERVAL,
|
||||||
|
SIGNAL_UPDATE_LEAF,
|
||||||
)
|
)
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
@ -90,7 +87,6 @@ CONFIG_SCHEMA = vol.Schema(
|
|||||||
|
|
||||||
PLATFORMS = [Platform.BINARY_SENSOR, Platform.BUTTON, Platform.SENSOR, Platform.SWITCH]
|
PLATFORMS = [Platform.BINARY_SENSOR, Platform.BUTTON, Platform.SENSOR, Platform.SWITCH]
|
||||||
|
|
||||||
SIGNAL_UPDATE_LEAF = "nissan_leaf_update"
|
|
||||||
|
|
||||||
SERVICE_UPDATE_LEAF = "update"
|
SERVICE_UPDATE_LEAF = "update"
|
||||||
SERVICE_START_CHARGE_LEAF = "start_charge"
|
SERVICE_START_CHARGE_LEAF = "start_charge"
|
||||||
@ -496,44 +492,3 @@ class LeafDataStore:
|
|||||||
self._remove_listener = async_track_point_in_utc_time(
|
self._remove_listener = async_track_point_in_utc_time(
|
||||||
self.hass, self.async_update_data, update_at
|
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)
|
|
||||||
|
@ -12,8 +12,9 @@ from homeassistant.core import HomeAssistant
|
|||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
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 .const import DATA_CHARGING, DATA_LEAF, DATA_PLUGGED_IN
|
||||||
|
from .entity import LeafEntity
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -9,7 +9,8 @@ from homeassistant.core import HomeAssistant
|
|||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
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__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -34,3 +34,5 @@ RESTRICTED_BATTERY: Final = 2
|
|||||||
MAX_RESPONSE_ATTEMPTS: Final = 3
|
MAX_RESPONSE_ATTEMPTS: Final = 3
|
||||||
|
|
||||||
PYCARWINGS2_SLEEP: Final = 40
|
PYCARWINGS2_SLEEP: Final = 40
|
||||||
|
|
||||||
|
SIGNAL_UPDATE_LEAF = "nissan_leaf_update"
|
||||||
|
56
homeassistant/components/nissan_leaf/entity.py
Normal file
56
homeassistant/components/nissan_leaf/entity.py
Normal 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)
|
@ -13,7 +13,7 @@ from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType, StateTyp
|
|||||||
from homeassistant.util.unit_conversion import DistanceConverter
|
from homeassistant.util.unit_conversion import DistanceConverter
|
||||||
from homeassistant.util.unit_system import US_CUSTOMARY_SYSTEM
|
from homeassistant.util.unit_system import US_CUSTOMARY_SYSTEM
|
||||||
|
|
||||||
from . import LeafDataStore, LeafEntity
|
from . import LeafDataStore
|
||||||
from .const import (
|
from .const import (
|
||||||
DATA_BATTERY,
|
DATA_BATTERY,
|
||||||
DATA_CHARGING,
|
DATA_CHARGING,
|
||||||
@ -21,6 +21,7 @@ from .const import (
|
|||||||
DATA_RANGE_AC,
|
DATA_RANGE_AC,
|
||||||
DATA_RANGE_AC_OFF,
|
DATA_RANGE_AC_OFF,
|
||||||
)
|
)
|
||||||
|
from .entity import LeafEntity
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -10,8 +10,9 @@ from homeassistant.core import HomeAssistant
|
|||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||||
|
|
||||||
from . import LeafDataStore, LeafEntity
|
from . import LeafDataStore
|
||||||
from .const import DATA_CLIMATE, DATA_LEAF
|
from .const import DATA_CLIMATE, DATA_LEAF
|
||||||
|
from .entity import LeafEntity
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user