sampsyo / wideq

reverse-engineered client for the LG SmartThinQ API
https://pypi.org/project/wideq/
MIT License
331 stars 159 forks source link

add a mapping for every subclass of Device with DeviceType enum as key #130

Closed pifou25 closed 4 years ago

pifou25 commented 4 years ago

This make it easy to get the relevant subclasse of Device according to the DeviceType of the model info

sampsyo commented 4 years ago

Seems like a pretty reasonable idea! I have a few random questions about the design of where to put this mapping, etc.:

pifou25 commented 4 years ago

I first imagined to add such method in the Client class next to get_device, something like a "get_device_class(self, device_id)" what do you t hink of that ? About "registration" : I didn't want to populate the dict mapping into client.py because it required to import every other files into client.py ... What I can try, is to define and import into the method:

def get_device_class(self, device_id):
  from ac import ACDevice
  ...
  device_classes = {DeviceType.AC: ACDevice, ... }

Is it a good practice ? Otherwise, I think the best way is to add a global registration function into util.py

sampsyo commented 4 years ago

Ah, got it, cool… such a method makes sense to have to get the specific class for a given DeviceInfo, but maybe the mapping should be available outside of the class?

And yeah, that "local import" thing is not a bad idea. What do you think about this?

Does that seem simple enough?

pifou25 commented 4 years ago

Hello, I made it simple I hope so :) And I also modified the example.py to use the new method. I successfully tested it with an AC device.

sampsyo commented 4 years ago

Awesome; this seems great! Thanks again!!