Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for Freebox Delta, Revolution and Mini 4K."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from freebox_api.exceptions import InsufficientPermissionsError
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 .const import DOMAIN
17 from .router import FreeboxRouter
18 
19 _LOGGER = logging.getLogger(__name__)
20 
21 
22 SWITCH_DESCRIPTIONS = [
24  key="wifi",
25  name="Freebox WiFi",
26  entity_category=EntityCategory.CONFIG,
27  )
28 ]
29 
30 
32  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
33 ) -> None:
34  """Set up the switch."""
35  router: FreeboxRouter = hass.data[DOMAIN][entry.unique_id]
36  entities = [
37  FreeboxSwitch(router, entity_description)
38  for entity_description in SWITCH_DESCRIPTIONS
39  ]
40  async_add_entities(entities, True)
41 
42 
44  """Representation of a freebox switch."""
45 
46  def __init__(
47  self, router: FreeboxRouter, entity_description: SwitchEntityDescription
48  ) -> None:
49  """Initialize the switch."""
50  self.entity_descriptionentity_description = entity_description
51  self._router_router = router
52  self._attr_device_info_attr_device_info = router.device_info
53  self._attr_unique_id_attr_unique_id = f"{router.mac} {entity_description.name}"
54 
55  async def _async_set_state(self, enabled: bool) -> None:
56  """Turn the switch on or off."""
57  try:
58  await self._router_router.wifi.set_global_config({"enabled": enabled})
59  except InsufficientPermissionsError:
60  _LOGGER.warning(
61  "Home Assistant does not have permissions to modify the Freebox"
62  " settings. Please refer to documentation"
63  )
64 
65  async def async_turn_on(self, **kwargs: Any) -> None:
66  """Turn the switch on."""
67  await self._async_set_state_async_set_state(True)
68 
69  async def async_turn_off(self, **kwargs: Any) -> None:
70  """Turn the switch off."""
71  await self._async_set_state_async_set_state(False)
72 
73  async def async_update(self) -> None:
74  """Get the state and update it."""
75  data = await self._router_router.wifi.get_global_config()
76  self._attr_is_on_attr_is_on = bool(data["enabled"])
None __init__(self, FreeboxRouter router, SwitchEntityDescription entity_description)
Definition: switch.py:48
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:33