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

Devtools exception miottemplate.py generate #885

Closed
IhorSyerkov opened this issue Dec 5, 2020 · 4 comments · Fixed by #902
Closed

Devtools exception miottemplate.py generate #885

IhorSyerkov opened this issue Dec 5, 2020 · 4 comments · Fixed by #902
Labels

Comments

@IhorSyerkov
Copy link
Contributor

Describe the bug
I'm trying to add support Yeelight Smart Dual Control Module (yeelink.switch.sw1) using devtools.
http://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:switch:0000A003:yeelink-sw1:1:0000C809
Downloaded json using python miottemplate.py download urn:miot-spec-v2:device:switch:0000A003:yeelink-sw1:1:0000C809
but getting error when trying to generate tempate python miottemplate.py generate urn:miot-spec-v2:device:switch:0000A003:yeelink-sw1:1:0000C809.json

Version information (please complete the following information):

  • OS: macOs big sur 11.0.1
  • python-miio: miiocli, version 0.5.4

Device information:
If the issue is specific to a device [Use miiocli device --ip <ip address> --token <token> info]:

  • Model: yeelink.switch.sw1
  • Hardware version: esp32
  • Firmware version: 2.0.6_0012

Console output

Traceback (most recent call last):
  File "/Users/ihor_syerkov/Library/Caches/pypoetry/virtualenvs/python-miio-HruUc-gR-py3.9/lib/python3.9/site-packages/dataclasses_json/core.py", line 263, in _decode_generic
    res = _get_type_cons(type_)(xs)
  File "/Users/ihor_syerkov/Library/Caches/pypoetry/virtualenvs/python-miio-HruUc-gR-py3.9/lib/python3.9/site-packages/dataclasses_json/core.py", line 306, in <genexpr>
    items = (_decode_dataclass(type_arg, x, infer_missing)
  File "/Users/ihor_syerkov/Library/Caches/pypoetry/virtualenvs/python-miio-HruUc-gR-py3.9/lib/python3.9/site-packages/dataclasses_json/core.py", line 201, in _decode_dataclass
    init_kwargs[field.name] = _decode_generic(field_type,
  File "/Users/ihor_syerkov/Library/Caches/pypoetry/virtualenvs/python-miio-HruUc-gR-py3.9/lib/python3.9/site-packages/dataclasses_json/core.py", line 258, in _decode_generic
    xs = _decode_items(type_.__args__[0], value, infer_missing)
  File "/usr/local/Cellar/python@3.9/3.9.0_4/Frameworks/Python.framework/Versions/3.9/lib/python3.9/typing.py", line 648, in __getattr__
    raise AttributeError(attr)
AttributeError: __args__

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/ihor_syerkov/Library/Caches/pypoetry/virtualenvs/python-miio-HruUc-gR-py3.9/lib/python3.9/site-packages/dataclasses_json/core.py", line 263, in _decode_generic
    res = _get_type_cons(type_)(xs)
  File "/Users/ihor_syerkov/Library/Caches/pypoetry/virtualenvs/python-miio-HruUc-gR-py3.9/lib/python3.9/site-packages/dataclasses_json/core.py", line 306, in <genexpr>
    items = (_decode_dataclass(type_arg, x, infer_missing)
  File "/Users/ihor_syerkov/Library/Caches/pypoetry/virtualenvs/python-miio-HruUc-gR-py3.9/lib/python3.9/site-packages/dataclasses_json/core.py", line 201, in _decode_dataclass
    init_kwargs[field.name] = _decode_generic(field_type,
  File "/Users/ihor_syerkov/Library/Caches/pypoetry/virtualenvs/python-miio-HruUc-gR-py3.9/lib/python3.9/site-packages/dataclasses_json/core.py", line 265, in _decode_generic
    res = type_(xs)
  File "/usr/local/Cellar/python@3.9/3.9.0_4/Frameworks/Python.framework/Versions/3.9/lib/python3.9/typing.py", line 622, in __call__
    raise TypeError(f"Type {self._name} cannot be instantiated; "
TypeError: Type List cannot be instantiated; use list() instead

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/ihor_syerkov/source/temp/python-miio/devtools/miottemplate.py", line 65, in <module>
    cli()
  File "/Users/ihor_syerkov/Library/Caches/pypoetry/virtualenvs/python-miio-HruUc-gR-py3.9/lib/python3.9/site-packages/click/core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "/Users/ihor_syerkov/Library/Caches/pypoetry/virtualenvs/python-miio-HruUc-gR-py3.9/lib/python3.9/site-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/Users/ihor_syerkov/Library/Caches/pypoetry/virtualenvs/python-miio-HruUc-gR-py3.9/lib/python3.9/site-packages/click/core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/Users/ihor_syerkov/Library/Caches/pypoetry/virtualenvs/python-miio-HruUc-gR-py3.9/lib/python3.9/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/Users/ihor_syerkov/Library/Caches/pypoetry/virtualenvs/python-miio-HruUc-gR-py3.9/lib/python3.9/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/Users/ihor_syerkov/source/temp/python-miio/devtools/miottemplate.py", line 47, in generate
    print(gen.generate())
  File "/Users/ihor_syerkov/source/temp/python-miio/devtools/miottemplate.py", line 25, in generate
    dev = Device.from_json(self.data)
  File "/Users/ihor_syerkov/Library/Caches/pypoetry/virtualenvs/python-miio-HruUc-gR-py3.9/lib/python3.9/site-packages/dataclasses_json/api.py", line 76, in from_json
    return cls.from_dict(kvs, infer_missing=infer_missing)
  File "/Users/ihor_syerkov/Library/Caches/pypoetry/virtualenvs/python-miio-HruUc-gR-py3.9/lib/python3.9/site-packages/dataclasses_json/api.py", line 83, in from_dict
    return _decode_dataclass(cls, kvs, infer_missing)
  File "/Users/ihor_syerkov/Library/Caches/pypoetry/virtualenvs/python-miio-HruUc-gR-py3.9/lib/python3.9/site-packages/dataclasses_json/core.py", line 201, in _decode_dataclass
    init_kwargs[field.name] = _decode_generic(field_type,
  File "/Users/ihor_syerkov/Library/Caches/pypoetry/virtualenvs/python-miio-HruUc-gR-py3.9/lib/python3.9/site-packages/dataclasses_json/core.py", line 265, in _decode_generic
    res = type_(xs)
  File "/usr/local/Cellar/python@3.9/3.9.0_4/Frameworks/Python.framework/Versions/3.9/lib/python3.9/typing.py", line 622, in __call__
    raise TypeError(f"Type {self._name} cannot be instantiated; "
TypeError: Type List cannot be instantiated; use list() instead```
@IhorSyerkov IhorSyerkov added the bug label Dec 5, 2020
@IhorSyerkov
Copy link
Contributor Author

There was few bugs in devtools/containers.py, but I'm not sure is this generator is valid.
Generated code contains MiOTService which is absent in code base.

@aalbul
Copy link

aalbul commented Dec 13, 2020

I'm getting the same problem, unfortunately :(

@IhorSyerkov
Copy link
Contributor Author

diff --git a/devtools/containers.py b/devtools/containers.py
index e0a5cb2..465d998 100644
--- a/devtools/containers.py
+++ b/devtools/containers.py
@@ -1,5 +1,5 @@
 from dataclasses import dataclass, field
-from typing import List
+from typing import Any, Dict, List, Optional
 
 from dataclasses_json import DataClassJsonMixin, config
 
@@ -35,12 +35,14 @@ class Property(DataClassJsonMixin):
     format: str
     access: List[str]
 
-    value_list: List = field(
+    value_list: Optional[List[Dict[str, Any]]] = field(
         default_factory=list, metadata=config(field_name="value-list")
     )
-    value_range: List = field(default=None, metadata=config(field_name="value-range"))
+    value_range: Optional[List[Any]] = field(
+        default=None, metadata=config(field_name="value-range")
+    )
 
-    unit: str = None
+    unit: Optional[str] = None
 
     def __repr__(self):
         return f"piid: {self.iid} ({self.description}): ({self.format}, unit: {self.unit}) (acc: {self.access}, value-list: {self.value_list}, value-range: {self.value_range})"

this will fix error, but as I see this generator outdated. Generated code is not relevant

@rytilahti
Copy link
Owner

Yes, sorry about that :-( The generator was uploaded only as a proof-of-concept to simplify the process, but in its current state it is only helpful for parsing out the details from the json files and cannot be used as a real generator.

The PR that aimed to improve the miot support and make it easier to implement the generation is #672, but I haven't had time nor do posess test devices anymore to improve it further to make it useful. Contributions are welcome on that front! I'd recommend using attrs instead of dataclasses for the containers though.

@IhorSyerkov feel free to create a PR to get that fix included (and maybe add a verbose warning that the generated code is not directly usable), it may still be useful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants