mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-10-10 01:18:03 +00:00
Fix repr for device created with no sysinfo or discovery info" (#1266)
Co-authored-by: Teemu R. <tpr@iki.fi>
This commit is contained in:
@@ -210,12 +210,12 @@ class Device(ABC):
|
||||
self.protocol: BaseProtocol = protocol or IotProtocol(
|
||||
transport=XorTransport(config=config or DeviceConfig(host=host)),
|
||||
)
|
||||
_LOGGER.debug("Initializing %s of type %s", self.host, type(self))
|
||||
self._last_update: Any = None
|
||||
_LOGGER.debug("Initializing %s of type %s", host, type(self))
|
||||
self._device_type = DeviceType.Unknown
|
||||
# TODO: typing Any is just as using Optional[Dict] would require separate
|
||||
# checks in accessors. the @updated_required decorator does not ensure
|
||||
# mypy that these are not accessed incorrectly.
|
||||
self._last_update: Any = None
|
||||
self._discovery_info: dict[str, Any] | None = None
|
||||
|
||||
self._features: dict[str, Feature] = {}
|
||||
@@ -492,6 +492,8 @@ class Device(ABC):
|
||||
|
||||
def __repr__(self) -> str:
|
||||
update_needed = " - update() needed" if not self._last_update else ""
|
||||
if not self._last_update and not self._discovery_info:
|
||||
return f"<{self.device_type} at {self.host}{update_needed}>"
|
||||
return (
|
||||
f"<{self.device_type} at {self.host} -"
|
||||
f" {self.alias} ({self.model}){update_needed}>"
|
||||
|
@@ -42,7 +42,9 @@ def requires_update(f: Callable) -> Any:
|
||||
@functools.wraps(f)
|
||||
async def wrapped(*args: Any, **kwargs: Any) -> Any:
|
||||
self = args[0]
|
||||
if self._last_update is None and f.__name__ not in self._sys_info:
|
||||
if self._last_update is None and (
|
||||
self._sys_info is None or f.__name__ not in self._sys_info
|
||||
):
|
||||
raise KasaException("You need to await update() to access the data")
|
||||
return await f(*args, **kwargs)
|
||||
|
||||
@@ -51,7 +53,9 @@ def requires_update(f: Callable) -> Any:
|
||||
@functools.wraps(f)
|
||||
def wrapped(*args: Any, **kwargs: Any) -> Any:
|
||||
self = args[0]
|
||||
if self._last_update is None and f.__name__ not in self._sys_info:
|
||||
if self._last_update is None and (
|
||||
self._sys_info is None or f.__name__ not in self._sys_info
|
||||
):
|
||||
raise KasaException("You need to await update() to access the data")
|
||||
return f(*args, **kwargs)
|
||||
|
||||
|
@@ -107,16 +107,26 @@ class SmartChildDevice(SmartDevice):
|
||||
@property
|
||||
def device_type(self) -> DeviceType:
|
||||
"""Return child device type."""
|
||||
category = self.sys_info["category"]
|
||||
dev_type = self.CHILD_DEVICE_TYPE_MAP.get(category)
|
||||
if dev_type is None:
|
||||
_LOGGER.warning(
|
||||
"Unknown child device type %s for model %s, please open issue",
|
||||
category,
|
||||
self.model,
|
||||
)
|
||||
dev_type = DeviceType.Unknown
|
||||
return dev_type
|
||||
if self._device_type is not DeviceType.Unknown:
|
||||
return self._device_type
|
||||
|
||||
if self.sys_info and (category := self.sys_info.get("category")):
|
||||
dev_type = self.CHILD_DEVICE_TYPE_MAP.get(category)
|
||||
if dev_type is None:
|
||||
_LOGGER.warning(
|
||||
"Unknown child device type %s for model %s, please open issue",
|
||||
category,
|
||||
self.model,
|
||||
)
|
||||
self._device_type = DeviceType.Unknown
|
||||
else:
|
||||
self._device_type = dev_type
|
||||
|
||||
return self._device_type
|
||||
|
||||
def __repr__(self) -> str:
|
||||
if not self._parent:
|
||||
return f"<{self.device_type}(child) without parent>"
|
||||
if not self._parent._last_update:
|
||||
return f"<{self.device_type}(child) of {self._parent}>"
|
||||
return f"<{self.device_type} {self.alias} ({self.model}) of {self._parent}>"
|
||||
|
@@ -757,6 +757,10 @@ class SmartDevice(Device):
|
||||
|
||||
# Fallback to device_type (from disco info)
|
||||
type_str = self._info.get("type", self._info.get("device_type"))
|
||||
|
||||
if not type_str: # no update or discovery info
|
||||
return self._device_type
|
||||
|
||||
self._device_type = self._get_device_type_from_components(
|
||||
list(self._components.keys()), type_str
|
||||
)
|
||||
|
@@ -25,8 +25,11 @@ class SmartCamera(SmartDevice):
|
||||
@staticmethod
|
||||
def _get_device_type_from_sysinfo(sysinfo: dict[str, Any]) -> DeviceType:
|
||||
"""Find type to be displayed as a supported device category."""
|
||||
device_type = sysinfo["device_type"]
|
||||
if device_type.endswith("HUB"):
|
||||
if (
|
||||
sysinfo
|
||||
and (device_type := sysinfo.get("device_type"))
|
||||
and device_type.endswith("HUB")
|
||||
):
|
||||
return DeviceType.Hub
|
||||
return DeviceType.Camera
|
||||
|
||||
|
Reference in New Issue
Block a user