Home Assistant Unofficial Reference 2024.12.1
button.py
Go to the documentation of this file.
1 """Button to start charging the Nissan Leaf."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 from homeassistant.components.button import ButtonEntity
8 from homeassistant.core import HomeAssistant
9 from homeassistant.helpers.entity_platform import AddEntitiesCallback
10 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
11 
12 from . import DATA_CHARGING, DATA_LEAF
13 from .entity import LeafEntity
14 
15 _LOGGER = logging.getLogger(__name__)
16 
17 
19  hass: HomeAssistant,
20  config: ConfigType,
21  add_entities: AddEntitiesCallback,
22  discovery_info: DiscoveryInfoType | None = None,
23 ) -> None:
24  """Set up of a Nissan Leaf button."""
25  if discovery_info is None:
26  return
27 
28  entities: list[LeafEntity] = []
29  for vin, datastore in hass.data[DATA_LEAF].items():
30  _LOGGER.debug("Adding button for vin=%s", vin)
31  entities.append(LeafChargingButton(datastore))
32 
33  add_entities(entities, True)
34 
35 
37  """Charging Button class."""
38 
39  _attr_icon = "mdi:power"
40 
41  @property
42  def name(self) -> str:
43  """Sensor name."""
44  return f"Start {self.car.leaf.nickname} Charging"
45 
46  @property
47  def available(self) -> bool:
48  """Button availability."""
49  return self.carcar.data[DATA_CHARGING] is not None
50 
51  async def async_press(self) -> None:
52  """Start charging."""
53  await self.carcar.async_start_charging()
None add_entities(AsusWrtRouter router, AddEntitiesCallback async_add_entities, set[str] tracked)
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)
Definition: button.py:23