80
custom_components/adguard_control_hub/binary_sensor.py
Normal file
80
custom_components/adguard_control_hub/binary_sensor.py
Normal file
@@ -0,0 +1,80 @@
|
||||
"""AdGuard Control Hub binary sensor platform."""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
from homeassistant.components.binary_sensor import BinarySensorEntity
|
||||
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
|
||||
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 binary sensor based on a config entry."""
|
||||
coordinator = hass.data[DOMAIN][entry.entry_id]
|
||||
|
||||
entities = [
|
||||
AdGuardBinarySensor(
|
||||
coordinator,
|
||||
entry,
|
||||
"running",
|
||||
"AdGuard Home Running",
|
||||
"mdi:shield-check-outline",
|
||||
),
|
||||
]
|
||||
|
||||
async_add_entities(entities)
|
||||
|
||||
|
||||
class AdGuardBinarySensor(CoordinatorEntity[AdGuardDataUpdateCoordinator], BinarySensorEntity):
|
||||
"""Representation of an AdGuard Control Hub binary sensor."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: AdGuardDataUpdateCoordinator,
|
||||
entry: ConfigEntry,
|
||||
sensor_type: str,
|
||||
name: str,
|
||||
icon: str,
|
||||
) -> None:
|
||||
"""Initialize the binary sensor."""
|
||||
super().__init__(coordinator)
|
||||
self._sensor_type = sensor_type
|
||||
self._entry = entry
|
||||
|
||||
self._attr_name = name
|
||||
self._attr_icon = icon
|
||||
self._attr_unique_id = f"{entry.entry_id}_{sensor_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 the binary sensor is on."""
|
||||
if self.coordinator.data is None:
|
||||
return None
|
||||
|
||||
# If we can fetch data, AdGuard Home is running
|
||||
return self.coordinator.data.get("status") is not None
|
||||
Reference in New Issue
Block a user