From dfc51b940893c6ae40a635275db95262ddced037 Mon Sep 17 00:00:00 2001 From: Teemu R Date: Fri, 16 Feb 2024 01:02:10 +0100 Subject: [PATCH] Fix genericmiot status to query all readable properties (#1898) Also, optimize future status queries a bit by constructing the status query already during the initialization phase. Fixes a regression caused by #1871, the genericmiot requires deliberately some tests to avoid such, but it's better to have basic functionalities working on master so I'll merge this asap. --- miio/integrations/genericmiot/genericmiot.py | 23 ++++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/miio/integrations/genericmiot/genericmiot.py b/miio/integrations/genericmiot/genericmiot.py index 8da1bf47d..90ddc887f 100644 --- a/miio/integrations/genericmiot/genericmiot.py +++ b/miio/integrations/genericmiot/genericmiot.py @@ -1,6 +1,6 @@ import logging from functools import partial -from typing import Dict, Optional +from typing import Dict, List, Optional from miio import MiotDevice from miio.click_common import command @@ -45,6 +45,7 @@ def __init__( self._actions: Dict[str, ActionDescriptor] = {} self._properties: Dict[str, PropertyDescriptor] = {} + self._status_query: List[Dict] = [] def initialize_model(self): """Initialize the miot model and create descriptions.""" @@ -59,17 +60,13 @@ def initialize_model(self): @command() def status(self) -> GenericMiotStatus: """Return status based on the miot model.""" - properties = [] - for _, prop in self.sensors().items(): - extras = prop.extras - prop = extras["miot_property"] - q = {"siid": prop.siid, "piid": prop.piid, "did": prop.name} - properties.append(q) + if not self._initialized: + self._initialize_descriptors() # TODO: max properties needs to be made configurable (or at least splitted to avoid too large udp datagrams # some devices are stricter: https://github.com/rytilahti/python-miio/issues/1550#issuecomment-1303046286 response = self.get_properties( - properties, property_getter="get_properties", max_properties=10 + self._status_query, property_getter="get_properties", max_properties=10 ) return GenericMiotStatus(response, self) @@ -105,7 +102,15 @@ def _create_properties(self, serv: MiotService): desc = prop.get_descriptor() - if desc.access & AccessFlags.Write: + # Add readable properties to the status query + if AccessFlags.Read in desc.access: + extras = prop.extras + prop = extras["miot_property"] + q = {"siid": prop.siid, "piid": prop.piid, "did": prop.name} + self._status_query.append(q) + + # Bind setter to the descriptor + if AccessFlags.Write in desc.access: desc.setter = partial( self.set_property_by, prop.siid, prop.piid, name=prop.name )