Git Product home page Git Product logo

pvoutput's Introduction

pvoutput

PVOutput.org python API module. Works with the R2 API version spec here.

Get your API key from the account page on PVOutput

Code style: black

Example usage

Here's a quick code example:

    from pvoutput import PVOutput
    import json
    apikey = 'aaaaaabbbbbbccccccddddddeeeeeeffffffgggg'
    systemid = 12345
    pvo = PVOutput(apikey=apikey, systemid=systemid)
    print(json.dumps(pvo.check_rate_limit(), indent=2))

Will give you output like this:

    {
        "X-Rate-Limit-Remaining": "271",
        "X-Rate-Limit-Limit": "300",
        "X-Rate-Limit-Reset": "1570597200"
    }

There are more example code snippets in the examples directory.

Installing

Prod-ish usage

python -m pip install pvoutput to install from pypi

Dev Install Things

python -m venv venv
source venv/bin/activate
python -m pip install --upgrade pip flit
python -m flit install

Input validation

This is handled by the pvoutput.base.PVOutputBase.validate_data function.

It expects the input data and a dict of configuration parameters, which are described in the table below:

Field name Required Valid Types Description
type Yes Any This is a python type object to match against the field type.
required No bool This defines if the field is required.
description No Any This is currently unused, but typically holds the description from the PVOutput API Docs
donation_required No bool If set to true, and the user's not donated, it'll throw a DonationRequired exception if the user tries to use functionality that requires them to have donated. It's a whole thing.
maxlen No int Maximum length of the field. ie. if len(field) > maxlen: raise ValueError
maxval No int Maximum value of the field.
minval No int Minimum value of the field.
additional_validators No List[function] A list of functions to run against the field, which should throw exceptions if something's wrong.

An example configuration

"date_val": {
    "required": True,
    "description": "Date",
    "type": date,
    "donation_required": False,
    "additional_validators" : [
        validate_delete_status_date
    ]
}

Contributing / Testing

ruff, black and mypy should all pass before submitting a PR.

License

MIT License (see LICENSE), don't use this for anything you care about - I don't provide a warranty at all, and it'll likely steal your socks and underfeed your dog.

Changelog

  • 0.0.1 Initial version
  • 0.0.2 2019-10-12 Fixed some bugs
  • 0.0.3 2019-10-13 Added PVOutput.getstatus() which returns the current status as a dict
  • 0.0.4 2019-11-05 Code cleanup using sonarqube, added an error check for registernotification
  • 0.0.5 Asyncio things
  • 0.0.6 I broke the build when uploading to pypi, fixed in 0.0.7.
  • 0.0.7 2021-12-27 #117 fix for getstatus issues
  • 0.0.8 2022-01-02 @cheops did great work cleaning up a lot of my mess, and testing is much better.
  • 0.0.10 2022-08-27 Added explicit timeouts to HTTP connections in the synchronous client.
  • 0.0.11 2022-08-27 Added explicit timeouts to HTTP connections in the aiohttp client.
  • 0.0.12 (pending) 2023-10-10 Adding addbatchstatus

pvoutput's People

Contributors

cheops avatar dependabot-preview[bot] avatar dependabot[bot] avatar yaleman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

pvoutput's Issues

Data validation is overexited

Running a script for a few weeks without issues, until today:

Mar 25 12:00:03 raspberrypi sdmpvo:     self.validate_data(data, ADDSTATUS_PARAMETERS)
Mar 25 12:00:03 raspberrypi sdmpvo:   File "/usr/local/lib/python3.7/dist-packages/pvoutput/utils.py", line 128, in validate_data
Mar 25 12:00:03 raspberrypi sdmpvo:     raise ValueError(f"v3 cannot be higher than 200000, is {data['v3']}")
Mar 25 12:00:03 raspberrypi sdmpvo: ValueError: v3 cannot be higher than 200000, is 217621

According to the PVO documentation for addstatus service there is no limitation for v3 (consumption in Wh)
I also provide c1=1 which means Both v1 and v3 values are lifetime energy values.

Commenting out the validation in /usr/local/lib/python3.7/dist-packages/pvoutput/utils.py makes the script running again (PVO accepts the data)

    #if int(data.get("v3", 0)) > 200000:
    #    raise ValueError(f"v3 cannot be higher than 200000, is {data['v3']}")
    #if int(data.get("v4", 0)) > 100000:
    #    raise ValueError(f"v4 cannot be higher than 100000, is {data['v4']}")

Edit: pvoutput was installed on raspberry pi using pip3 install pvoutput

ValueError: minute must be in 0..59

File "/home/pi/.local/lib/python3.7/site-packages/pvoutput/init.py", line 133, in addstatus
data["t"] = time(hour=hour, minute=minute).strftime("%H:%M")
ValueError: minute must be in 0..59

If using datetime.now()
the rounding here can round up to 60 minutes, which isn't desireable ->
minute = int(
self.stats_period
* round(int(datetime.now().strftime("%M")) / self.stats_period)
)

check_rate_limit can fail if getStatus does not return any results

check_rate_limit uses the url = "https://pvoutput.org/service/r2/getstatus.jsp" (and also wrongly uses the default _call method=requests.post)
but this can return an error, from the documentation:

Use the d parameter to request a specific date. When date is omitted, the last known live data in the last 7 days will be used.
A Bad request 400: No status found
No live data found on a specified date or no live data reported in the last 7 days when no date parameter is used.

the Get System Service with the method requests.get seems a safer bet

          url = "https://pvoutput.org/service/r2/getsystem.jsp"
          response = self._call(url, {}, headers=headers, method=requests.get)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.