91
custom_components/adguard_control_hub/switch.py
Normal file
91
custom_components/adguard_control_hub/switch.py
Normal file
@@ -0,0 +1,91 @@
|
||||
"""AdGuard Control Hub switch platform."""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
from homeassistant.components.switch import SwitchEntity
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity import DeviceInfo
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
|
||||
from .const import DOMAIN, SWITCHES
|
||||
from .coordinator import AdGuardDataUpdateCoordinator
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: ConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up AdGuard Control Hub switch based on a config entry."""
|
||||
coordinator = hass.data[DOMAIN][entry.entry_id]
|
||||
|
||||
entities = []
|
||||
for switch_type, switch_info in SWITCHES.items():
|
||||
entities.append(
|
||||
AdGuardSwitch(
|
||||
coordinator,
|
||||
entry,
|
||||
switch_type,
|
||||
switch_info,
|
||||
)
|
||||
)
|
||||
|
||||
async_add_entities(entities)
|
||||
|
||||
|
||||
class AdGuardSwitch(CoordinatorEntity[AdGuardDataUpdateCoordinator], SwitchEntity):
|
||||
"""Representation of an AdGuard Control Hub switch."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: AdGuardDataUpdateCoordinator,
|
||||
entry: ConfigEntry,
|
||||
switch_type: str,
|
||||
switch_info: dict[str, Any],
|
||||
) -> None:
|
||||
"""Initialize the switch."""
|
||||
super().__init__(coordinator)
|
||||
self._switch_type = switch_type
|
||||
self._switch_info = switch_info
|
||||
self._entry = entry
|
||||
|
||||
self._attr_name = switch_info["name"]
|
||||
self._attr_icon = switch_info["icon"]
|
||||
self._attr_unique_id = f"{entry.entry_id}_{switch_type}"
|
||||
|
||||
@property
|
||||
def device_info(self) -> DeviceInfo:
|
||||
"""Return device information about this AdGuard Home instance."""
|
||||
return DeviceInfo(
|
||||
identifiers={(DOMAIN, self._entry.entry_id)},
|
||||
name="AdGuard Control Hub",
|
||||
manufacturer="AdGuard",
|
||||
model="AdGuard Home",
|
||||
sw_version=self.coordinator.data.get("status", {}).get("version"),
|
||||
configuration_url=f"http://{self._entry.data['host']}:{self._entry.data['port']}",
|
||||
)
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool | None:
|
||||
"""Return True if entity is on."""
|
||||
if self.coordinator.data is None:
|
||||
return None
|
||||
|
||||
status_data = self.coordinator.data.get("status", {})
|
||||
api_key = self._switch_info["api_key"]
|
||||
|
||||
return status_data.get(api_key, False)
|
||||
|
||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||
"""Turn the entity on."""
|
||||
await self.coordinator.async_set_switch(self._switch_type, True)
|
||||
|
||||
async def async_turn_off(self, **kwargs: Any) -> None:
|
||||
"""Turn the entity off."""
|
||||
await self.coordinator.async_set_switch(self._switch_type, False)
|
||||
Reference in New Issue
Block a user