Skip to content

Commit

Permalink
Fix setting enum values, report on invalid values in miotsimulator
Browse files Browse the repository at this point in the history
  • Loading branch information
rytilahti committed Nov 6, 2022
1 parent e6a0c45 commit 6eab046
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions miio/devtools/simulators/miotsimulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
_LOGGER = logging.getLogger(__name__)
UNSET = -10000

ERR_INVALID_SETTING = -1000


def create_random(values):
"""Create random value for the given mapping."""
Expand Down Expand Up @@ -72,14 +74,14 @@ def verify_value(cls, v, values):
raise ValueError(f"{casted_value} not in range {range}")

choices = values["choices"]
if choices is not None:
return choices[casted_value]
if choices is not None and not any(c.value == casted_value for c in choices):
raise ValueError(f"{casted_value} not found in {choices}")

return casted_value

class Config:
validate_assignment = True
smart_union = True
smart_union = True # try all types before coercing


class SimulatedMiotService(MiotService):
Expand Down Expand Up @@ -121,8 +123,13 @@ def get_properties(self, payload):
params = payload["params"]
for p in params:
res = p.copy()
res["value"] = self._state[res["siid"]][res["piid"]].current_value
res["code"] = 0
try:
res["value"] = self._state[res["siid"]][res["piid"]].current_value
res["code"] = 0
except Exception as ex:
res["value"] = ""
res["code"] = ERR_INVALID_SETTING
res["exception"] = str(ex)
response.append(res)

return {"result": response}
Expand Down

0 comments on commit 6eab046

Please sign in to comment.