python-siseli 0.2.0__py3-none-any.whl → 0.2.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: python-siseli
3
- Version: 0.2.0
3
+ Version: 0.2.2
4
4
  Summary: Python SDK for Siseli Cloud
5
5
  License: MIT
6
6
  Requires-Python: >=3.11
@@ -10,7 +10,7 @@ siseli/device.py,sha256=crix14pYakrDaFmomPC3Uwgdq-1boZ-ZaEg2HgzXibs,2762
10
10
  siseli/dictionary.py,sha256=VSO83uXPaGLPzKGRmpROuWQKivXN7DNOmXN8QQJ-Y-w,1120
11
11
  siseli/exceptions.py,sha256=y9ws9c2vr5Q3bZxLwkXybUqQooq57YEEXAN0wO3_fxk,1420
12
12
  siseli/history.py,sha256=CgBvDfZBIrrewu7PeRv08-Zr4boEVOY8Xevruu-ge-I,3869
13
- siseli/open_auth.py,sha256=t64FrrzS-edsQlLr-tY0MHVvHP0nfNJEd1LcZPw_yXQ,5857
13
+ siseli/open_auth.py,sha256=w3Jq7ZA2m3d-A_ZR_O8G1bODHvcUJ4Rc1eOS4uq69l0,5570
14
14
  siseli/state.py,sha256=d7Exlf7FzF8O_CcF-NbAt6kq8qaR4fFhjGW8028kbrM,5800
15
15
  siseli/station.py,sha256=e1OK4OIry9WUjT8KggQI5b4Acypm9D31mWAMTjDGFew,5531
16
16
  siseli/models/__init__.py,sha256=WSIXRM_JRKNhB_hDwxrpeQgdqKUHdbT3KiFvdrLrrzY,1242
@@ -22,9 +22,9 @@ siseli/models/dashboard.py,sha256=P86feNI8Yn6ADkwDGFQJFUESrKLt6VttGqsEiNzGnZA,11
22
22
  siseli/models/device.py,sha256=u57-csypaF-c5hAimqb8GIMeZqYZMKMhYFBTVuBRSfQ,657
23
23
  siseli/models/dictionary.py,sha256=d6KCu77UMlPcIDON8G3sjuytkSfbI5go1y3I_jAGTX8,422
24
24
  siseli/models/history.py,sha256=dx26VWLJosucMvwL36vojvAguvkMiv5GEKTllUtZV0Y,791
25
- siseli/models/state.py,sha256=VK5CNqMLRkY1w9HrcUhvaO_6J-rjqP7VwvbppCFpucI,2393
25
+ siseli/models/state.py,sha256=WXONHjGx9Ro87mJqn1Y0T-nt3PJKOTbWXAyzqZ2GX48,4749
26
26
  siseli/models/station.py,sha256=02OLINAVblnKkF73ZwKxYvPtUFROsu4-g544oDg8ApA,1783
