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

Setting development environment #22

Closed
r-jordan opened this issue Jun 7, 2021 · 4 comments
Closed

Setting development environment #22

r-jordan opened this issue Jun 7, 2021 · 4 comments

Comments

@r-jordan
Copy link

r-jordan commented Jun 7, 2021

Hi @azogue ,

I'm trying to setup a development environment for this library because I would like to add some new calculated attributes. What python version do I need to install? Some other recommendations? My operating system is Linux.

Regards,
Raúl

@r-jordan
Copy link
Author

r-jordan commented Jun 7, 2021

I cloned the git repository and when I try the following test code using python version 3.8.10 (example in the README):

from aiopvpc import PVPCData

pvpc_handler = PVPCData(tariff="discrimination")

start = datetime(2020, 3, 20, 22)
end = datetime(2020, 4, 30, 16)
prices_range: dict = await pvpc_handler.async_download_prices_for_range(start, end)

I get the following error message:

  File "test.py", line 7
    prices_range: dict = await pvpc_handler.async_download_prices_for_range(start, end)
                         ^
SyntaxError: 'await' outside function

@azogue
Copy link
Owner

azogue commented Jun 9, 2021

Hi @r-jordan, this is an async library (the "a" in aiopvpc 😜), so most methods are actually coroutines which need to be awaited...

(there is a lot of info out there about concurrency in Python, try this: https://realpython.com/async-io-python/)

The README example is intended for jupyter users 😊, where there is an event loop running and you can await subroutines directly.

To call it from a script you would need to use asyncio.run(pvpc_handler.async_download_prices_for_range(start, end), or just use the blocking method of the PVPCData class: pvpc_handler.download_prices_for_range(start, end) 😝

About the setup environment, everything you need to know is inside the pyproject.toml, and you'll need poetry to install it.

I would like to add some new calculated attributes

About new sensor attributes, please take a look into #21 and feel free to comment or suggest about other attributes :)

@r-jordan
Copy link
Author

r-jordan commented Jun 9, 2021

Hi @azogue , it's public that my knowledge of the asyncio library nor python development with poetry is not very high, I'm sorry 🙏

I'll try to learn about all those things and I'll come back if I have more questions.

Anyway, I'm going to suggest one new attribute in #21 👍

Thank you!

@r-jordan r-jordan closed this as completed Jun 9, 2021
@r-jordan
Copy link
Author

Hi @azogue,

Thanks to your instructions, finally, I succeed making the test script 🎉 😄

I'm sure this is not any great success for an experimenter python developer but for me it's challenging 🤦‍♂️

This is the most simple python code to test the library integration with HA that I made:

from aiopvpc import PVPCData
from datetime import datetime
import asyncio
import aiohttp

async def main():
    async with aiohttp.ClientSession() as session:
        pvpc_handler = PVPCData(tariff="discrimination", websession=session )
        await pvpc_handler.async_update_prices( datetime.now() )
        pvpc_handler.process_state_and_attributes( datetime.now() )
        print( pvpc_handler.state )
        print( pvpc_handler.attributes )

loop = asyncio.get_event_loop()
loop.run_until_complete(main())

The following is the result at the moment of executing it with the printing of the state and the attributes:

0.23482
{'attribution': 'Data retrieved from api.esios.ree.es by REE', 'tariff': '2.0TD', 'min_price': 0.09614, 'min_price_at': 4, 'next_best_at': [16, 17, 15, 14, 23, 22, 13, 18, 12, 19, 21, 20], 'price_00h': 0.10981, 'price_01h': 0.10678, 'price_02h': 0.10105, 'price_03h': 0.09804, 'price_04h': 0.09614, 'price_05h': 0.09934, 'price_06h': 0.10902, 'price_07h': 0.11468, 'price_08h': 0.15268, 'price_09h': 0.14259, 'price_10h': 0.23373, 'price_11h': 0.23414, 'price_12h': 0.23482, 'price_13h': 0.23409, 'price_14h': 0.14089, 'price_15h': 0.13898, 'price_16h': 0.13749, 'price_17h': 0.13762, 'price_18h': 0.23436, 'price_19h': 0.24117, 'price_20h': 0.24423, 'price_21h': 0.24247, 'price_22h': 0.15115, 'price_23h': 0.14768}

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

No branches or pull requests

2 participants