Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix setting enum values, report on invalids in miotsimulator #1574

Merged
merged 1 commit into from
Nov 6, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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