Store runtime data inside the config entry in Upnp (#117030)
store runtime data inside the config entry
This commit is contained in:
parent
35d44ec90a
commit
7f7d025b44
3 changed files with 12 additions and 21 deletions
|
@ -36,13 +36,13 @@ PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR]
|
||||||
|
|
||||||
CONFIG_SCHEMA = cv.removed(DOMAIN, raise_if_present=False)
|
CONFIG_SCHEMA = cv.removed(DOMAIN, raise_if_present=False)
|
||||||
|
|
||||||
|
UpnpConfigEntry = ConfigEntry[UpnpDataUpdateCoordinator]
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
||||||
|
async def async_setup_entry(hass: HomeAssistant, entry: UpnpConfigEntry) -> bool:
|
||||||
"""Set up UPnP/IGD device from a config entry."""
|
"""Set up UPnP/IGD device from a config entry."""
|
||||||
LOGGER.debug("Setting up config entry: %s", entry.entry_id)
|
LOGGER.debug("Setting up config entry: %s", entry.entry_id)
|
||||||
|
|
||||||
hass.data.setdefault(DOMAIN, {})
|
|
||||||
|
|
||||||
udn = entry.data[CONFIG_ENTRY_UDN]
|
udn = entry.data[CONFIG_ENTRY_UDN]
|
||||||
st = entry.data[CONFIG_ENTRY_ST]
|
st = entry.data[CONFIG_ENTRY_ST]
|
||||||
usn = f"{udn}::{st}"
|
usn = f"{udn}::{st}"
|
||||||
|
@ -168,7 +168,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
await coordinator.async_config_entry_first_refresh()
|
await coordinator.async_config_entry_first_refresh()
|
||||||
|
|
||||||
# Save coordinator.
|
# Save coordinator.
|
||||||
hass.data[DOMAIN][entry.entry_id] = coordinator
|
entry.runtime_data = coordinator
|
||||||
|
|
||||||
# Setup platforms, creating sensors/binary_sensors.
|
# Setup platforms, creating sensors/binary_sensors.
|
||||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||||
|
@ -179,10 +179,4 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
"""Unload a UPnP/IGD device from a config entry."""
|
"""Unload a UPnP/IGD device from a config entry."""
|
||||||
LOGGER.debug("Unloading config entry: %s", entry.entry_id)
|
LOGGER.debug("Unloading config entry: %s", entry.entry_id)
|
||||||
|
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||||
# Unload platforms.
|
|
||||||
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
|
||||||
if unload_ok:
|
|
||||||
del hass.data[DOMAIN][entry.entry_id]
|
|
||||||
|
|
||||||
return unload_ok
|
|
||||||
|
|
|
@ -9,13 +9,12 @@ from homeassistant.components.binary_sensor import (
|
||||||
BinarySensorEntity,
|
BinarySensorEntity,
|
||||||
BinarySensorEntityDescription,
|
BinarySensorEntityDescription,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import EntityCategory
|
from homeassistant.const import EntityCategory
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from . import UpnpDataUpdateCoordinator
|
from . import UpnpConfigEntry, UpnpDataUpdateCoordinator
|
||||||
from .const import DOMAIN, LOGGER, WAN_STATUS
|
from .const import LOGGER, WAN_STATUS
|
||||||
from .entity import UpnpEntity, UpnpEntityDescription
|
from .entity import UpnpEntity, UpnpEntityDescription
|
||||||
|
|
||||||
|
|
||||||
|
@ -38,11 +37,11 @@ SENSOR_DESCRIPTIONS: tuple[UpnpBinarySensorEntityDescription, ...] = (
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: ConfigEntry,
|
config_entry: UpnpConfigEntry,
|
||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the UPnP/IGD sensors."""
|
"""Set up the UPnP/IGD sensors."""
|
||||||
coordinator: UpnpDataUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id]
|
coordinator = config_entry.runtime_data
|
||||||
|
|
||||||
entities = [
|
entities = [
|
||||||
UpnpStatusBinarySensor(
|
UpnpStatusBinarySensor(
|
||||||
|
|
|
@ -11,7 +11,6 @@ from homeassistant.components.sensor import (
|
||||||
SensorEntityDescription,
|
SensorEntityDescription,
|
||||||
SensorStateClass,
|
SensorStateClass,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
EntityCategory,
|
EntityCategory,
|
||||||
UnitOfDataRate,
|
UnitOfDataRate,
|
||||||
|
@ -21,12 +20,12 @@ from homeassistant.const import (
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
|
from . import UpnpConfigEntry
|
||||||
from .const import (
|
from .const import (
|
||||||
BYTES_RECEIVED,
|
BYTES_RECEIVED,
|
||||||
BYTES_SENT,
|
BYTES_SENT,
|
||||||
DATA_PACKETS,
|
DATA_PACKETS,
|
||||||
DATA_RATE_PACKETS_PER_SECOND,
|
DATA_RATE_PACKETS_PER_SECOND,
|
||||||
DOMAIN,
|
|
||||||
KIBIBYTES_PER_SEC_RECEIVED,
|
KIBIBYTES_PER_SEC_RECEIVED,
|
||||||
KIBIBYTES_PER_SEC_SENT,
|
KIBIBYTES_PER_SEC_SENT,
|
||||||
LOGGER,
|
LOGGER,
|
||||||
|
@ -38,7 +37,6 @@ from .const import (
|
||||||
ROUTER_UPTIME,
|
ROUTER_UPTIME,
|
||||||
WAN_STATUS,
|
WAN_STATUS,
|
||||||
)
|
)
|
||||||
from .coordinator import UpnpDataUpdateCoordinator
|
|
||||||
from .entity import UpnpEntity, UpnpEntityDescription
|
from .entity import UpnpEntity, UpnpEntityDescription
|
||||||
|
|
||||||
|
|
||||||
|
@ -146,11 +144,11 @@ SENSOR_DESCRIPTIONS: tuple[UpnpSensorEntityDescription, ...] = (
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: ConfigEntry,
|
config_entry: UpnpConfigEntry,
|
||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the UPnP/IGD sensors."""
|
"""Set up the UPnP/IGD sensors."""
|
||||||
coordinator: UpnpDataUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id]
|
coordinator = config_entry.runtime_data
|
||||||
|
|
||||||
entities: list[UpnpSensor] = [
|
entities: list[UpnpSensor] = [
|
||||||
UpnpSensor(
|
UpnpSensor(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue