Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for Huawei LTE switches."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
9  DOMAIN as SWITCH_DOMAIN,
10  SwitchDeviceClass,
11  SwitchEntity,
12 )
13 from homeassistant.config_entries import ConfigEntry
14 from homeassistant.core import HomeAssistant
15 from homeassistant.helpers.entity import Entity
16 from homeassistant.helpers.entity_platform import AddEntitiesCallback
17 
18 from .const import (
19  DOMAIN,
20  KEY_DIALUP_MOBILE_DATASWITCH,
21  KEY_WLAN_WIFI_GUEST_NETWORK_SWITCH,
22 )
23 from .entity import HuaweiLteBaseEntityWithDevice
24 
25 _LOGGER = logging.getLogger(__name__)
26 
27 
29  hass: HomeAssistant,
30  config_entry: ConfigEntry,
31  async_add_entities: AddEntitiesCallback,
32 ) -> None:
33  """Set up from config entry."""
34  router = hass.data[DOMAIN].routers[config_entry.entry_id]
35  switches: list[Entity] = []
36 
37  if router.data.get(KEY_DIALUP_MOBILE_DATASWITCH):
38  switches.append(HuaweiLteMobileDataSwitch(router))
39 
40  if router.data.get(KEY_WLAN_WIFI_GUEST_NETWORK_SWITCH, {}).get("WifiEnable"):
41  switches.append(HuaweiLteWifiGuestNetworkSwitch(router))
42 
43  async_add_entities(switches, True)
44 
45 
47  """Huawei LTE switch device base class."""
48 
49  key: str
50  item: str
51 
52  _attr_device_class: SwitchDeviceClass = SwitchDeviceClass.SWITCH
53  _raw_state: str | None = None
54 
55  def _turn(self, state: bool) -> None:
56  raise NotImplementedError
57 
58  def turn_on(self, **kwargs: Any) -> None:
59  """Turn switch on."""
60  self._turn_turn(state=True)
61 
62  def turn_off(self, **kwargs: Any) -> None:
63  """Turn switch off."""
64  self._turn_turn(state=False)
65 
66  async def async_added_to_hass(self) -> None:
67  """Subscribe to needed data on add."""
68  await super().async_added_to_hass()
69  self.routerrouter.subscriptions[self.key].append(f"{SWITCH_DOMAIN}/{self.item}")
70 
71  async def async_will_remove_from_hass(self) -> None:
72  """Unsubscribe from needed data on remove."""
73  await super().async_will_remove_from_hass()
74  self.routerrouter.subscriptions[self.key].remove(f"{SWITCH_DOMAIN}/{self.item}")
75 
76  async def async_update(self) -> None:
77  """Update state."""
78  try:
79  value = self.routerrouter.data[self.key][self.item]
80  except KeyError:
81  _LOGGER.debug("%s[%s] not in data", self.key, self.item)
82  self._available_available_available = False
83  return
84  self._available_available_available = True
85  self._raw_state_raw_state = str(value)
86 
87 
89  """Huawei LTE mobile data switch device."""
90 
91  _attr_translation_key: str = "mobile_data"
92 
93  key = KEY_DIALUP_MOBILE_DATASWITCH
94  item = "dataswitch"
95 
96  @property
97  def _device_unique_id(self) -> str:
98  return f"{self.key}.{self.item}"
99 
100  @property
101  def is_on(self) -> bool:
102  """Return whether the switch is on."""
103  return self._raw_state_raw_state_raw_state == "1"
104 
105  def _turn(self, state: bool) -> None:
106  value = 1 if state else 0
107  self.routerrouter.client.dial_up.set_mobile_dataswitch(dataswitch=value)
108  self._raw_state_raw_state_raw_state = str(value)
109  self.schedule_update_ha_stateschedule_update_ha_state()
110 
111 
113  """Huawei LTE WiFi guest network switch device."""
114 
115  _attr_translation_key: str = "wifi_guest_network"
116 
117  key = KEY_WLAN_WIFI_GUEST_NETWORK_SWITCH
118  item = "WifiEnable"
119 
120  @property
121  def _device_unique_id(self) -> str:
122  return f"{self.key}.{self.item}"
123 
124  @property
125  def is_on(self) -> bool:
126  """Return whether the switch is on."""
127  return self._raw_state_raw_state_raw_state == "1"
128 
129  def _turn(self, state: bool) -> None:
130  self.routerrouter.client.wlan.wifi_guest_network_switch(state)
131  self._raw_state_raw_state_raw_state = "1" if state else "0"
132  self.schedule_update_ha_stateschedule_update_ha_state()
133 
134  @property
135  def extra_state_attributes(self) -> dict[str, str | None]:
136  """Return the state attributes."""
137  return {"ssid": self.routerrouter.data[self.keykey].get("WifiSsid")}
None schedule_update_ha_state(self, bool force_refresh=False)
Definition: entity.py:1244
bool remove(self, _T matcher)
Definition: match.py:214
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:32