Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for StarLine switch."""
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 .account import StarlineAccount, StarlineDevice
13 from .const import DOMAIN
14 from .entity import StarlineEntity
15 
16 SWITCH_TYPES: tuple[SwitchEntityDescription, ...] = (
18  key="ign",
19  translation_key="engine",
20  ),
22  key="webasto",
23  translation_key="webasto",
24  ),
26  key="out",
27  translation_key="additional_channel",
28  ),
30  key="valet",
31  translation_key="service_mode",
32  ),
33 )
34 
35 
37  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
38 ) -> None:
39  """Set up the StarLine switch."""
40  account: StarlineAccount = hass.data[DOMAIN][entry.entry_id]
41  entities = [
42  switch
43  for device in account.api.devices.values()
44  if device.support_state
45  for description in SWITCH_TYPES
46  if (switch := StarlineSwitch(account, device, description)).is_on is not None
47  ]
48  async_add_entities(entities)
49 
50 
52  """Representation of a StarLine switch."""
53 
54  _attr_assumed_state = True
55 
56  def __init__(
57  self,
58  account: StarlineAccount,
59  device: StarlineDevice,
60  description: SwitchEntityDescription,
61  ) -> None:
62  """Initialize the switch."""
63  super().__init__(account, device, description.key)
64  self.entity_descriptionentity_description = description
65 
66  @property
67  def available(self) -> bool:
68  """Return True if entity is available."""
69  return super().available and self._device_device.online
70 
71  @property
73  """Return the state attributes of the switch."""
74  if self._key_key_key == "ign":
75  return self._account_account.engine_attrs(self._device_device)
76  return None
77 
78  @property
79  def is_on(self):
80  """Return True if entity is on."""
81  return self._device_device.car_state.get(self._key_key_key)
82 
83  def turn_on(self, **kwargs: Any) -> None:
84  """Turn the entity on."""
85  self._account_account.api.set_car_state(self._device_device.device_id, self._key_key_key, True)
86 
87  def turn_off(self, **kwargs: Any) -> None:
88  """Turn the entity off."""
89  self._account_account.api.set_car_state(self._device_device.device_id, self._key_key_key, False)
None __init__(self, StarlineAccount account, StarlineDevice device, SwitchEntityDescription description)
Definition: switch.py:61
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:38