Skip to content

Commit

Permalink
V1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
signag committed Sep 2, 2024
1 parent 7dddc25 commit ae7ee6e
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 37 deletions.
20 changes: 12 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ You may alternatively run monitorViHP in a virtual Python environment from the c

1. Login to the [Viessmann Developer Portal](https://app.developer.viessmann.com/) with your existing ViCare app username/password.
2. On the developer dashboard click add in the clients section.
3. reate a new client using following data:
3. Create a new client using following data:
- Name: PyViCare
- Google reCAPTCHA: Disabled
- Redirect URIs: vicare://oauth-callback/everest
Expand All @@ -65,6 +65,8 @@ You may alternatively run monitorViHP in a virtual Python environment from the c

Configuration for **monitorViHP** needs to be provided in a specific configuration file.

**NOTE:** See [Running monitorViHP as Python Program](#running-monitorvihp-as-python-program) how you can test that a configuration file is valid.

By default, a configuration file "monitorViHP.json" is searched in the given sequence under ```$ROOT/config```, ```$HOME/.config``` or under ```/etc```. <br>Here, ```$ROOT``` is the project root directory and ```$HOME``` is the home directory of the user running the program.

For testing in a development environment, primarily the location ```$ROOT/tests/data``` is searched for a configuration file.
Expand Down Expand Up @@ -165,7 +167,7 @@ A template can be found under
|-- **value** | Rule for determining the value for the tag or field |
|--- source | Source where to get the value from.<br>May be<br>- "value" if the value is a constant value (only to be used for tags)<br>- "cfg" when referring to this configuration<br>- "vicare" if referring to a specific ViCare feature |
|--- feature | 'Feature' key depending on source:<br>- constant value<br>- key within this configuration<br>- ViCare feature key|
|--- formula | Only for ```"source"="vicare"```: Python expression how to get the value from the ```properties``` of the ViCare feature. |
|--- formula | Only for ```"source"="vicare"```: Python expression how to get the value from the ```properties``` of the ViCare feature.<br>See [monitorViHP_tpl.json](./config/monitorViHP_tpl.json) for examples |
|--- type | Type with which the value shall be stored in Influx.<br>- "int"<br>- "float"<br>- "str"

### Getting Tag and Field Values from ViCare Features
Expand Down Expand Up @@ -432,18 +434,20 @@ usage: monitorViHP.py [-h] [-t] [-s] [-l] [-L] [-F] [-p LOGFILE] [-f FILE] [-v]

This package includes a small [getViCareInstallation.py](./tryViCareApi/getViCareInstallation.py) Python program which can be used to get information on your installation from ViCare.

Before usage, copy this file to ```$PARENTDIR/tests``` and name it according to your needs.
(```$PARENTDIR/tests``` is ignored by git)
Before usage, copy this file to ```$PARENT/tests```.
(```$PARENT/tests``` is ignored by git)

1. Set 'client_id', 'email' and 'password' for your ViCare access (See [Gaining Access to ViCare Developer Portal](#gaining-access-to-vicare-developer-portal))
2. Run the program in the [virtual environment for monitorViHP](#running-monitorvihp-as-python-program).<br>```python getViCareInstallation.py```<br>The program will print out general information on your installation, among others: the installation ID
1. Edit ```$PARENT/tests/getViCareInstallation.py``` and set 'client_id', 'email' and 'password' for your ViCare access (See [Gaining Access to ViCare Developer Portal](#gaining-access-to-vicare-developer-portal))
2. Change to the ```$PARENT/monitorViHP``` directory and activate the virtual environment, if not already done:<br>```.venv\Scripts\activate``` (Windows)<br>```source .venv/bin/activate``` (Linux)
3. Run the program:<br>```python tests/getViCareInstallation.py```<br>The program will print out general information on your installation, among others: the **Installation ID**

## How to get a List with ViCare Features

This package includes a small [getViCareAllFeatures.py](./tryViCareApi/getViCareAllFeatures.py) Python program which can be used to generate a list of features available for a device.

Before usage, copy this file to ```$PARENTDIR/tests``` and name it according to your needs.
Before usage, copy this file to ```$PARENTDIR/tests```.
(```$PARENTDIR/tests``` is ignored by git)

1. Set 'client_id', 'email' and 'password' for your ViCare access (See [Gaining Access to ViCare Developer Portal](#gaining-access-to-vicare-developer-portal))
2. Run the program in the [virtual environment for monitorViHP](#running-monitorvihp-as-python-program).<br>```python getViCareAllFeatures.py```<br>The program will print out a JSON structure with all available ViCare features:
2. Change to the ```$PARENT/monitorViHP``` directory and activate the virtual environment, if not already done:<br>```.venv\Scripts\activate``` (Windows)<br>```source .venv/bin/activate``` (Linux)
3. Run the program:<br>```python tests/getViCareAllFeatures.py```<br>The program will generate a ```$PARENTDIR/tests/features.json``` file with all available ViCare features.
63 changes: 50 additions & 13 deletions monitorViHP/monitorViHP.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@
import influxdb_client
from influxdb_client.client.write_api import SYNCHRONOUS
from PyViCare.PyViCare import PyViCare
from PyViCare.PyViCareUtils import PyViCareRateLimitError
from PyViCare.PyViCareUtils import (
PyViCareBrowserOAuthTimeoutReachedError,
PyViCareCommandError,
PyViCareInternalServerError,
PyViCareRateLimitError,
)

# Set up logging
import logging
Expand Down Expand Up @@ -617,7 +622,7 @@ def dictFromFeatureList(features: list) -> dict:
getCl()

logger.info("=============================================================")
logger.info("monitorViHP V1.2 started")
logger.info("monitorViHP V1.3 started")
logger.info("=============================================================")

# Get configuration
Expand All @@ -629,7 +634,7 @@ def dictFromFeatureList(features: list) -> dict:
stop = False

try:
# Instatntiate InfluxDB access
# Instantiate InfluxDB access
if cfg["InfluxOutput"]:
influxClient = influxdb_client.InfluxDBClient(
url=cfg["InfluxURL"], token=cfg["InfluxToken"], org=cfg["InfluxOrg"]
Expand All @@ -645,17 +650,9 @@ def dictFromFeatureList(features: list) -> dict:
influxClient = None
influxWriteAPI = None

try:
# Log in ViCare
session = getPyViCareSession()
except Exception as error:
logger.critical("Unexpected Exception (%s): %s", error.__class__, error.__cause__)
logger.critical("Unexpected Exception: %s", error)
logger.critical("Could not get ViCare access")
stop = True

noWait = False
waitUntilMidnight = False
serverErrorWait = 2 * cfg["measurementInterval"] if cfg["measurementInterval"] > 300 else 300

while not stop:
try:
Expand All @@ -674,6 +671,9 @@ def dictFromFeatureList(features: list) -> dict:
)
mTS = UTC_datetimeRounded.strftime("%Y-%m-%dT%H:%M:%S.%f000Z")

# Get session
session = getPyViCareSession()

# Get all Features
d = int(cfg["vicareDevice"])
device = session.devices[d]
Expand All @@ -686,6 +686,10 @@ def dictFromFeatureList(features: list) -> dict:
for measurement in measurements:
storeViCaraData(influxWriteAPI, featureDict, measurement)

del session

serverErrorWait = 2 * cfg["measurementInterval"] if cfg["measurementInterval"] > 300 else 300

logger.info("monitorViHP - cycle completed")

if testRun:
Expand All @@ -695,24 +699,57 @@ def dictFromFeatureList(features: list) -> dict:
except PyViCareRateLimitError as error:
stop = False
waitUntilMidnight = True
logger.error("Rate limit reached: %s", error)
serverErrorWait = 2 * cfg["measurementInterval"] if cfg["measurementInterval"] > 300 else 300
logger.error("Rate limit reached: %s. Waiting untilmidnight", error)

except PyViCareInternalServerError as error:
logger.error(
"PyViCareInternalServerError error %s. Waiting %s sec",
error,
serverErrorWait,
)
time.sleep(serverErrorWait)
if serverErrorWait < 3600 * 4:
serverErrorWait = serverErrorWait * 2

except PyViCareBrowserOAuthTimeoutReachedError as error:
logger.error(
"PyViCareBrowserOAuthTimeoutReachedError error %s. Waiting %s sec",
error,
serverErrorWait,
)
time.sleep(serverErrorWait)
if serverErrorWait < 3600 * 4:
serverErrorWait = serverErrorWait * 2

except PyViCareCommandError as error:
logger.error(
"PyViCareCommandError error %s. Waiting %s sec", error, serverErrorWait
)
time.sleep(serverErrorWait)
if serverErrorWait < 3600 * 4:
serverErrorWait = serverErrorWait * 2

except Exception as error:
stop = True
logger.critical("Unexpected Exception: %s", error)
if influxClient:
del influxClient
influxClient = None
if influxWriteAPI:
del influxWriteAPI
influxWriteAPI = None
raise error

except KeyboardInterrupt:
stop = True
logger.debug("KeyboardInterrupt")
if influxClient:
del influxClient
influxClient = None
if influxWriteAPI:
del influxWriteAPI
influxWriteAPI = None
if influxClient:
del influxClient
if influxWriteAPI:
Expand Down
16 changes: 8 additions & 8 deletions tryViCareAPI/getViCareAllFeatures.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import sys
import logging
from PyViCare.PyViCare import PyViCare
from functools import wraps
from typing import Callable
from PyViCare import Feature
from PyViCare.PyViCareUtils import PyViCareNotSupportedFeatureError
import json
#from myUtils import HandleNotSupported

client_id = ""
email = ""
password = ""
file = "./tests/features.json"

vicare = PyViCare()
vicare.initWithCredentials(email, password, client_id, "token.save")

device = vicare.devices[1]
print(json.dumps(device.service.fetch_all_features()))

fJson = json.dumps(
device.service.fetch_all_features(),
indent=4,
)
with open(file, mode="w", encoding="utf-8") as f:
f.write(fJson)
print("Features have been written to %s", file)
8 changes: 0 additions & 8 deletions tryViCareAPI/getViCareInstallation.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
import sys
import logging
from PyViCare.PyViCare import PyViCare
from functools import wraps
from typing import Callable
from PyViCare import Feature
from PyViCare.PyViCareUtils import PyViCareNotSupportedFeatureError
import json
# from myUtils import HandleNotSupported

client_id = ""
email = ""
Expand Down

0 comments on commit ae7ee6e

Please sign in to comment.