From 6eab0463776261f6a3f48758658f3a5a6cacfb6a Mon Sep 17 00:00:00 2001 From: Teemu Rytilahti Date: Sun, 6 Nov 2022 21:31:01 +0100 Subject: [PATCH] Fix setting enum values, report on invalid values in miotsimulator --- miio/devtools/simulators/miotsimulator.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/miio/devtools/simulators/miotsimulator.py b/miio/devtools/simulators/miotsimulator.py index f92c7e16e..cea12e078 100644 --- a/miio/devtools/simulators/miotsimulator.py +++ b/miio/devtools/simulators/miotsimulator.py @@ -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.""" @@ -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): @@ -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}