Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Support for the Nissan Leaf Carwings/Nissan Connect API."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from homeassistant.core import callback
9 from homeassistant.helpers.dispatcher import async_dispatcher_connect
10 from homeassistant.helpers.entity import Entity
11 
12 from . import LeafDataStore
13 from .const import SIGNAL_UPDATE_LEAF
14 
15 _LOGGER = logging.getLogger(__name__)
16 
17 
19  """Base class for Nissan Leaf entity."""
20 
21  def __init__(self, car: LeafDataStore) -> None:
22  """Store LeafDataStore upon init."""
23  self.carcar = car
24 
25  def log_registration(self) -> None:
26  """Log registration."""
27  _LOGGER.debug(
28  "Registered %s integration for VIN %s",
29  self.__class__.__name__,
30  self.carcar.leaf.vin,
31  )
32 
33  @property
34  def extra_state_attributes(self) -> dict[str, Any]:
35  """Return default attributes for Nissan leaf entities."""
36  return {
37  "next_update": self.carcar.next_update,
38  "last_attempt": self.carcar.last_check,
39  "updated_on": self.carcar.last_battery_response,
40  "update_in_progress": self.carcar.request_in_progress,
41  "vin": self.carcar.leaf.vin,
42  }
43 
44  async def async_added_to_hass(self) -> None:
45  """Register callbacks."""
46  self.log_registrationlog_registration()
47  self.async_on_removeasync_on_remove(
49  self.carcar.hass, SIGNAL_UPDATE_LEAF, self._update_callback_update_callback
50  )
51  )
52 
53  @callback
54  def _update_callback(self) -> None:
55  """Update the state."""
56  self.async_schedule_update_ha_stateasync_schedule_update_ha_state(True)
None __init__(self, LeafDataStore car)
Definition: entity.py:21
None async_schedule_update_ha_state(self, bool force_refresh=False)
Definition: entity.py:1265
None async_on_remove(self, CALLBACK_TYPE func)
Definition: entity.py:1331
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103