Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for Volvo heater."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from volvooncall.dashboard import Instrument
8 
9 from homeassistant.components.switch import SwitchEntity
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.core import HomeAssistant, callback
12 from homeassistant.helpers.dispatcher import async_dispatcher_connect
13 from homeassistant.helpers.entity_platform import AddEntitiesCallback
14 
15 from .const import DOMAIN, VOLVO_DISCOVERY_NEW
16 from .coordinator import VolvoUpdateCoordinator
17 from .entity import VolvoEntity
18 
19 
21  hass: HomeAssistant,
22  config_entry: ConfigEntry,
23  async_add_entities: AddEntitiesCallback,
24 ) -> None:
25  """Configure binary_sensors from a config entry created in the integrations UI."""
26  coordinator: VolvoUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id]
27  volvo_data = coordinator.volvo_data
28 
29  @callback
30  def async_discover_device(instruments: list[Instrument]) -> None:
31  """Discover and add a discovered Volvo On Call switch."""
34  coordinator,
35  instrument.vehicle.vin,
36  instrument.component,
37  instrument.attr,
38  instrument.slug_attr,
39  )
40  for instrument in instruments
41  if instrument.component == "switch"
42  )
43 
44  async_discover_device([*volvo_data.instruments])
45 
46  config_entry.async_on_unload(
47  async_dispatcher_connect(hass, VOLVO_DISCOVERY_NEW, async_discover_device)
48  )
49 
50 
52  """Representation of a Volvo switch."""
53 
54  def __init__(
55  self,
56  coordinator: VolvoUpdateCoordinator,
57  vin: str,
58  component: str,
59  attribute: str,
60  slug_attr: str,
61  ) -> None:
62  """Initialize the switch."""
63  super().__init__(vin, component, attribute, slug_attr, coordinator)
64 
65  @property
66  def is_on(self):
67  """Determine if switch is on."""
68  return self.instrumentinstrument.state
69 
70  async def async_turn_on(self, **kwargs: Any) -> None:
71  """Turn the switch on."""
72  await self.instrumentinstrument.turn_on()
73  await self.coordinator.async_request_refresh()
74 
75  async def async_turn_off(self, **kwargs: Any) -> None:
76  """Turn the switch off."""
77  await self.instrumentinstrument.turn_off()
78  await self.coordinator.async_request_refresh()
None __init__(self, VolvoUpdateCoordinator coordinator, str vin, str component, str attribute, str slug_attr)
Definition: switch.py:61
None turn_off(self, **Any kwargs)
Definition: entity.py:1705
None turn_on(self, **Any kwargs)
Definition: entity.py:1697
ElkSystem|None async_discover_device(HomeAssistant hass, str host)
Definition: discovery.py:78
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:24
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103