Add disabled entities support to AdGuard ()

This commit is contained in:
Franck Nijhof 2020-01-24 00:50:59 +01:00 committed by Martin Hjelmare
parent 7fed328e1c
commit 47b708974d
3 changed files with 42 additions and 11 deletions
homeassistant/components/adguard

View file

@ -142,11 +142,14 @@ async def async_unload_entry(hass: HomeAssistantType, entry: ConfigType) -> bool
class AdGuardHomeEntity(Entity):
"""Defines a base AdGuard Home entity."""
def __init__(self, adguard, name: str, icon: str) -> None:
def __init__(
self, adguard, name: str, icon: str, enabled_default: bool = True
) -> None:
"""Initialize the AdGuard Home entity."""
self._name = name
self._icon = icon
self._available = True
self._enabled_default = enabled_default
self._icon = icon
self._name = name
self.adguard = adguard
@property
@ -159,6 +162,11 @@ class AdGuardHomeEntity(Entity):
"""Return the mdi icon of the entity."""
return self._icon
@property
def entity_registry_enabled_default(self) -> bool:
"""Return if the entity should be enabled when first added to the entity registry."""
return self._enabled_default
@property
def available(self) -> bool:
"""Return True if entity is available."""
@ -166,6 +174,9 @@ class AdGuardHomeEntity(Entity):
async def async_update(self) -> None:
"""Update AdGuard Home entity."""
if not self.enabled:
return
try:
await self._adguard_update()
self._available = True

View file

@ -51,14 +51,20 @@ class AdGuardHomeSensor(AdGuardHomeDeviceEntity):
"""Defines a AdGuard Home sensor."""
def __init__(
self, adguard, name: str, icon: str, measurement: str, unit_of_measurement: str
self,
adguard,
name: str,
icon: str,
measurement: str,
unit_of_measurement: str,
enabled_default: bool = True,
) -> None:
"""Initialize AdGuard Home sensor."""
self._state = None
self._unit_of_measurement = unit_of_measurement
self.measurement = measurement
super().__init__(adguard, name, icon)
super().__init__(adguard, name, icon, enabled_default)
@property
def unique_id(self) -> str:
@ -109,6 +115,7 @@ class AdGuardHomeBlockedFilteringSensor(AdGuardHomeSensor):
"mdi:magnify-close",
"blocked_filtering",
"queries",
enabled_default=False,
)
async def _adguard_update(self) -> None:
@ -214,7 +221,12 @@ class AdGuardHomeRulesCountSensor(AdGuardHomeSensor):
def __init__(self, adguard):
"""Initialize AdGuard Home sensor."""
super().__init__(
adguard, "AdGuard Rules Count", "mdi:counter", "rules_count", "rules"
adguard,
"AdGuard Rules Count",
"mdi:counter",
"rules_count",
"rules",
enabled_default=False,
)
async def _adguard_update(self) -> None:

View file

@ -10,9 +10,9 @@ from homeassistant.components.adguard.const import (
DATA_ADGUARD_VERION,
DOMAIN,
)
from homeassistant.components.switch import SwitchDevice
from homeassistant.config_entries import ConfigEntry
from homeassistant.exceptions import PlatformNotReady
from homeassistant.helpers.entity import ToggleEntity
from homeassistant.helpers.typing import HomeAssistantType
_LOGGER = logging.getLogger(__name__)
@ -45,14 +45,16 @@ async def async_setup_entry(
async_add_entities(switches, True)
class AdGuardHomeSwitch(ToggleEntity, AdGuardHomeDeviceEntity):
class AdGuardHomeSwitch(AdGuardHomeDeviceEntity, SwitchDevice):
"""Defines a AdGuard Home switch."""
def __init__(self, adguard, name: str, icon: str, key: str):
def __init__(
self, adguard, name: str, icon: str, key: str, enabled_default: bool = True
):
"""Initialize AdGuard Home switch."""
self._state = False
self._key = key
super().__init__(adguard, name, icon)
super().__init__(adguard, name, icon, enabled_default)
@property
def unique_id(self) -> str:
@ -204,7 +206,13 @@ class AdGuardHomeQueryLogSwitch(AdGuardHomeSwitch):
def __init__(self, adguard) -> None:
"""Initialize AdGuard Home switch."""
super().__init__(adguard, "AdGuard Query Log", "mdi:shield-check", "querylog")
super().__init__(
adguard,
"AdGuard Query Log",
"mdi:shield-check",
"querylog",
enabled_default=False,
)
async def _adguard_turn_off(self) -> None:
"""Turn off the switch."""