Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Demo platform that has some fake switches."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from homeassistant.components.switch import SwitchDeviceClass, SwitchEntity
8 from homeassistant.config_entries import ConfigEntry
9 from homeassistant.core import HomeAssistant
10 from homeassistant.helpers.device_registry import DeviceInfo
11 from homeassistant.helpers.entity_platform import AddEntitiesCallback
12 
13 from . import DOMAIN
14 from .device import async_create_device
15 
16 
18  hass: HomeAssistant,
19  config_entry: ConfigEntry,
20  async_add_entities: AddEntitiesCallback,
21 ) -> None:
22  """Set up the demo switch platform."""
24  hass,
25  config_entry.entry_id,
26  None,
27  "n_ch_power_strip",
28  {"number_of_sockets": "2"},
29  "2_ch_power_strip",
30  )
31 
33  [
34  DemoSwitch(
35  unique_id="outlet_1",
36  device_name="Outlet 1",
37  entity_name=None,
38  state=False,
39  assumed=False,
40  via_device="2_ch_power_strip",
41  ),
42  DemoSwitch(
43  unique_id="outlet_2",
44  device_name="Outlet 2",
45  entity_name=None,
46  state=True,
47  assumed=False,
48  via_device="2_ch_power_strip",
49  ),
50  ]
51  )
52 
53 
55  """Representation of a demo switch."""
56 
57  _attr_has_entity_name = True
58  _attr_should_poll = False
59 
60  def __init__(
61  self,
62  *,
63  unique_id: str,
64  device_name: str,
65  entity_name: str | None,
66  state: bool,
67  assumed: bool,
68  translation_key: str | None = None,
69  device_class: SwitchDeviceClass | None = None,
70  via_device: str | None = None,
71  ) -> None:
72  """Initialize the Demo switch."""
73  self._attr_assumed_state_attr_assumed_state = assumed
74  self._attr_device_class_attr_device_class = device_class
75  self._attr_translation_key_attr_translation_key = translation_key
76  self._attr_is_on_attr_is_on = state
77  self._attr_unique_id_attr_unique_id = unique_id
78  self._attr_device_info_attr_device_info = DeviceInfo(
79  identifiers={(DOMAIN, unique_id)},
80  name=device_name,
81  )
82  if via_device:
83  self._attr_device_info_attr_device_info["via_device"] = (DOMAIN, via_device)
84  self._attr_name_attr_name = entity_name
85 
86  def turn_on(self, **kwargs: Any) -> None:
87  """Turn the switch on."""
88  self._attr_is_on_attr_is_on = True
89  self.schedule_update_ha_stateschedule_update_ha_state()
90 
91  def turn_off(self, **kwargs: Any) -> None:
92  """Turn the device off."""
93  self._attr_is_on_attr_is_on = False
94  self.schedule_update_ha_stateschedule_update_ha_state()
None __init__(self, *str unique_id, str device_name, str|None entity_name, bool state, bool assumed, str|None translation_key=None, SwitchDeviceClass|None device_class=None, str|None via_device=None)
Definition: switch.py:71
None schedule_update_ha_state(self, bool force_refresh=False)
Definition: entity.py:1244
dr.DeviceEntry async_create_device(HomeAssistant hass, str config_entry_id, str|None device_name, str|None device_translation_key, dict[str, str]|None device_translation_placeholders, str unique_id)
Definition: device.py:18
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:21