From 47b708974d060bfe098f420d991b7acbb136c414 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Fri, 24 Jan 2020 00:50:59 +0100 Subject: [PATCH] Add disabled entities support to AdGuard (#31106) --- homeassistant/components/adguard/__init__.py | 17 ++++++++++++++--- homeassistant/components/adguard/sensor.py | 18 +++++++++++++++--- homeassistant/components/adguard/switch.py | 18 +++++++++++++----- 3 files changed, 42 insertions(+), 11 deletions(-) diff --git a/homeassistant/components/adguard/__init__.py b/homeassistant/components/adguard/__init__.py index 1f4d63d627b..6996a2b0d51 100644 --- a/homeassistant/components/adguard/__init__.py +++ b/homeassistant/components/adguard/__init__.py @@ -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 diff --git a/homeassistant/components/adguard/sensor.py b/homeassistant/components/adguard/sensor.py index c818752ad2f..e5618282a97 100644 --- a/homeassistant/components/adguard/sensor.py +++ b/homeassistant/components/adguard/sensor.py @@ -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: diff --git a/homeassistant/components/adguard/switch.py b/homeassistant/components/adguard/switch.py index 39cd1ef028d..1ddefb3367b 100644 --- a/homeassistant/components/adguard/switch.py +++ b/homeassistant/components/adguard/switch.py @@ -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."""