-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from Amateur-God/V2.2.0-Beta
V2.2.0 beta
- Loading branch information
Showing
5 changed files
with
81 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,56 @@ | ||
import logging | ||
from homeassistant.components.button import ButtonEntity | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.helpers.update_coordinator import CoordinatorEntity | ||
from homeassistant.helpers.entity import DeviceInfo | ||
|
||
from .const import DOMAIN, AD_BLOCKING_DURATION_OPTIONS | ||
from .api import TechnitiumDNSApi | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
async def async_setup_entry(hass: HomeAssistant, entry, async_add_entities): | ||
async def async_setup_entry(hass, entry, async_add_entities): | ||
"""Set up TechnitiumDNS button entities based on a config entry.""" | ||
api = TechnitiumDNSApi(entry.data["api_url"], entry.data["token"]) | ||
server_name = entry.data["server_name"] | ||
config_entry = hass.data[DOMAIN][entry.entry_id] | ||
api = config_entry["api"] | ||
server_name = config_entry["server_name"] | ||
|
||
# Ensure durations are sorted as integers | ||
sorted_durations = sorted(AD_BLOCKING_DURATION_OPTIONS.keys()) | ||
|
||
# Define the buttons using the sorted durations | ||
buttons = [ | ||
TechnitiumDNSButton( | ||
hass, api, AD_BLOCKING_DURATION_OPTIONS[duration], duration, server_name | ||
api, AD_BLOCKING_DURATION_OPTIONS[duration], duration, server_name, entry.entry_id | ||
) | ||
for duration in sorted_durations | ||
] | ||
|
||
# Add entities | ||
async_add_entities(buttons) | ||
|
||
|
||
class TechnitiumDNSButton(ButtonEntity): | ||
"""Representation of a TechnitiumDNS button.""" | ||
|
||
def __init__( | ||
self, | ||
hass: HomeAssistant, | ||
api: TechnitiumDNSApi, | ||
name: str, | ||
duration: int, | ||
server_name: str, | ||
): | ||
def __init__(self, api: TechnitiumDNSApi, name: str, duration: int, server_name: str, entry_id: str): | ||
"""Initialize the button.""" | ||
self._hass = hass | ||
self._api = api | ||
self._attr_name = f"{name} ({server_name})" | ||
self._duration = duration | ||
self._entry_id = entry_id | ||
|
||
async def async_press(self) -> None: | ||
"""Handle the button press.""" | ||
try: | ||
await self._api.temporary_disable_blocking(self._duration) | ||
_LOGGER.info( | ||
f"Ad blocking disabled for {self._duration} minutes on {self._attr_name}" | ||
) | ||
_LOGGER.info(f"Ad blocking disabled for {self._duration} minutes on {self._attr_name}") | ||
except Exception as e: | ||
_LOGGER.error(f"Failed to disable ad blocking: {e}") | ||
|
||
@property | ||
def device_info(self): | ||
"""Return device information for this entity.""" | ||
return DeviceInfo( | ||
identifiers={(DOMAIN, self._entry_id)}, | ||
name=self._attr_name, | ||
manufacturer="Technitium", | ||
model="DNS Server", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters