Skip to content

Commit

Permalink
Fix genericmiot status to query all readable properties (#1898)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rytilahti committed Feb 16, 2024
1 parent 56c6d3f commit dfc51b9
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions miio/integrations/genericmiot/genericmiot.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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."""
Expand All @@ -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)
Expand Down Expand Up @@ -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
)
Expand Down

0 comments on commit dfc51b9

Please sign in to comment.