Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for turning on and off Pi-hole system."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from hole.exceptions import HoleError
9 import voluptuous as vol
10 
11 from homeassistant.components.switch import SwitchEntity
12 from homeassistant.const import CONF_NAME
13 from homeassistant.core import HomeAssistant
14 from homeassistant.helpers import config_validation as cv, entity_platform
15 from homeassistant.helpers.entity_platform import AddEntitiesCallback
16 
17 from . import PiHoleConfigEntry
18 from .const import SERVICE_DISABLE, SERVICE_DISABLE_ATTR_DURATION
19 from .entity import PiHoleEntity
20 
21 _LOGGER = logging.getLogger(__name__)
22 
23 
25  hass: HomeAssistant,
26  entry: PiHoleConfigEntry,
27  async_add_entities: AddEntitiesCallback,
28 ) -> None:
29  """Set up the Pi-hole switch."""
30  name = entry.data[CONF_NAME]
31  hole_data = entry.runtime_data
32  switches = [
34  hole_data.api,
35  hole_data.coordinator,
36  name,
37  entry.entry_id,
38  )
39  ]
40  async_add_entities(switches, True)
41 
42  # register service
43  platform = entity_platform.async_get_current_platform()
44  platform.async_register_entity_service(
45  SERVICE_DISABLE,
46  {
47  vol.Required(SERVICE_DISABLE_ATTR_DURATION): vol.All(
48  cv.time_period_str, cv.positive_timedelta
49  ),
50  },
51  "async_disable",
52  )
53 
54 
56  """Representation of a Pi-hole switch."""
57 
58  _attr_icon = "mdi:pi-hole"
59 
60  @property
61  def name(self) -> str:
62  """Return the name of the switch."""
63  return self._name_name
64 
65  @property
66  def unique_id(self) -> str:
67  """Return the unique id of the switch."""
68  return f"{self._server_unique_id}/Switch"
69 
70  @property
71  def is_on(self) -> bool:
72  """Return if the service is on."""
73  return self.apiapi.data.get("status") == "enabled" # type: ignore[no-any-return]
74 
75  async def async_turn_on(self, **kwargs: Any) -> None:
76  """Turn on the service."""
77  try:
78  await self.apiapi.enable()
79  await self.async_updateasync_update()
80  except HoleError as err:
81  _LOGGER.error("Unable to enable Pi-hole: %s", err)
82 
83  async def async_turn_off(self, **kwargs: Any) -> None:
84  """Turn off the service."""
85  await self.async_disableasync_disable()
86 
87  async def async_disable(self, duration: Any = None) -> None:
88  """Disable the service for a given duration."""
89  duration_seconds = True # Disable infinitely by default
90  if duration is not None:
91  duration_seconds = duration.total_seconds()
92  _LOGGER.debug(
93  "Disabling Pi-hole '%s' (%s) for %d seconds",
94  self.namenamenamename,
95  self.apiapi.host,
96  duration_seconds,
97  )
98  try:
99  await self.apiapi.disable(duration_seconds)
100  await self.async_updateasync_update()
101  except HoleError as err:
102  _LOGGER.error("Unable to disable Pi-hole: %s", err)
None async_disable(self, Any duration=None)
Definition: switch.py:87
str|UndefinedType|None name(self)
Definition: entity.py:738
None async_setup_entry(HomeAssistant hass, PiHoleConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:28