|
| 1 | +# Copyright (c) 2025, Renaud Allard <renaud@allard.it> |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# Redistribution and use in source and binary forms, with or without |
| 5 | +# modification, are permitted provided that the following conditions are met: |
| 6 | +# |
| 7 | +# 1. Redistributions of source code must retain the above copyright notice, |
| 8 | +# this list of conditions and the following disclaimer. |
| 9 | +# |
| 10 | +# 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | +# this list of conditions and the following disclaimer in the documentation |
| 12 | +# and/or other materials provided with the distribution. |
| 13 | +# |
| 14 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 15 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 17 | +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 18 | +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 19 | +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 20 | +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 21 | +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 22 | +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 23 | +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 24 | +# POSSIBILITY OF SUCH DAMAGE. |
| 25 | + |
| 26 | +"""Switch entities for enabling/disabling dnsdist backend servers.""" |
| 27 | + |
| 28 | +from __future__ import annotations |
| 29 | + |
| 30 | +import logging |
| 31 | +from typing import Any |
| 32 | + |
| 33 | +from homeassistant.components.switch import SwitchEntity |
| 34 | +from homeassistant.config_entries import ConfigEntry |
| 35 | +from homeassistant.core import HomeAssistant, callback |
| 36 | +from homeassistant.helpers.device_registry import DeviceInfo |
| 37 | +from homeassistant.helpers.entity import EntityCategory |
| 38 | +from homeassistant.helpers.entity_platform import AddEntitiesCallback |
| 39 | +from homeassistant.helpers import entity_registry as er |
| 40 | +from homeassistant.helpers.update_coordinator import CoordinatorEntity |
| 41 | + |
| 42 | +from .const import ATTR_BACKENDS, CONF_IS_GROUP, DOMAIN |
| 43 | +from .services import _call_dnsdist_api, _encode_backend_segment |
| 44 | +from .utils import build_device_info |
| 45 | + |
| 46 | +_LOGGER = logging.getLogger(__name__) |
| 47 | + |
| 48 | + |
| 49 | +async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback) -> None: |
| 50 | + """Set up dnsdist backend switches for a host.""" |
| 51 | + coordinator = hass.data[DOMAIN][entry.entry_id] |
| 52 | + is_group = bool(entry.data.get(CONF_IS_GROUP)) |
| 53 | + |
| 54 | + if is_group: |
| 55 | + return |
| 56 | + |
| 57 | + switch_entities: dict[str, DnsdistBackendSwitch] = {} |
| 58 | + |
| 59 | + @callback |
| 60 | + def _async_sync_switches() -> None: |
| 61 | + if not coordinator.data: |
| 62 | + return |
| 63 | + backends = coordinator.data.get(ATTR_BACKENDS) |
| 64 | + if not isinstance(backends, dict): |
| 65 | + backends = {} |
| 66 | + |
| 67 | + current_slugs = set(backends.keys()) |
| 68 | + known_slugs = set(switch_entities.keys()) |
| 69 | + |
| 70 | + removed_slugs = known_slugs - current_slugs |
| 71 | + ent_reg = er.async_get(hass) |
| 72 | + for slug in removed_slugs: |
| 73 | + entity = switch_entities.pop(slug, None) |
| 74 | + if entity: |
| 75 | + if entity.entity_id and ent_reg.async_get(entity.entity_id): |
| 76 | + ent_reg.async_remove(entity.entity_id) |
| 77 | + else: |
| 78 | + hass.async_create_task(entity.async_remove()) |
| 79 | + |
| 80 | + new_entities: list[DnsdistBackendSwitch] = [] |
| 81 | + for slug in current_slugs: |
| 82 | + if slug in switch_entities: |
| 83 | + continue |
| 84 | + entity = DnsdistBackendSwitch( |
| 85 | + coordinator=coordinator, |
| 86 | + entry_id=entry.entry_id, |
| 87 | + backend_slug=slug, |
| 88 | + ) |
| 89 | + switch_entities[slug] = entity |
| 90 | + new_entities.append(entity) |
| 91 | + |
| 92 | + if new_entities: |
| 93 | + async_add_entities(new_entities) |
| 94 | + |
| 95 | + _async_sync_switches() |
| 96 | + entry.async_on_unload(coordinator.async_add_listener(_async_sync_switches)) |
| 97 | + |
| 98 | + |
| 99 | +class DnsdistBackendSwitch(CoordinatorEntity, SwitchEntity): |
| 100 | + """Switch to enable/disable a dnsdist backend server.""" |
| 101 | + |
| 102 | + _attr_has_entity_name = False |
| 103 | + _attr_should_poll = False |
| 104 | + _attr_entity_category = EntityCategory.CONFIG |
| 105 | + _attr_icon = "mdi:server-network" |
| 106 | + |
| 107 | + def __init__(self, *, coordinator, entry_id: str, backend_slug: str) -> None: |
| 108 | + super().__init__(coordinator) |
| 109 | + self._slug = backend_slug |
| 110 | + self._attr_unique_id = f"{entry_id}:backend_switch:{backend_slug}" |
| 111 | + |
| 112 | + def _backend_data(self) -> dict[str, Any]: |
| 113 | + data = self.coordinator.data or {} |
| 114 | + backends = data.get(ATTR_BACKENDS, {}) if isinstance(data, dict) else {} |
| 115 | + if isinstance(backends, dict): |
| 116 | + return backends.get(self._slug, {}) |
| 117 | + return {} |
| 118 | + |
| 119 | + @property |
| 120 | + def name(self) -> str: |
| 121 | + host = getattr(self.coordinator, "_name", "dnsdist") |
| 122 | + backend = self._backend_data() |
| 123 | + address = backend.get("address") or self._slug |
| 124 | + name = backend.get("name") |
| 125 | + if name: |
| 126 | + return f"{host} Backend {name}" |
| 127 | + return f"{host} Backend {address}" |
| 128 | + |
| 129 | + @property |
| 130 | + def is_on(self) -> bool | None: |
| 131 | + backend = self._backend_data() |
| 132 | + if not backend: |
| 133 | + return None |
| 134 | + state = str(backend.get("state", "")).lower() |
| 135 | + # "off" means manually disabled via the API |
| 136 | + return state != "off" |
| 137 | + |
| 138 | + async def async_turn_on(self, **kwargs: Any) -> None: |
| 139 | + """Enable the backend server.""" |
| 140 | + backend = self._backend_data() |
| 141 | + address = backend.get("address", "") |
| 142 | + encoded = _encode_backend_segment(address) |
| 143 | + if not encoded: |
| 144 | + _LOGGER.warning("Cannot enable backend: invalid address %s", address) |
| 145 | + return |
| 146 | + await _call_dnsdist_api(self.coordinator, "PUT", f"/api/v1/servers/{encoded}/enable") |
| 147 | + await self.coordinator.async_request_refresh() |
| 148 | + |
| 149 | + async def async_turn_off(self, **kwargs: Any) -> None: |
| 150 | + """Disable the backend server.""" |
| 151 | + backend = self._backend_data() |
| 152 | + address = backend.get("address", "") |
| 153 | + encoded = _encode_backend_segment(address) |
| 154 | + if not encoded: |
| 155 | + _LOGGER.warning("Cannot disable backend: invalid address %s", address) |
| 156 | + return |
| 157 | + await _call_dnsdist_api(self.coordinator, "PUT", f"/api/v1/servers/{encoded}/disable") |
| 158 | + await self.coordinator.async_request_refresh() |
| 159 | + |
| 160 | + @property |
| 161 | + def extra_state_attributes(self) -> dict[str, Any]: |
| 162 | + backend = self._backend_data() |
| 163 | + attrs: dict[str, Any] = {} |
| 164 | + for key in ("address", "name", "state", "order", "weight", "pools"): |
| 165 | + if key in backend and backend[key] is not None: |
| 166 | + attrs[key] = backend[key] |
| 167 | + return attrs |
| 168 | + |
| 169 | + @property |
| 170 | + def device_info(self) -> DeviceInfo: |
| 171 | + return build_device_info(self.coordinator, False) |
0 commit comments