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

Inheritance #46

Open
wants to merge 12 commits into
base: master
Choose a base branch
from

Conversation

Apsysikal
Copy link
Contributor

This moved the MyStromDevice class from the __init__.py file into its separate file including the related functions from the __init__.py.
The imports of the _request function have been updated in the bulb.py, pir.py and switch.py files to reflect the changes.
This also made the device classes inherit from the MyStromDevice base class.
The url had to be changed to make all devices work from the base class url.
Duplicate functions and variable assignments were removed from the inheriting classes. E.g.:

self._close_session = False
self._host = host
self._session = session

These changes were tested using the examples with the myStrom Switch and the myStrom PIR. I don't own or have bulbs, so if someone could test those, that would be great.

@Apsysikal
Copy link
Contributor Author

Apsysikal commented Jan 13, 2024

@fabaff This fixes the linting errors and also resoled the merge conflicts, merging the upstream master.

Edit: should this be a minor release, as every device inheriting from the base class now supports the get_device_info function. I could update the version info in the pull request.

Copy link
Member

@fabaff fabaff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution.

For the bulb example i get a trackback.

$ python examples/example-bulb.py 
DEBUG:asyncio:Using selector: EpollSelector
/home/repos/python-mystrom/pymystrom/bulb.py:176: RuntimeWarning: coroutine 'MyStromDevice.__aenter__' was never awaited
  super().__aenter__()
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
Get the details from the bulb...
Traceback (most recent call last):
  File "/home/repos/python-mystrom/examples/example-bulb.py", line 65, in <module>
    loop.run_until_complete(main())
  File "/etc/profiles/per-user/lib/python3.11/asyncio/base_events.py", line 653, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/home/repos/python-mystrom/examples/example-bulb.py", line 16, in main
    await bulb.get_state()
          ^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'get_state'

@Apsysikal
Copy link
Contributor Author

I see. From what I've found I would most likely need to await the super().__aenter__().

So:

async def __aenter__(self) -> "MyStromBulb":
  super().__aenter__()

would become:

async def __aenter__(self) -> "MyStromBulb":
  await super().__aenter__()
  return self

I'll test it as soon as possible. If you have time to test it in the bulb I'll gladly update all the other files based on your feedback.

@Apsysikal
Copy link
Contributor Author

If I understand the async context manager correctly und looking at your usage it should be good, just returning self.

@Apsysikal
Copy link
Contributor Author

I've tested my pir and plug.

Pir output:

Settings: {'rest': True, 'panel': True, 'hap_disable': False, 'name': '', 'temp_offset': 0}
PIR settings: {'backoff_time': 180, 'led_enable': True}
Sensors: {'motion': False, 'light': 24, 'temperature': 20.62}
Temperatures: {'measured': 31.93, 'compensation': 11.31, 'compensated': 20.62, 'offset': 0}
Temperature measured: 31.93
Brightness: 24
Day?: True
Raw light data: {'visible': 24, 'infrared': 4}
Actions: {'pir': {'generic': 'get://192.168.0.11:8123/api/mystrom', 'night': '', 'twilight': '', 'day': '', 'rise': '', 'fall': ''}}

Plug output:

Power consumption: 13.1
Energy consumed: 22.3
Relay state: True
Temperature: 21.8
Firmware: 4.0.14
MAC address: C8F09E86BD50
Turn on the switch

They also use the async with signature, same as the bulb. My guess would be, that your test should now also succeed.

Little side note: Don't run the switch example if your router and wifi is plugged into it. And no I didn't just accidentally shut down my internet 👀

@Apsysikal
Copy link
Contributor Author

Did you have time to look into it?

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

Successfully merging this pull request may close these issues.

None yet

2 participants