Fix upnp error on unload_entry if device does not exist (#38230)

pull/40212/head
Steven Looman 2020-08-05 02:24:42 +02:00 committed by GitHub
parent e76db60367
commit c33f309d5f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 14 deletions

View File

@ -20,6 +20,10 @@ from .const import (
DISCOVERY_UDN, DISCOVERY_UDN,
DISCOVERY_USN, DISCOVERY_USN,
DOMAIN, DOMAIN,
DOMAIN_CONFIG,
DOMAIN_COORDINATORS,
DOMAIN_DEVICES,
DOMAIN_LOCAL_IP,
LOGGER as _LOGGER, LOGGER as _LOGGER,
) )
from .device import Device from .device import Device
@ -78,10 +82,10 @@ async def async_setup(hass: HomeAssistantType, config: ConfigType):
conf = config.get(DOMAIN, conf_default) conf = config.get(DOMAIN, conf_default)
local_ip = await hass.async_add_executor_job(get_local_ip) local_ip = await hass.async_add_executor_job(get_local_ip)
hass.data[DOMAIN] = { hass.data[DOMAIN] = {
"config": conf, DOMAIN_CONFIG: conf,
"devices": {}, DOMAIN_COORDINATORS: {},
"coordinators": {}, DOMAIN_DEVICES: {},
"local_ip": conf.get(CONF_LOCAL_IP, local_ip), DOMAIN_LOCAL_IP: conf.get(CONF_LOCAL_IP, local_ip),
} }
# Only start if set up via configuration.yaml. # Only start if set up via configuration.yaml.
@ -108,7 +112,7 @@ async def async_setup_entry(hass: HomeAssistantType, config_entry: ConfigEntry)
raise ConfigEntryNotReady raise ConfigEntryNotReady
# Save device # Save device
hass.data[DOMAIN]["devices"][device.udn] = device hass.data[DOMAIN][DOMAIN_DEVICES][device.udn] = device
# Ensure entry has proper unique_id. # Ensure entry has proper unique_id.
if config_entry.unique_id != device.unique_id: if config_entry.unique_id != device.unique_id:
@ -141,8 +145,10 @@ async def async_unload_entry(
) -> bool: ) -> bool:
"""Unload a UPnP/IGD device from a config entry.""" """Unload a UPnP/IGD device from a config entry."""
udn = config_entry.data.get(CONFIG_ENTRY_UDN) udn = config_entry.data.get(CONFIG_ENTRY_UDN)
del hass.data[DOMAIN]["devices"][udn] if udn in hass.data[DOMAIN][DOMAIN_DEVICES]:
del hass.data[DOMAIN]["coordinators"][udn] 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") _LOGGER.debug("Deleting sensors")
return await hass.config_entries.async_forward_entry_unload(config_entry, "sensor") return await hass.config_entries.async_forward_entry_unload(config_entry, "sensor")

View File

@ -20,6 +20,7 @@ from .const import ( # pylint: disable=unused-import
DISCOVERY_UDN, DISCOVERY_UDN,
DISCOVERY_USN, DISCOVERY_USN,
DOMAIN, DOMAIN,
DOMAIN_COORDINATORS,
LOGGER as _LOGGER, LOGGER as _LOGGER,
) )
from .device import Device from .device import Device
@ -221,7 +222,7 @@ class UpnpOptionsFlowHandler(config_entries.OptionsFlow):
"""Manage the options.""" """Manage the options."""
if user_input is not None: if user_input is not None:
udn = self.config_entry.data.get(CONFIG_ENTRY_UDN) 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( update_interval_sec = user_input.get(
CONFIG_ENTRY_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL CONFIG_ENTRY_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL
) )

View File

@ -4,9 +4,14 @@ import logging
from homeassistant.const import TIME_SECONDS from homeassistant.const import TIME_SECONDS
LOGGER = logging.getLogger(__package__)
CONF_LOCAL_IP = "local_ip" CONF_LOCAL_IP = "local_ip"
DOMAIN = "upnp" 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_RECEIVED = "bytes_received"
BYTES_SENT = "bytes_sent" BYTES_SENT = "bytes_sent"
PACKETS_RECEIVED = "packets_received" PACKETS_RECEIVED = "packets_received"

View File

@ -20,6 +20,7 @@ from .const import (
DISCOVERY_UDN, DISCOVERY_UDN,
DISCOVERY_USN, DISCOVERY_USN,
DOMAIN, DOMAIN,
DOMAIN_CONFIG,
LOGGER as _LOGGER, LOGGER as _LOGGER,
PACKETS_RECEIVED, PACKETS_RECEIVED,
PACKETS_SENT, PACKETS_SENT,
@ -40,8 +41,8 @@ class Device:
"""Discover UPnP/IGD devices.""" """Discover UPnP/IGD devices."""
_LOGGER.debug("Discovering UPnP/IGD devices") _LOGGER.debug("Discovering UPnP/IGD devices")
local_ip = None local_ip = None
if DOMAIN in hass.data and "config" in hass.data[DOMAIN]: if DOMAIN in hass.data and DOMAIN_CONFIG in hass.data[DOMAIN]:
local_ip = hass.data[DOMAIN]["config"].get(CONF_LOCAL_IP) local_ip = hass.data[DOMAIN][DOMAIN_CONFIG].get(CONF_LOCAL_IP)
if local_ip: if local_ip:
local_ip = IPv4Address(local_ip) local_ip = IPv4Address(local_ip)

View File

@ -18,6 +18,8 @@ from .const import (
DATA_RATE_PACKETS_PER_SECOND, DATA_RATE_PACKETS_PER_SECOND,
DEFAULT_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL,
DOMAIN, DOMAIN,
DOMAIN_COORDINATORS,
DOMAIN_DEVICES,
KIBIBYTE, KIBIBYTE,
LOGGER as _LOGGER, LOGGER as _LOGGER,
PACKETS_RECEIVED, PACKETS_RECEIVED,
@ -84,9 +86,9 @@ async def async_setup_entry(
udn = data[CONFIG_ENTRY_UDN] udn = data[CONFIG_ENTRY_UDN]
else: else:
# any device will do # 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( update_interval_sec = config_entry.options.get(
CONFIG_ENTRY_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL CONFIG_ENTRY_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL
@ -102,7 +104,7 @@ async def async_setup_entry(
update_interval=update_interval, update_interval=update_interval,
) )
await coordinator.async_refresh() await coordinator.async_refresh()
hass.data[DOMAIN]["coordinators"][udn] = coordinator hass.data[DOMAIN][DOMAIN_COORDINATORS][udn] = coordinator
sensors = [ sensors = [
RawUpnpSensor(coordinator, device, SENSOR_TYPES[BYTES_RECEIVED]), RawUpnpSensor(coordinator, device, SENSOR_TYPES[BYTES_RECEIVED]),