Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Charge and Climate Control Support for the Nissan Leaf."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from homeassistant.components.switch import SwitchEntity
9 from homeassistant.core import HomeAssistant
10 from homeassistant.helpers.entity_platform import AddEntitiesCallback
11 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
12 
13 from . import LeafDataStore
14 from .const import DATA_CLIMATE, DATA_LEAF
15 from .entity import LeafEntity
16 
17 _LOGGER = logging.getLogger(__name__)
18 
19 
21  hass: HomeAssistant,
22  config: ConfigType,
23  add_entities: AddEntitiesCallback,
24  discovery_info: DiscoveryInfoType | None = None,
25 ) -> None:
26  """Nissan Leaf switch platform setup."""
27  if discovery_info is None:
28  return
29 
30  entities: list[LeafEntity] = []
31  for vin, datastore in hass.data[DATA_LEAF].items():
32  _LOGGER.debug("Adding switch for vin=%s", vin)
33  entities.append(LeafClimateSwitch(datastore))
34 
35  add_entities(entities, True)
36 
37 
39  """Nissan Leaf Climate Control switch."""
40 
41  def __init__(self, car: LeafDataStore) -> None:
42  """Set up climate control switch."""
43  super().__init__(car)
44  self._attr_unique_id_attr_unique_id = f"{self.car.leaf.vin.lower()}_climatecontrol"
45 
46  @property
47  def name(self) -> str:
48  """Switch name."""
49  return f"{self.car.leaf.nickname} Climate Control"
50 
51  def log_registration(self) -> None:
52  """Log registration."""
53  _LOGGER.debug(
54  "Registered LeafClimateSwitch integration with Home Assistant for VIN %s",
55  self.carcar.leaf.vin,
56  )
57 
58  @property
59  def extra_state_attributes(self) -> dict[str, Any]:
60  """Return climate control attributes."""
61  attrs = super().extra_state_attributes
62  attrs["updated_on"] = self.carcar.last_climate_response
63  return attrs
64 
65  @property
66  def is_on(self) -> bool:
67  """Return true if climate control is on."""
68  return bool(self.carcar.data[DATA_CLIMATE])
69 
70  async def async_turn_on(self, **kwargs: Any) -> None:
71  """Turn on climate control."""
72  if await self.carcar.async_set_climate(True):
73  self.carcar.data[DATA_CLIMATE] = True
74 
75  async def async_turn_off(self, **kwargs: Any) -> None:
76  """Turn off climate control."""
77  if await self.carcar.async_set_climate(False):
78  self.carcar.data[DATA_CLIMATE] = False
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: switch.py:25