Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for interface with a Gree climate systems."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 from typing import Any
8 
9 from greeclimate.device import Device
10 
12  SwitchDeviceClass,
13  SwitchEntity,
14  SwitchEntityDescription,
15 )
16 from homeassistant.config_entries import ConfigEntry
17 from homeassistant.core import HomeAssistant, callback
18 from homeassistant.helpers.dispatcher import async_dispatcher_connect
19 from homeassistant.helpers.entity_platform import AddEntitiesCallback
20 
21 from .const import COORDINATORS, DISPATCH_DEVICE_DISCOVERED, DOMAIN
22 from .entity import GreeEntity
23 
24 
25 @dataclass(kw_only=True, frozen=True)
27  """Describes a Gree switch entity."""
28 
29  get_value_fn: Callable[[Device], bool]
30  set_value_fn: Callable[[Device, bool], None]
31 
32 
33 def _set_light(device: Device, value: bool) -> None:
34  """Typed helper to set device light property."""
35  device.light = value
36 
37 
38 def _set_quiet(device: Device, value: bool) -> None:
39  """Typed helper to set device quiet property."""
40  device.quiet = value
41 
42 
43 def _set_fresh_air(device: Device, value: bool) -> None:
44  """Typed helper to set device fresh_air property."""
45  device.fresh_air = value
46 
47 
48 def _set_xfan(device: Device, value: bool) -> None:
49  """Typed helper to set device xfan property."""
50  device.xfan = value
51 
52 
53 def _set_anion(device: Device, value: bool) -> None:
54  """Typed helper to set device anion property."""
55  device.anion = value
56 
57 
58 GREE_SWITCHES: tuple[GreeSwitchEntityDescription, ...] = (
60  key="Panel Light",
61  translation_key="light",
62  get_value_fn=lambda d: d.light,
63  set_value_fn=_set_light,
64  ),
66  key="Quiet",
67  translation_key="quiet",
68  get_value_fn=lambda d: d.quiet,
69  set_value_fn=_set_quiet,
70  ),
72  key="Fresh Air",
73  translation_key="fresh_air",
74  get_value_fn=lambda d: d.fresh_air,
75  set_value_fn=_set_fresh_air,
76  ),
78  key="XFan",
79  translation_key="xfan",
80  get_value_fn=lambda d: d.xfan,
81  set_value_fn=_set_xfan,
82  ),
84  key="Health mode",
85  translation_key="health_mode",
86  get_value_fn=lambda d: d.anion,
87  set_value_fn=_set_anion,
88  entity_registry_enabled_default=False,
89  ),
90 )
91 
92 
94  hass: HomeAssistant,
95  entry: ConfigEntry,
96  async_add_entities: AddEntitiesCallback,
97 ) -> None:
98  """Set up the Gree HVAC device from a config entry."""
99 
100  @callback
101  def init_device(coordinator):
102  """Register the device."""
103 
105  GreeSwitch(coordinator=coordinator, description=description)
106  for description in GREE_SWITCHES
107  )
108 
109  for coordinator in hass.data[DOMAIN][COORDINATORS]:
110  init_device(coordinator)
111 
112  entry.async_on_unload(
113  async_dispatcher_connect(hass, DISPATCH_DEVICE_DISCOVERED, init_device)
114  )
115 
116 
118  """Generic Gree switch entity."""
119 
120  _attr_device_class = SwitchDeviceClass.SWITCH
121  entity_description: GreeSwitchEntityDescription
122 
123  def __init__(self, coordinator, description: GreeSwitchEntityDescription) -> None:
124  """Initialize the Gree device."""
125  self.entity_descriptionentity_description = description
126 
127  super().__init__(coordinator, description.key)
128 
129  @property
130  def is_on(self) -> bool:
131  """Return if the state is turned on."""
132  return self.entity_descriptionentity_description.get_value_fn(self.coordinator.device)
133 
134  async def async_turn_on(self, **kwargs: Any) -> None:
135  """Turn the entity on."""
136  self.entity_descriptionentity_description.set_value_fn(self.coordinator.device, True)
137  await self.coordinator.push_state_update()
138  self.async_write_ha_stateasync_write_ha_state()
139 
140  async def async_turn_off(self, **kwargs: Any) -> None:
141  """Turn the entity off."""
142  self.entity_descriptionentity_description.set_value_fn(self.coordinator.device, False)
143  await self.coordinator.push_state_update()
144  self.async_write_ha_stateasync_write_ha_state()
None __init__(self, coordinator, GreeSwitchEntityDescription description)
Definition: switch.py:123
None async_turn_off(self, **Any kwargs)
Definition: switch.py:140
None async_turn_on(self, **Any kwargs)
Definition: switch.py:134
None _set_light(Device device, bool value)
Definition: switch.py:33
None _set_xfan(Device device, bool value)
Definition: switch.py:48
None _set_anion(Device device, bool value)
Definition: switch.py:53
None _set_quiet(Device device, bool value)
Definition: switch.py:38
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:97
None _set_fresh_air(Device device, bool value)
Definition: switch.py:43
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103