Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Home Assistant component for accessing the Wallbox Portal API. The switch component creates a switch entity."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
8 from homeassistant.config_entries import ConfigEntry
9 from homeassistant.core import HomeAssistant
10 from homeassistant.helpers.entity_platform import AddEntitiesCallback
11 
12 from .const import (
13  CHARGER_DATA_KEY,
14  CHARGER_PAUSE_RESUME_KEY,
15  CHARGER_SERIAL_NUMBER_KEY,
16  CHARGER_STATUS_DESCRIPTION_KEY,
17  DOMAIN,
18  ChargerStatus,
19 )
20 from .coordinator import WallboxCoordinator
21 from .entity import WallboxEntity
22 
23 SWITCH_TYPES: dict[str, SwitchEntityDescription] = {
24  CHARGER_PAUSE_RESUME_KEY: SwitchEntityDescription(
25  key=CHARGER_PAUSE_RESUME_KEY,
26  translation_key="pause_resume",
27  ),
28 }
29 
30 
32  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
33 ) -> None:
34  """Create wallbox sensor entities in HASS."""
35  coordinator: WallboxCoordinator = hass.data[DOMAIN][entry.entry_id]
37  [WallboxSwitch(coordinator, SWITCH_TYPES[CHARGER_PAUSE_RESUME_KEY])]
38  )
39 
40 
42  """Representation of the Wallbox portal."""
43 
44  def __init__(
45  self,
46  coordinator: WallboxCoordinator,
47  description: SwitchEntityDescription,
48  ) -> None:
49  """Initialize a Wallbox switch."""
50  super().__init__(coordinator)
51  self.entity_descriptionentity_description = description
52  self._attr_unique_id_attr_unique_id = f"{description.key}-{coordinator.data[CHARGER_DATA_KEY][CHARGER_SERIAL_NUMBER_KEY]}"
53 
54  @property
55  def available(self) -> bool:
56  """Return the availability of the switch."""
57  return super().available and self.coordinator.data[
58  CHARGER_STATUS_DESCRIPTION_KEY
59  ] not in {
60  ChargerStatus.UNKNOWN,
61  ChargerStatus.UPDATING,
62  ChargerStatus.ERROR,
63  ChargerStatus.LOCKED,
64  ChargerStatus.LOCKED_CAR_CONNECTED,
65  ChargerStatus.DISCONNECTED,
66  ChargerStatus.READY,
67  }
68 
69  @property
70  def is_on(self) -> bool:
71  """Return the status of pause/resume."""
72  return self.coordinator.data[CHARGER_STATUS_DESCRIPTION_KEY] in {
73  ChargerStatus.CHARGING,
74  ChargerStatus.DISCHARGING,
75  ChargerStatus.WAITING_FOR_CAR,
76  ChargerStatus.WAITING,
77  }
78 
79  async def async_turn_off(self, **kwargs: Any) -> None:
80  """Pause charger."""
81  await self.coordinator.async_pause_charger(True)
82 
83  async def async_turn_on(self, **kwargs: Any) -> None:
84  """Resume charger."""
85  await self.coordinator.async_pause_charger(False)
None __init__(self, WallboxCoordinator coordinator, SwitchEntityDescription description)
Definition: switch.py:48
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:33