Initial version
This commit is contained in:
commit
f891a7facc
4 changed files with 201 additions and 0 deletions
61
.gitignore
vendored
Normal file
61
.gitignore
vendored
Normal file
|
@ -0,0 +1,61 @@
|
|||
# ---> Python
|
||||
# Byte-compiled / optimized / DLL files
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
*$py.class
|
||||
|
||||
# C extensions
|
||||
*.so
|
||||
|
||||
# Distribution / packaging
|
||||
.Python
|
||||
env/
|
||||
build/
|
||||
develop-eggs/
|
||||
dist/
|
||||
downloads/
|
||||
eggs/
|
||||
.eggs/
|
||||
lib/
|
||||
lib64/
|
||||
parts/
|
||||
sdist/
|
||||
var/
|
||||
*.egg-info/
|
||||
.installed.cfg
|
||||
*.egg
|
||||
|
||||
# PyInstaller
|
||||
# Usually these files are written by a python script from a template
|
||||
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
||||
*.manifest
|
||||
*.spec
|
||||
|
||||
# Installer logs
|
||||
pip-log.txt
|
||||
pip-delete-this-directory.txt
|
||||
|
||||
# Unit test / coverage reports
|
||||
htmlcov/
|
||||
.tox/
|
||||
.coverage
|
||||
.coverage.*
|
||||
.cache
|
||||
nosetests.xml
|
||||
coverage.xml
|
||||
*,cover
|
||||
|
||||
# Translations
|
||||
*.mo
|
||||
*.pot
|
||||
|
||||
# Django stuff:
|
||||
*.log
|
||||
|
||||
# Sphinx documentation
|
||||
docs/_build/
|
||||
|
||||
# PyBuilder
|
||||
target/
|
||||
|
||||
config.yml
|
8
LICENSE
Normal file
8
LICENSE
Normal file
|
@ -0,0 +1,8 @@
|
|||
MIT License
|
||||
Copyright (c) <year> <copyright holders>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
6
README.md
Normal file
6
README.md
Normal file
|
@ -0,0 +1,6 @@
|
|||
# Écowatt Py3status
|
||||
|
||||
Simple module for Py3status to display [MonEcowatt](https://www.monecowatt.fr/) status
|
||||
|
||||
You will need an app ID and secret to connect to the API; to get one, you must
|
||||
register on the [open-data website of RTE](https://data.rte-france.com/).
|
126
ecowatt.py
Executable file
126
ecowatt.py
Executable file
|
@ -0,0 +1,126 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Ecowatt Data
|
||||
|
||||
Configuration parameters:
|
||||
name: human-readable name to display
|
||||
id: client ID
|
||||
token: secret associated with this ID
|
||||
"""
|
||||
|
||||
import datetime
|
||||
import requests
|
||||
import base64
|
||||
import typing as ty
|
||||
|
||||
|
||||
class Py3status:
|
||||
API_AUTH: str = "https://digital.iservices.rte-france.com/token/oauth/"
|
||||
API_BASE: str = "https://digital.iservices.rte-france.com/open_api/ecowatt/v4"
|
||||
API_SIGNALS: str = "/signals"
|
||||
UPDATE_EVERY = datetime.timedelta(seconds=3600)
|
||||
|
||||
TEXT = "\uf0e7\uf21e" # FontAwesome
|
||||
|
||||
aging_threshold = 2
|
||||
old_threshold = 4
|
||||
|
||||
api_id: str
|
||||
api_secret: str
|
||||
|
||||
auth_expires: datetime.datetime
|
||||
next_update: datetime.datetime
|
||||
bearer_token: str
|
||||
|
||||
api_data = None
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.auth_expires = datetime.datetime.fromtimestamp(0) # force re-auth
|
||||
self.next_update = self.auth_expires
|
||||
self.bearer_token = ""
|
||||
|
||||
def _authenticate(self):
|
||||
"""Authenticate to the OAuth API"""
|
||||
auth_str: str = base64.b64encode(
|
||||
f"{self.api_id}:{self.api_secret}".encode("utf8")
|
||||
).decode("utf8")
|
||||
response = requests.post(
|
||||
self.API_AUTH, headers={"Authorization": f"Basic {auth_str}"}
|
||||
)
|
||||
if response.status_code != 200:
|
||||
raise Exception(f"Failed to authenticate: {response.body()}")
|
||||
json_response = response.json()
|
||||
self.bearer_token = (
|
||||
f"{json_response['token_type']} {json_response['access_token']}"
|
||||
)
|
||||
self.auth_expires = datetime.datetime.now() + datetime.timedelta(
|
||||
seconds=int(json_response["expires_in"] - 60)
|
||||
)
|
||||
|
||||
def _update(self):
|
||||
if datetime.datetime.now() < self.next_update:
|
||||
return
|
||||
|
||||
if datetime.datetime.now() > self.auth_expires:
|
||||
self._authenticate()
|
||||
|
||||
response = requests.get(
|
||||
self.API_BASE + self.API_SIGNALS,
|
||||
headers={
|
||||
"Authorization": self.bearer_token,
|
||||
},
|
||||
)
|
||||
now = datetime.datetime.now()
|
||||
if response.status_code == 429:
|
||||
print(response.headers)
|
||||
self.next_update = now + datetime.timedelta(
|
||||
seconds=int(response.headers["Retry-After"]) + 10
|
||||
)
|
||||
self.api_data = None
|
||||
return
|
||||
if response.status_code != 200:
|
||||
raise Exception(f"Failed to get data: {response.body()}")
|
||||
self.api_data = response.json()
|
||||
self.next_update = now + self.UPDATE_EVERY
|
||||
|
||||
def ecowatt(self):
|
||||
if None in [self.api_id, self.api_secret]:
|
||||
return {"full_text": "Must be configured: api_id, api_secret"}
|
||||
self._update()
|
||||
|
||||
if self.api_data is None:
|
||||
return {
|
||||
"full_text": f"{self.TEXT} [?]",
|
||||
"color": self.py3.COLOR_DEGRADED,
|
||||
"urgent": False,
|
||||
}
|
||||
|
||||
day_data = None
|
||||
for day in self.api_data["signals"]:
|
||||
if (
|
||||
datetime.datetime.fromisoformat(day["jour"]).date()
|
||||
== datetime.date.today()
|
||||
):
|
||||
day_data = day
|
||||
break
|
||||
else:
|
||||
raise Exception("Current date not found in data")
|
||||
|
||||
cur_hour = datetime.datetime.now().hour
|
||||
hour_data = day["values"][cur_hour]
|
||||
assert int(hour_data["pas"]) == cur_hour
|
||||
|
||||
alert_val = max(int(hour_data["hvalue"]), int(day_data["dvalue"]))
|
||||
|
||||
color = self.py3.COLOR_GOOD
|
||||
if alert_val == 2:
|
||||
color = self.py3.COLOR_DEGRADED
|
||||
if alert_val > 2:
|
||||
color = self.py3.COLOR_BAD
|
||||
|
||||
return {
|
||||
"full_text": self.TEXT,
|
||||
"color": color,
|
||||
"urgent": False,
|
||||
}
|
Loading…
Reference in a new issue