forked from GuilleGF/hassio-ovh
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix coordinator_context (> HA 2022.07.0)
- Loading branch information
Showing
3 changed files
with
59 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,75 @@ | ||
"""Integrate with PowerDNS service.""" | ||
from __future__ import annotations | ||
|
||
import logging | ||
from datetime import timedelta | ||
|
||
from homeassistant.config_entries import ConfigEntry | ||
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.helpers.aiohttp_client import async_create_clientsession | ||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed | ||
|
||
from .pdns import PDNS, CannotConnect, TimeoutExpired, PDNSFailed, DetectionFailed | ||
from .pdns import PDNS, PDNSFailed | ||
|
||
DEFAULT_INTERVAL = 15 | ||
DOMAIN = "pdns" | ||
CONF_PDNSSRV = "pdns_server" | ||
CONF_ALIAS = "dns_alias" | ||
_LOGGER = logging.getLogger(__name__) | ||
PLATFORMS = ["binary_sensor"] | ||
|
||
|
||
async def async_setup(hass, config): | ||
"""Load configuration for component.""" | ||
hass.data.setdefault(DOMAIN, {}) | ||
return True | ||
|
||
|
||
async def async_setup_entry(hass, config_entry): | ||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: | ||
"""Initialize the component.""" | ||
alias = config_entry.data.get(CONF_ALIAS) | ||
username = config_entry.data.get(CONF_USERNAME) | ||
password = config_entry.data.get(CONF_PASSWORD) | ||
servername = config_entry.data.get(CONF_PDNSSRV) | ||
|
||
session = async_create_clientsession(hass) | ||
client = PDNS(servername, alias, username, password, session) | ||
hass.data.setdefault(DOMAIN, {}) | ||
|
||
coordinator = DataUpdateCoordinator( | ||
coordinator = PDNSDataUpdateCoordinator( | ||
hass, | ||
_LOGGER, | ||
name=DOMAIN, | ||
update_method=client.async_update, | ||
update_interval=timedelta(minutes=DEFAULT_INTERVAL), | ||
entry, | ||
entry.data.get(CONF_PDNSSRV), | ||
entry.data.get(CONF_ALIAS), | ||
entry.data.get(CONF_USERNAME), | ||
entry.data.get(CONF_PASSWORD), | ||
) | ||
await coordinator.async_config_entry_first_refresh() | ||
if coordinator.data is None: | ||
return False | ||
|
||
hass.data[DOMAIN] = coordinator | ||
hass.async_create_task( | ||
hass.config_entries.async_forward_entry_setup(config_entry, "binary_sensor") | ||
) | ||
hass.data[DOMAIN][entry.entry_id] = coordinator | ||
hass.config_entries.async_setup_platforms(entry, PLATFORMS) | ||
|
||
return True | ||
|
||
|
||
async def async_unload_entry(hass, config_entry): | ||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: | ||
"""Unload a config entry.""" | ||
await hass.config_entries.async_forward_entry_unload(config_entry, "binary_sensor") | ||
return True | ||
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS): | ||
hass.data[DOMAIN].pop(entry.entry_id) | ||
return unload_ok | ||
|
||
|
||
class PDNSDataUpdateCoordinator(DataUpdateCoordinator): | ||
"""Define an object to fetch datas.""" | ||
|
||
def __init__( | ||
self, | ||
hass: HomeAssistant, | ||
config_entry: ConfigEntry, | ||
servername: str, | ||
alias: str, | ||
username: str, | ||
password: str, | ||
) -> None: | ||
"""Class to manage fetching data API.""" | ||
super().__init__( | ||
hass, _LOGGER, name=DOMAIN, update_interval=timedelta(DEFAULT_INTERVAL) | ||
) | ||
session = async_create_clientsession(hass) | ||
self.pdns_client = PDNS(servername, alias, username, password, session) | ||
|
||
async def _async_update_data(self) -> dict: | ||
try: | ||
return await self.pdns_client.async_update() | ||
except PDNSFailed as error: | ||
raise UpdateFailed(error) from error |
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