Add disabled entities support to AdGuard (#31106)

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

View File

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

View File

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

View File

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