Skip to content

Commit

Permalink
Store runtime data inside the config entry
Browse files Browse the repository at this point in the history
  • Loading branch information
cyr-ius committed May 22, 2024
1 parent 250ecfd commit 211dc2d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
17 changes: 8 additions & 9 deletions custom_components/pdns/__init__.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
"""Integrate with PowerDNS service."""

from __future__ import annotations

import logging

from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant

from .const import DOMAIN, PLATFORMS
from .const import PLATFORMS
from .coordinator import PDNSDataUpdateCoordinator

type PDNSConfigEtry = ConfigEntry[PDNSDataUpdateCoordinator]

_LOGGER = logging.getLogger(__name__)


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_setup_entry(hass: HomeAssistant, entry: PDNSConfigEtry) -> bool:
"""Initialize the component."""
hass.data.setdefault(DOMAIN, {})

coordinator = PDNSDataUpdateCoordinator(hass, entry)
await coordinator.async_config_entry_first_refresh()
entry.runtime_data = coordinator

hass.data[DOMAIN][entry.entry_id] = coordinator
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

return True


async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_unload_entry(hass: HomeAssistant, entry: PDNSConfigEtry) -> bool:
"""Unload a config entry."""
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
10 changes: 4 additions & 6 deletions custom_components/pdns/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
"""binary sensor entities."""

from __future__ import annotations

from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
BinarySensorEntity,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity

from . import DOMAIN, PDNSDataUpdateCoordinator
from . import PDNSConfigEtry, PDNSDataUpdateCoordinator


async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
hass: HomeAssistant, entry: PDNSConfigEtry, async_add_entities: AddEntitiesCallback
) -> None:
"""Defer binary sensor setup to the shared sensor module."""
coordinator = hass.data[DOMAIN][config_entry.entry_id]
coordinator = entry.runtime_data
async_add_entities([DyndnsStatus(coordinator)])


Expand Down

0 comments on commit 211dc2d

Please sign in to comment.