27
- python_siseli-0.2.0.dist-info/METADATA,sha256=qHA8H1BcZ_7_LZKWG7mBP5sw61JOUuWXSXHzF58hRFg,1439
28
- python_siseli-0.2.0.dist-info/WHEEL,sha256=K260EYznzXsJYBQGqmI8VTxEdiZYNvDZwW9cBh9-_MA,91
29
- python_siseli-0.2.0.dist-info/top_level.txt,sha256=Dofr9c7hGvRRV6ij6aoPuAZqxEjkgtsBeN4KtFHbGgo,7
30
- python_siseli-0.2.0.dist-info/RECORD,,
27
+ python_siseli-0.2.2.dist-info/METADATA,sha256=m7CXqxigJiQL4vA3P9cqZ-RnYJKPwnl9onCyRBbs0Kc,1439
28
+ python_siseli-0.2.2.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
29
+ python_siseli-0.2.2.dist-info/top_level.txt,sha256=Dofr9c7hGvRRV6ij6aoPuAZqxEjkgtsBeN4KtFHbGgo,7
30
+ python_siseli-0.2.2.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (83.0.0)
2
+ Generator: setuptools (84.0.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
siseli/models/state.py CHANGED
@@ -9,6 +9,19 @@ from typing import Any
9
9
  from .common import LookupValue
10
10
 
11
11
 
12
+ def _numeric(value: Any) -> float | None:
13
+ """Return finite numeric telemetry as a float, otherwise None."""
14
+ if value is None or isinstance(value, bool):
15
+ return None
16
+ try:
17
+ number = float(value)
18
+ except (TypeError, ValueError, OverflowError):
19
+ return None
20
+ if number != number or number in (float("inf"), float("-inf")):
21
+ return None
22
+ return number
23
+
24
+
12
25
  @dataclass
13
26
  class StateAttribute:
14
27
  """A single telemetry attribute."""
@@ -76,6 +89,52 @@ class DeviceState:
76
89
  """Return an attribute by its key, or *None* if not present."""
77
90
  return self.fields.get(key)
78
91
 
92
+ def to_sensor_data(self) -> dict[str, Any]:
93
+ """Return stable, typed semantic telemetry fields for integrations.
94
+
95
+ API-specific field names, unit conversions, and derived battery values
96
+ belong here rather than in consumer integrations. Invalid numeric
97
+ telemetry is represented as None. Power values reported in kW by this
98
+ device are converted to W.
99
+ """
100
+ def value(api_key: str) -> Any:
101
+ attribute = self.fields.get(api_key)
102
+ return attribute.value if attribute is not None else None
103
+
104
+ def number(api_key: str) -> float | None:
105
+ return _numeric(value(api_key))
106
+
107
+ voltage = number("batteryVoltage")
108
+ charging = number("batteryChargingCurrent")
109
+ discharging = number("batteryDischargeCurrent")
110
+ current = (
111
+ charging - discharging
112
+ if charging is not None and discharging is not None
113
+ else None
114
+ )
115
+ pv_power = number("generationPower")
116
+ load_power = number("acOutputActivePower")
117
+ battery_power = (
118
+ voltage * current
119
+ if voltage is not None and current is not None
120
+ else None
121
+ )
122
+ return {
123
+ "battery_soc": number("batteryCapacity"),
124
+ "battery_voltage": voltage,
125
+ "battery_current": current,
126
+ "battery_power": battery_power,
127
+ "pv_voltage": number("pvInputVoltage1"),
128
+ "pv_current": number("pv1InputCurrentForBattery"),
129
+ "pv_power": pv_power * 1000 if pv_power is not None else None,
130
+ "grid_voltage": number("gridVoltage"),
131
+ "grid_frequency": number("gridFrequency"),
132
+ "output_voltage": number("acOutputVoltage"),
133
+ "output_frequency": number("acOutputFrequency"),
134
+ "load_power": load_power * 1000 if load_power is not None else None,
135
+ "inverter_state": value("workingMode"),
136
+ }
137
+
79
138
 
80
139
  @dataclass
81
140
  class FlowNode:
siseli/open_auth.py CHANGED
@@ -7,18 +7,20 @@ client (CryptoJS-based) exactly:
7
7
  1. Decrypt the base64-encoded app secret with AES-128-CBC.
8
8
  Key and IV are derived from MD5(app_id) as ASCII hex chars.
9
9
  2. Compute a body hash: empty string for GET; SHA-256 hex for everything else.
10
- 3. Collect query params, inject the three open-auth fields, sort by key,
11
- serialise without URL-encoding, hex-encode the UTF-8 bytes.
12
- 4. Sign: HMAC-SHA256(hex_string, secret) → MD5 → lowercase hex.
10
+ 3. Sign input: ``IOT-Open-AppID``, ``IOT-Open-Body-Hash`` and
11
+ ``IOT-Open-Nonce`` only, sorted by key, joined as ``k=v&k=v`` without
12
+ URL-encoding, then base64-encoded (UTF-8).
13
+ 4. Sign: HMAC-SHA256(base64_string, secret) raw digest → MD5 → lowercase hex.
14
+ 5. The body hash is part of the signature but is NOT sent as an HTTP header.
13
15
  """
14
16
 
15
17
  from __future__ import annotations
16
18
 
19
+ import base64
17
20
  import hashlib
18
21
  import hmac
19
22
  import uuid
20
23
  from typing import TYPE_CHECKING, Any
21
- from urllib.parse import parse_qs
22
24
 
23
25
  from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
24
26
 
@@ -37,8 +39,6 @@ def decrypt_open_secret(app_id: str, encrypted_secret_b64: str) -> str:
37
39
 
38
40
  ZeroPadding is assumed: trailing null bytes are stripped from the output.
39
41
  """
40
- import base64
41
-
42
42
  md5_hex = hashlib.md5(app_id.encode()).hexdigest() # noqa: S324
43
43
  key = md5_hex[:16].encode("ascii")
44
44
  iv = md5_hex[16:].encode("ascii")
@@ -72,7 +72,8 @@ def build_open_headers(
72
72
  method:
73
73
  HTTP method ("GET", "POST", …).
74
74
  query:
75
- The raw query string portion of the URL (without the leading "?").
75
+ The raw query string portion of the URL. Accepted for API
76
+ compatibility; it does not take part in the signature.
76
77
  body:
77
78
  Raw request body bytes, or *None* / empty for GET requests.
78
79
  app_id:
@@ -90,39 +91,28 @@ def build_open_headers(
90
91
  else:
91
92
  body_hash = _sha256_hex(body if body else b"")
92
93
 
93
- # --- params for the sign input -------------------------------------------
94
- params: dict[str, str] = {}
95
- if query:
96
- for key, values in parse_qs(query, keep_blank_values=True).items():
97
- params[key] = values[0]
98
-
99
- # Remove any stale open-auth fields so they cannot influence the new sign.
100
- for field in ("IOT-Open-AppID", "IOT-Open-Nonce", "IOT-Open-Sign", "IOT-Open-Body-Hash"):
101
- params.pop(field, None)
102
-
103
- params["IOT-Open-AppID"] = app_id
104
- params["IOT-Open-Nonce"] = nonce
105
- params["IOT-Open-Body-Hash"] = body_hash
106
-
107
- # Sort alphabetically (case-sensitive, matching JS default sort order).
108
- query_str = "&".join(f"{k}={v}" for k, v in sorted(params.items()))
94
+ # --- sign input: exactly three params, sorted, no URL-encoding -----------
95
+ params = {
96
+ "IOT-Open-AppID": app_id,
97
+ "IOT-Open-Body-Hash": body_hash,
98
+ "IOT-Open-Nonce": nonce,
99
+ }
100
+ signing_string = "&".join(f"{k}={v}" for k, v in sorted(params.items()))
109
101
 
110
- # --- sign ----------------------------------------------------------------
111
- # CryptoJS: Utf8.parse(query_str) → Hex.stringify → hex_string
112
- # Then: HmacSHA256(hex_string, secret) → MD5 → hex
113
- hex_str = query_str.encode("utf-8").hex()
102
+ # --- sign: base64 -> HMAC-SHA256 (raw digest) -> MD5 hex -----------------
103
+ base64_input = base64.b64encode(signing_string.encode("utf-8")).decode("ascii")
114
104
  hmac_digest = hmac.new(
115
105
  app_secret.encode("utf-8"),
116
- hex_str.encode("utf-8"),
106
+ base64_input.encode("ascii"),
117
107
  hashlib.sha256,
118
108
  ).digest()
119
109
  sign = hashlib.md5(hmac_digest).hexdigest() # noqa: S324
120
110
 
111
+ # The body hash takes part in the signature but is not sent as a header.
121
112
  return {
122
113
  "IOT-Time-Zone": timezone,
123
114
  "IOT-Open-AppID": app_id,
124
115
  "IOT-Open-Nonce": nonce,
125
- "IOT-Open-Body-Hash": body_hash,
126
116
  "IOT-Open-Sign": sign,
127
117
  }
128
118
 
@@ -141,9 +131,10 @@ def attach_open_auth(
141
131
  - ``IOT-Time-Zone``
142
132
  - ``IOT-Open-AppID``
143
133
  - ``IOT-Open-Nonce``
144
- - ``IOT-Open-Body-Hash``
145
134
  - ``IOT-Open-Sign``
146
135
 
136
+ ``IOT-Open-Body-Hash`` is part of the signature but is not sent.
137
+
147
138
  Parameters
148
139
  ----------
149
140
  client: