Skip to content

Commit

Permalink
Use module as a single source of truth on default Z3K data types
Browse files Browse the repository at this point in the history
  • Loading branch information
defanator committed Oct 16, 2024
1 parent 1f02587 commit 171529c
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 38 deletions.
13 changes: 4 additions & 9 deletions examples/dump_data/dump_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import sys
import json
from datetime import datetime, timedelta
from zont_api import ZontAPI
from zont_api import ZontAPI, DATA_TYPES_Z3K


def main():
Expand All @@ -13,18 +13,13 @@ def main():
dt_to = datetime.now()
dt_from = dt_to - timedelta(minutes=5)

data_types = [
"z3k_temperature",
"z3k_heating_circuit",
"z3k_boiler_adapter",
"z3k_analog_input",
]

data = []

for device in devices:
interval = (dt_from.timestamp(), dt_to.timestamp())
data.append(zapi.load_data(device.id, data_types=data_types, interval=interval))
data.append(
zapi.load_data(device.id, data_types=DATA_TYPES_Z3K, interval=interval)
)

print(json.dumps(data, ensure_ascii=False))

Expand Down
11 changes: 2 additions & 9 deletions examples/load_data/export_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import csv
from http.client import HTTPConnection

from zont_api import ZontAPI, ZontDevice
from zont_api import ZontAPI, ZontDevice, DATA_TYPES_Z3K

logging.basicConfig(
level=logging.INFO,
Expand Down Expand Up @@ -130,15 +130,8 @@ def export_data(
elif args.period == "daily":
targetdir = dt_from.strftime(f"{args.targetdir}/%Y/%Y-%m/%Y-%m-%d")

data_types = [
"z3k_analog_input",
"z3k_temperature",
"z3k_boiler_adapter",
"z3k_heating_circuit",
]

interval = (dt_from.timestamp(), dt_to.timestamp())
data = zapi.load_data(device.id, data_types=data_types, interval=interval)
data = zapi.load_data(device.id, data_types=DATA_TYPES_Z3K, interval=interval)

# analog inputs like power controller
# for analog_input in device.get_analog_inputs():
Expand Down
2 changes: 1 addition & 1 deletion src/zont_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
__license__ = "MIT"
__copyright__ = f"Copyright (c) {__author__}"

from .zont_api import ZontAPIException, ZontAPI, ZontDevice
from .zont_api import ZontAPIException, ZontAPI, ZontDevice, DATA_TYPES_Z3K
from .version import __version__, __release__, __build__
17 changes: 10 additions & 7 deletions src/zont_api/zont_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@
logger = logging.getLogger(__name__)


DATA_TYPES_Z3K = [
"z3k_analog_input",
"z3k_boiler_adapter",
"z3k_heating_circuit",
"z3k_temperature",
]


class ZontAPIException(Exception):
"""
Base class for Zont API exception
Expand Down Expand Up @@ -280,14 +288,9 @@ def load_data(
now = int(time())
interval = (now - 60, now)

# if data types are not specified, use z3k subset
# if data types are not specified, use z3k subset as default
if not data_types:
data_types = [
"z3k_temperature",
"z3k_heating_circuit",
"z3k_boiler_adapter",
"z3k_analog_input",
]
data_types = DATA_TYPES_Z3K

data = {
"requests": [
Expand Down
17 changes: 5 additions & 12 deletions tests/zont_api_load_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,14 @@

import logging
from unittest.mock import patch, MagicMock
from zont_api import ZontAPI, ZontDevice
from zont_api import ZontAPI, ZontDevice, DATA_TYPES_Z3K


__author__ = "Andrei Belov"
__license__ = "MIT"
__copyright__ = f"Copyright (c) {__author__}"


DATA_TYPES_ALL = [
"z3k_temperature",
"z3k_heating_circuit",
"z3k_boiler_adapter",
"z3k_analog_input",
]

MOCK_DATA_EMPTY_RESPONSE = {
"ok": True,
"responses": [
Expand Down Expand Up @@ -125,10 +118,10 @@ def test_load_data_no_series(mock_requests, caplog):
mock_response.json.return_value = MOCK_DATA_EMPTY_RESPONSE
mock_requests.post.return_value = mock_response

data = zapi.load_data(zdev.id, data_types=DATA_TYPES_ALL, interval=(1000, 1120))
data = zapi.load_data(zdev.id, data_types=DATA_TYPES_Z3K, interval=(1000, 1120))

assert data.get("device_id") == zdev.id
for key_name in DATA_TYPES_ALL:
for key_name in DATA_TYPES_Z3K:
assert key_name in data.keys(), f"{key_name} present in load_data response"
assert isinstance(data.get(key_name), dict), f"{key_name} is dict"
assert bool(data.get(key_name)) is False, f"{key_name} dict is empty"
Expand All @@ -153,10 +146,10 @@ def test_load_data_with_metrics(mock_requests, caplog):
mock_response.json.return_value = MOCK_DATA_RESPONSE_WITH_METRICS
mock_requests.post.return_value = mock_response

data = zapi.load_data(zdev.id, data_types=DATA_TYPES_ALL, interval=(1000, 1120))
data = zapi.load_data(zdev.id, data_types=DATA_TYPES_Z3K, interval=(1000, 1120))

assert data.get("device_id") == zdev.id
for key_name in DATA_TYPES_ALL:
for key_name in DATA_TYPES_Z3K:
assert key_name in data.keys(), f"{key_name} present in load_data response"
assert isinstance(data.get(key_name), dict), f"{key_name} is dict"
assert bool(data.get(key_name)) is True, f"{key_name} dict is not empty"
Expand Down

0 comments on commit 171529c

Please sign in to comment.