wattpilot-api 1.2.0__tar.gz → 1.4.0__tar.gz

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.
@@ -65,3 +65,4 @@ Thumbs.db
65
65
  *.log
66
66
  .idea/
67
67
  site-packages/
68
+ .claude/
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: wattpilot-api
3
- Version: 1.2.0
3
+ Version: 1.4.0
4
4
  Summary: Async Python library to connect to a Fronius Wattpilot Wallbox
5
5
  Project-URL: Homepage, https://github.com/ruaan-deysel/wattpilot-api
6
6
  Project-URL: Documentation, https://github.com/ruaan-deysel/wattpilot-api#readme
@@ -1,3 +1,3 @@
1
1
  """Version information for wattpilot-api."""
2
2
 
3
- __version__ = "1.2.0"
3
+ __version__ = "1.4.0"
@@ -57,12 +57,18 @@ class Wattpilot:
57
57
  cloud: bool = False,
58
58
  connect_timeout: float = 30.0,
59
59
  init_timeout: float = 30.0,
60
+ auto_reconnect: bool = True,
61
+ reconnect_delay_min: float = 5.0,
62
+ reconnect_delay_max: float = 300.0,
60
63
  ) -> None:
61
64
  self._host = host
62
65
  self._password = password
63
66
  self._cloud = cloud
64
67
  self._connect_timeout = connect_timeout
65
68
  self._init_timeout = init_timeout
69
+ self._auto_reconnect = auto_reconnect
70
+ self._reconnect_delay_min = reconnect_delay_min
71
+ self._reconnect_delay_max = reconnect_delay_max
66
72
 
67
73
  self._device = DeviceInfo(serial=serial or "")
68
74
  self._hashed_password: bytes = b""
@@ -597,6 +603,11 @@ class Wattpilot:
597
603
 
598
604
  Handles timestamp conversion and DST adjustment automatically
599
605
  based on the charger's ``tds`` (daylight-saving) property.
606
+
607
+ The ``tds`` property indicates which DST *scheme* is configured
608
+ (``1`` = European Summer Time, ``2`` = US Daylight Time), **not**
609
+ whether DST is currently active. The offset is only applied when
610
+ the system's local timezone reports that DST is in effect.
600
611
  """
601
612
  if isinstance(departure_time, datetime.datetime):
602
613
  departure_time = departure_time.time()
@@ -605,7 +616,9 @@ class Wattpilot:
605
616
 
606
617
  tds = self._all_props.get("tds")
607
618
  if tds is not None and int(tds) in (1, 2):
608
- timestamp += 3600
619
+ local_now = datetime.datetime.now().astimezone()
620
+ if local_now.dst() and local_now.dst() != datetime.timedelta(0):
621
+ timestamp += 3600
609
622
 
610
623
  await self.set_property("ftt", timestamp)
611
624
 
@@ -785,13 +798,42 @@ class Wattpilot:
785
798
 
786
799
  async def _message_loop(self) -> None:
787
800
  assert self._ws is not None
801
+ reconnect_delay = self._reconnect_delay_min
788
802
  try:
789
- async for raw in self._ws:
790
- if isinstance(raw, bytes):
791
- raw = raw.decode("utf-8")
792
- await self._handle_message(raw)
793
- except websockets.exceptions.ConnectionClosed: # pragma: no cover
794
- _LOGGER.info("WebSocket connection closed")
803
+ while True:
804
+ try:
805
+ async for raw in self._ws:
806
+ if isinstance(raw, bytes):
807
+ raw = raw.decode("utf-8")
808
+ await self._handle_message(raw)
809
+ except websockets.exceptions.ConnectionClosed:
810
+ _LOGGER.info("WebSocket connection closed")
811
+
812
+ self._connected = False
813
+ self._connected_event.clear()
814
+
815
+ if not self._auto_reconnect:
816
+ break
817
+
818
+ # Stop reconnecting on authentication failure — retrying with the
819
+ # same rejected credentials would loop forever.
820
+ if self._auth_error is not None:
821
+ _LOGGER.error(
822
+ "Not reconnecting after authentication failure: %s",
823
+ self._auth_error,
824
+ )
825
+ break
826
+
827
+ _LOGGER.info("Reconnecting in %.0fs...", reconnect_delay)
828
+ await asyncio.sleep(reconnect_delay)
829
+ try:
830
+ self._all_props_initialized = False
831
+ self._initialized_event.clear()
832
+ self._ws = await websockets.asyncio.client.connect(self._url)
833
+ reconnect_delay = self._reconnect_delay_min
834
+ except (OSError, websockets.exceptions.WebSocketException) as exc:
835
+ reconnect_delay = min(reconnect_delay * 2, self._reconnect_delay_max)
836
+ _LOGGER.warning("Reconnect failed: %s, retrying in %.0fs", exc, reconnect_delay)
795
837
  finally:
796
838
  self._connected = False
797
839
  self._connected_event.clear()
@@ -949,12 +991,16 @@ class Wattpilot:
949
991
  self._amp = value
950
992
  case "version":
951
993
  self._version = value
952
- case "ast":
953
- self._access_state = value
954
994
  case "fwv":
955
995
  self._firmware = value
956
996
  case "wss":
957
997
  self._wifi_ssid = value
998
+ case "ccw":
999
+ # Current firmware reports the connected AP here rather than
1000
+ # in ``wss``; the value is an object carrying the SSID.
1001
+ ssid = getattr(value, "ssid", None)
1002
+ if ssid:
1003
+ self._wifi_ssid = ssid
958
1004
 
959
1005
  # Fire property callbacks
960
1006
  for cb in self._property_callbacks:
@@ -94,6 +94,15 @@ def decode_property(pd: dict[str, Any], value: Any) -> Any:
94
94
  return remapped
95
95
 
96
96
 
97
+ def _sanitise_topic_value(value: str) -> str:
98
+ """Escape ``{`` and ``}`` in a value so it cannot be misinterpreted as a format
99
+ specifier when the value is later substituted into a topic template via
100
+ :func:`str.format`. This prevents unexpected ``KeyError`` or value injection
101
+ when the value originates from an untrusted source such as an MQTT message topic.
102
+ """
103
+ return value.replace("{", "{{").replace("}", "}}")
104
+
105
+
97
106
  def substitute_topic(
98
107
  template: str,
99
108
  values: dict[str, str],
@@ -102,11 +111,16 @@ def substitute_topic(
102
111
  topic_base: str = "",
103
112
  expand_tilde: bool = True,
104
113
  ) -> str:
105
- """Substitute ``{placeholders}`` in an MQTT topic template."""
114
+ """Substitute ``{placeholders}`` in an MQTT topic template.
115
+
116
+ Values are sanitised before substitution so that any ``{`` / ``}`` characters
117
+ inside a value cannot be interpreted as additional format specifiers (OWASP A03).
118
+ """
106
119
  s = template
107
120
  if expand_tilde:
108
121
  s = re.sub(r"^~", topic_property_base, s)
109
- all_values = {"baseTopic": topic_base} | values
122
+ safe_values = {k: _sanitise_topic_value(v) for k, v in values.items()}
123
+ all_values = {"baseTopic": _sanitise_topic_value(topic_base)} | safe_values
110
124
  return s.format(**all_values)
111
125
 
112
126
 
File without changes
File without changes