Use runtime data for FritzBox Call Monitor (#116553)
This commit is contained in:
parent
7fd60ddba4
commit
a25e202ef0
3 changed files with 18 additions and 38 deletions
|
@ -11,19 +11,16 @@ from homeassistant.core import HomeAssistant
|
|||
from homeassistant.exceptions import ConfigEntryNotReady
|
||||
|
||||
from .base import FritzBoxPhonebook
|
||||
from .const import (
|
||||
CONF_PHONEBOOK,
|
||||
CONF_PREFIXES,
|
||||
DOMAIN,
|
||||
FRITZBOX_PHONEBOOK,
|
||||
PLATFORMS,
|
||||
UNDO_UPDATE_LISTENER,
|
||||
)
|
||||
from .const import CONF_PHONEBOOK, CONF_PREFIXES, PLATFORMS
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
FritzBoxCallMonitorConfigEntry = ConfigEntry[FritzBoxPhonebook]
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant, config_entry: FritzBoxCallMonitorConfigEntry
|
||||
) -> bool:
|
||||
"""Set up the fritzbox_callmonitor platforms."""
|
||||
fritzbox_phonebook = FritzBoxPhonebook(
|
||||
host=config_entry.data[CONF_HOST],
|
||||
|
@ -51,34 +48,22 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
|
|||
_LOGGER.error("Unable to connect to AVM FRITZ!Box call monitor: %s", ex)
|
||||
raise ConfigEntryNotReady from ex
|
||||
|
||||
undo_listener = config_entry.add_update_listener(update_listener)
|
||||
|
||||
hass.data.setdefault(DOMAIN, {})
|
||||
hass.data[DOMAIN][config_entry.entry_id] = {
|
||||
FRITZBOX_PHONEBOOK: fritzbox_phonebook,
|
||||
UNDO_UPDATE_LISTENER: undo_listener,
|
||||
}
|
||||
|
||||
config_entry.runtime_data = fritzbox_phonebook
|
||||
config_entry.async_on_unload(config_entry.add_update_listener(update_listener))
|
||||
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
||||
async def async_unload_entry(
|
||||
hass: HomeAssistant, config_entry: FritzBoxCallMonitorConfigEntry
|
||||
) -> bool:
|
||||
"""Unloading the fritzbox_callmonitor platforms."""
|
||||
|
||||
unload_ok = await hass.config_entries.async_unload_platforms(
|
||||
config_entry, PLATFORMS
|
||||
)
|
||||
|
||||
hass.data[DOMAIN][config_entry.entry_id][UNDO_UPDATE_LISTENER]()
|
||||
|
||||
if unload_ok:
|
||||
hass.data[DOMAIN].pop(config_entry.entry_id)
|
||||
|
||||
return unload_ok
|
||||
return await hass.config_entries.async_unload_platforms(config_entry, PLATFORMS)
|
||||
|
||||
|
||||
async def update_listener(hass: HomeAssistant, config_entry: ConfigEntry) -> None:
|
||||
async def update_listener(
|
||||
hass: HomeAssistant, config_entry: FritzBoxCallMonitorConfigEntry
|
||||
) -> None:
|
||||
"""Update listener to reload after option has changed."""
|
||||
await hass.config_entries.async_reload(config_entry.entry_id)
|
||||
|
|
|
@ -38,5 +38,3 @@ DOMAIN: Final = "fritzbox_callmonitor"
|
|||
MANUFACTURER: Final = "AVM"
|
||||
|
||||
PLATFORMS = [Platform.SENSOR]
|
||||
UNDO_UPDATE_LISTENER: Final = "undo_update_listener"
|
||||
FRITZBOX_PHONEBOOK: Final = "fritzbox_phonebook"
|
||||
|
|
|
@ -14,19 +14,18 @@ from typing import Any, cast
|
|||
from fritzconnection.core.fritzmonitor import FritzMonitor
|
||||
|
||||
from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_HOST, CONF_PORT, EVENT_HOMEASSISTANT_STOP
|
||||
from homeassistant.core import Event, HomeAssistant
|
||||
from homeassistant.helpers.device_registry import DeviceInfo
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import FritzBoxCallMonitorConfigEntry
|
||||
from .base import FritzBoxPhonebook
|
||||
from .const import (
|
||||
ATTR_PREFIXES,
|
||||
CONF_PHONEBOOK,
|
||||
CONF_PREFIXES,
|
||||
DOMAIN,
|
||||
FRITZBOX_PHONEBOOK,
|
||||
MANUFACTURER,
|
||||
SERIAL_NUMBER,
|
||||
FritzState,
|
||||
|
@ -48,13 +47,11 @@ class CallState(StrEnum):
|
|||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: FritzBoxCallMonitorConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up the fritzbox_callmonitor sensor from config_entry."""
|
||||
fritzbox_phonebook: FritzBoxPhonebook = hass.data[DOMAIN][config_entry.entry_id][
|
||||
FRITZBOX_PHONEBOOK
|
||||
]
|
||||
fritzbox_phonebook = config_entry.runtime_data
|
||||
|
||||
phonebook_id: int = config_entry.data[CONF_PHONEBOOK]
|
||||
prefixes: list[str] | None = config_entry.options.get(CONF_PREFIXES)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue