Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for the Hive switches."""
2 
3 from __future__ import annotations
4 
5 from datetime import timedelta
6 from typing import Any
7 
8 from apyhiveapi import Hive
9 
10 from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
11 from homeassistant.config_entries import ConfigEntry
12 from homeassistant.const import EntityCategory
13 from homeassistant.core import HomeAssistant
14 from homeassistant.helpers.entity_platform import AddEntitiesCallback
15 
16 from . import refresh_system
17 from .const import ATTR_MODE, DOMAIN
18 from .entity import HiveEntity
19 
20 PARALLEL_UPDATES = 0
21 SCAN_INTERVAL = timedelta(seconds=15)
22 
23 
24 SWITCH_TYPES: tuple[SwitchEntityDescription, ...] = (
26  key="activeplug",
27  ),
29  key="Heating_Heat_On_Demand",
30  entity_category=EntityCategory.CONFIG,
31  ),
32 )
33 
34 
36  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
37 ) -> None:
38  """Set up Hive thermostat based on a config entry."""
39 
40  hive = hass.data[DOMAIN][entry.entry_id]
41  devices = hive.session.deviceList.get("switch")
42  if not devices:
43  return
45  (
46  HiveSwitch(hive, dev, description)
47  for dev in devices
48  for description in SWITCH_TYPES
49  if dev["hiveType"] == description.key
50  ),
51  True,
52  )
53 
54 
56  """Hive Active Plug."""
57 
58  def __init__(
59  self,
60  hive: Hive,
61  hive_device: dict[str, Any],
62  entity_description: SwitchEntityDescription,
63  ) -> None:
64  """Initialise hive switch."""
65  super().__init__(hive, hive_device)
66  self.entity_descriptionentity_description = entity_description
67 
68  @refresh_system
69  async def async_turn_on(self, **kwargs: Any) -> None:
70  """Turn the switch on."""
71  await self.hivehive.switch.turnOn(self.devicedevicedevice)
72 
73  @refresh_system
74  async def async_turn_off(self, **kwargs: Any) -> None:
75  """Turn the device off."""
76  await self.hivehive.switch.turnOff(self.devicedevicedevice)
77 
78  async def async_update(self) -> None:
79  """Update all Node data from Hive."""
80  await self.hivehive.session.updateData(self.devicedevicedevice)
81  self.devicedevicedevice = await self.hivehive.switch.getSwitch(self.devicedevicedevice)
82  self.attributes.update(self.devicedevicedevice.get("attributes", {}))
83  self._attr_extra_state_attributes_attr_extra_state_attributes = {
84  ATTR_MODE: self.attributes.get(ATTR_MODE),
85  }
86  self._attr_available_attr_available = self.devicedevicedevice["deviceData"].get("online")
87  if self._attr_available_attr_available:
88  self._attr_is_on_attr_is_on = self.devicedevicedevice["status"]["state"]
None __init__(self, Hive hive, dict[str, Any] hive_device, SwitchEntityDescription entity_description)
Definition: switch.py:63
None async_turn_off(self, **Any kwargs)
Definition: switch.py:74
None async_turn_on(self, **Any kwargs)
Definition: switch.py:69
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:37
IssData update(pyiss.ISS iss)
Definition: __init__.py:33