Fix upnp error on unload_entry if device does not exist (#38230)
parent
e76db60367
commit
c33f309d5f
|
@ -20,6 +20,10 @@ from .const import (
|
|||
DISCOVERY_UDN,
|
||||
DISCOVERY_USN,
|
||||
DOMAIN,
|
||||
DOMAIN_CONFIG,
|
||||
DOMAIN_COORDINATORS,
|
||||
DOMAIN_DEVICES,
|
||||
DOMAIN_LOCAL_IP,
|
||||
LOGGER as _LOGGER,
|
||||
)
|
||||
from .device import Device
|
||||
|
@ -78,10 +82,10 @@ async def async_setup(hass: HomeAssistantType, config: ConfigType):
|
|||
conf = config.get(DOMAIN, conf_default)
|
||||
local_ip = await hass.async_add_executor_job(get_local_ip)
|
||||
hass.data[DOMAIN] = {
|
||||
"config": conf,
|
||||
"devices": {},
|
||||
"coordinators": {},
|
||||
"local_ip": conf.get(CONF_LOCAL_IP, local_ip),
|
||||
DOMAIN_CONFIG: conf,
|
||||
DOMAIN_COORDINATORS: {},
|
||||
DOMAIN_DEVICES: {},
|
||||
DOMAIN_LOCAL_IP: conf.get(CONF_LOCAL_IP, local_ip),
|
||||
}
|
||||
|
||||
# Only start if set up via configuration.yaml.
|
||||
|
@ -108,7 +112,7 @@ async def async_setup_entry(hass: HomeAssistantType, config_entry: ConfigEntry)
|
|||
raise ConfigEntryNotReady
|
||||
|
||||
# Save device
|
||||
hass.data[DOMAIN]["devices"][device.udn] = device
|
||||
hass.data[DOMAIN][DOMAIN_DEVICES][device.udn] = device
|
||||
|
||||
# Ensure entry has proper unique_id.
|
||||
if config_entry.unique_id != device.unique_id:
|
||||
|
@ -141,8 +145,10 @@ async def async_unload_entry(
|
|||
) -> bool:
|
||||
"""Unload a UPnP/IGD device from a config entry."""
|
||||
udn = config_entry.data.get(CONFIG_ENTRY_UDN)
|
||||
del hass.data[DOMAIN]["devices"][udn]
|
||||
del hass.data[DOMAIN]["coordinators"][udn]
|
||||
if udn in hass.data[DOMAIN][DOMAIN_DEVICES]:
|
||||
del hass.data[DOMAIN][DOMAIN_DEVICES][udn]
|
||||
if udn in hass.data[DOMAIN][DOMAIN_COORDINATORS]:
|
||||
del hass.data[DOMAIN][DOMAIN_COORDINATORS][udn]
|
||||
|
||||
_LOGGER.debug("Deleting sensors")
|
||||
return await hass.config_entries.async_forward_entry_unload(config_entry, "sensor")
|
||||
|
|
|
@ -20,6 +20,7 @@ from .const import ( # pylint: disable=unused-import
|
|||
DISCOVERY_UDN,
|
||||
DISCOVERY_USN,
|
||||
DOMAIN,
|
||||
DOMAIN_COORDINATORS,
|
||||
LOGGER as _LOGGER,
|
||||
)
|
||||
from .device import Device
|
||||
|
@ -221,7 +222,7 @@ class UpnpOptionsFlowHandler(config_entries.OptionsFlow):
|
|||
"""Manage the options."""
|
||||
if user_input is not None:
|
||||
udn = self.config_entry.data.get(CONFIG_ENTRY_UDN)
|
||||
coordinator = self.hass.data[DOMAIN]["coordinators"][udn]
|
||||
coordinator = self.hass.data[DOMAIN][DOMAIN_COORDINATORS][udn]
|
||||
update_interval_sec = user_input.get(
|
||||
CONFIG_ENTRY_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL
|
||||
)
|
||||
|
|
|
@ -4,9 +4,14 @@ import logging
|
|||
|
||||
from homeassistant.const import TIME_SECONDS
|
||||
|
||||
LOGGER = logging.getLogger(__package__)
|
||||
|
||||
CONF_LOCAL_IP = "local_ip"
|
||||
DOMAIN = "upnp"
|
||||
LOGGER = logging.getLogger(__package__)
|
||||
DOMAIN_COORDINATORS = "coordinators"
|
||||
DOMAIN_DEVICES = "devices"
|
||||
DOMAIN_LOCAL_IP = "local_ip"
|
||||
DOMAIN_CONFIG = "config"
|
||||
BYTES_RECEIVED = "bytes_received"
|
||||
BYTES_SENT = "bytes_sent"
|
||||
PACKETS_RECEIVED = "packets_received"
|
||||
|
|
|
@ -20,6 +20,7 @@ from .const import (
|
|||
DISCOVERY_UDN,
|
||||
DISCOVERY_USN,
|
||||
DOMAIN,
|
||||
DOMAIN_CONFIG,
|
||||
LOGGER as _LOGGER,
|
||||
PACKETS_RECEIVED,
|
||||
PACKETS_SENT,
|
||||
|
@ -40,8 +41,8 @@ class Device:
|
|||
"""Discover UPnP/IGD devices."""
|
||||
_LOGGER.debug("Discovering UPnP/IGD devices")
|
||||
local_ip = None
|
||||
if DOMAIN in hass.data and "config" in hass.data[DOMAIN]:
|
||||
local_ip = hass.data[DOMAIN]["config"].get(CONF_LOCAL_IP)
|
||||
if DOMAIN in hass.data and DOMAIN_CONFIG in hass.data[DOMAIN]:
|
||||
local_ip = hass.data[DOMAIN][DOMAIN_CONFIG].get(CONF_LOCAL_IP)
|
||||
if local_ip:
|
||||
local_ip = IPv4Address(local_ip)
|
||||
|
||||
|
|
|
@ -18,6 +18,8 @@ from .const import (
|
|||
DATA_RATE_PACKETS_PER_SECOND,
|
||||
DEFAULT_SCAN_INTERVAL,
|
||||
DOMAIN,
|
||||
DOMAIN_COORDINATORS,
|
||||
DOMAIN_DEVICES,
|
||||
KIBIBYTE,
|
||||
LOGGER as _LOGGER,
|
||||
PACKETS_RECEIVED,
|
||||
|
@ -84,9 +86,9 @@ async def async_setup_entry(
|
|||
udn = data[CONFIG_ENTRY_UDN]
|
||||
else:
|
||||
# any device will do
|
||||
udn = list(hass.data[DOMAIN]["devices"].keys())[0]
|
||||
udn = list(hass.data[DOMAIN][DOMAIN_DEVICES].keys())[0]
|
||||
|
||||
device: Device = hass.data[DOMAIN]["devices"][udn]
|
||||
device: Device = hass.data[DOMAIN][DOMAIN_DEVICES][udn]
|
||||
|
||||
update_interval_sec = config_entry.options.get(
|
||||
CONFIG_ENTRY_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL
|
||||
|
@ -102,7 +104,7 @@ async def async_setup_entry(
|
|||
update_interval=update_interval,
|
||||
)
|
||||
await coordinator.async_refresh()
|
||||
hass.data[DOMAIN]["coordinators"][udn] = coordinator
|
||||
hass.data[DOMAIN][DOMAIN_COORDINATORS][udn] = coordinator
|
||||
|
||||
sensors = [
|
||||
RawUpnpSensor(coordinator, device, SENSOR_TYPES[BYTES_RECEIVED]),
|
||||
|
|
Loading…
Reference in New Issue