1 """Support for turning on and off Pi-hole system."""
3 from __future__
import annotations
8 from hole.exceptions
import HoleError
9 import voluptuous
as vol
17 from .
import PiHoleConfigEntry
18 from .const
import SERVICE_DISABLE, SERVICE_DISABLE_ATTR_DURATION
19 from .entity
import PiHoleEntity
21 _LOGGER = logging.getLogger(__name__)
26 entry: PiHoleConfigEntry,
27 async_add_entities: AddEntitiesCallback,
29 """Set up the Pi-hole switch."""
30 name = entry.data[CONF_NAME]
31 hole_data = entry.runtime_data
35 hole_data.coordinator,
43 platform = entity_platform.async_get_current_platform()
44 platform.async_register_entity_service(
47 vol.Required(SERVICE_DISABLE_ATTR_DURATION): vol.All(
48 cv.time_period_str, cv.positive_timedelta
56 """Representation of a Pi-hole switch."""
58 _attr_icon =
"mdi:pi-hole"
62 """Return the name of the switch."""
63 return self.
_name_name
67 """Return the unique id of the switch."""
68 return f
"{self._server_unique_id}/Switch"
72 """Return if the service is on."""
73 return self.
apiapi.data.get(
"status") ==
"enabled"
76 """Turn on the service."""
80 except HoleError
as err:
81 _LOGGER.error(
"Unable to enable Pi-hole: %s", err)
84 """Turn off the service."""
88 """Disable the service for a given duration."""
89 duration_seconds =
True
90 if duration
is not None:
91 duration_seconds = duration.total_seconds()
93 "Disabling Pi-hole '%s' (%s) for %d seconds",
101 except HoleError
as err:
102 _LOGGER.error(
"Unable to disable Pi-hole: %s", err)
None async_disable(self, Any duration=None)
None async_turn_off(self, **Any kwargs)
None async_turn_on(self, **Any kwargs)
str|UndefinedType|None name(self)
None async_setup_entry(HomeAssistant hass, PiHoleConfigEntry entry, AddEntitiesCallback async_add_entities)