From bd9f03010f09018d90cc6710f3fc9bc7477daed5 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Thu, 29 Dec 2022 10:59:06 +0100 Subject: [PATCH] Improve `upnp` typing (#84652) --- homeassistant/components/upnp/coordinator.py | 3 +-- homeassistant/components/upnp/device.py | 3 +-- homeassistant/components/upnp/sensor.py | 5 +++-- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/upnp/coordinator.py b/homeassistant/components/upnp/coordinator.py index 18d37b4a388..2820a584632 100644 --- a/homeassistant/components/upnp/coordinator.py +++ b/homeassistant/components/upnp/coordinator.py @@ -1,6 +1,5 @@ """UPnP/IGD coordinator.""" -from collections.abc import Mapping from datetime import timedelta from typing import Any @@ -35,7 +34,7 @@ class UpnpDataUpdateCoordinator(DataUpdateCoordinator): update_interval=update_interval, ) - async def _async_update_data(self) -> Mapping[str, Any]: + async def _async_update_data(self) -> dict[str, Any]: """Update data.""" try: return await self.device.async_get_data() diff --git a/homeassistant/components/upnp/device.py b/homeassistant/components/upnp/device.py index 61784749c6f..ed06a9eb363 100644 --- a/homeassistant/components/upnp/device.py +++ b/homeassistant/components/upnp/device.py @@ -1,7 +1,6 @@ """Home Assistant representation of an UPnP/IGD.""" from __future__ import annotations -from collections.abc import Mapping from functools import partial from ipaddress import ip_address from typing import Any @@ -135,7 +134,7 @@ class Device: """Get string representation.""" return f"IGD Device: {self.name}/{self.udn}::{self.device_type}" - async def async_get_data(self) -> Mapping[str, Any]: + async def async_get_data(self) -> dict[str, Any]: """Get all data from device.""" _LOGGER.debug("Getting data for device: %s", self) igd_state = await self._igd_device.async_get_traffic_and_status_data() diff --git a/homeassistant/components/upnp/sensor.py b/homeassistant/components/upnp/sensor.py index e5b09d1a398..97741c0dbdd 100644 --- a/homeassistant/components/upnp/sensor.py +++ b/homeassistant/components/upnp/sensor.py @@ -178,7 +178,8 @@ class UpnpSensor(UpnpEntity, SensorEntity): @property def native_value(self) -> str | None: """Return the state of the device.""" - value = self.coordinator.data[self.entity_description.value_key] - if value is None: + if (key := self.entity_description.value_key) is None: + return None + if (value := self.coordinator.data[key]) is None: return None return format(value, self.entity_description.format)