Skip to content

Commit 0848e5a

Browse files
committed
Implement energy-reset functions
1 parent 4983462 commit 0848e5a

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

plugwise_usb/__init__.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from collections.abc import Callable, Coroutine
1111
from functools import wraps
1212
import logging
13-
from typing import Any, TypeVar, cast, Final
13+
from typing import Any, Final, TypeVar, cast
1414

1515
from .api import NodeEvent, PlugwiseNode, StickEvent
1616
from .connection import StickController
@@ -210,6 +210,20 @@ async def set_accept_join_request(self, state: bool) -> bool:
210210
raise NodeError(f"Failed setting accept joining: {exc}") from exc
211211
return True
212212

213+
async def energy_reset_request(self, mac: str) -> bool:
214+
"""Send an energy-reset request to a Node."""
215+
try:
216+
await self._network.energy_reset_request(mac)
217+
except (MessageError, NodeError) as exc:
218+
raise NodeError(f"{exc}") from exc
219+
220+
# Follow up by an energy-intervals (re)set
221+
result = await self.set_energy_intervals(mac, 60, 0)
222+
if result:
223+
return True
224+
225+
return False
226+
213227
async def set_energy_intervals(
214228
self, mac: str, cons_interval: int, prod_interval: int
215229
) -> bool:
@@ -295,7 +309,7 @@ async def connect(self, port: str | None = None) -> None:
295309
"Unable to connect. " +
296310
"Path to USB-Stick is not defined, set port property first"
297311
)
298-
312+
299313
await self._controller.connect_to_stick(
300314
self._port,
301315
)

plugwise_usb/network/__init__.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212

1313
from ..api import NodeEvent, NodeType, PlugwiseNode, StickEvent
1414
from ..connection import StickController
15-
from ..constants import UTF8
15+
from ..constants import UTC, UTF8
1616
from ..exceptions import CacheError, MessageError, NodeError, StickError, StickTimeout
1717
from ..messages.requests import (
18-
CirclePlusAllowJoiningRequest,
18+
CircleClockSetRequest,
1919
CircleMeasureIntervalRequest,
20+
CirclePlusAllowJoiningRequest,
2021
NodePingRequest,
2122
)
2223
from ..messages.responses import (
@@ -541,6 +542,23 @@ async def allow_join_requests(self, state: bool) -> None:
541542
_LOGGER.debug("Sent AllowJoiningRequest to Circle+ with state=%s", state)
542543
self.accept_join_request = state
543544

545+
async def energy_reset_request(self, mac: str) -> None:
546+
"""Send an energy-reset to a Node."""
547+
request = CircleClockSetRequest(
548+
self._send,
549+
self._mac_in_bytes,
550+
datetime.now(tz=UTC),
551+
self._node_protocols.max,
552+
True,
553+
)
554+
if (response := await request.send()) is None:
555+
raise NodeError(f"Energy-reset for {mac} failed")
556+
557+
if response.ack_id != NodeResponseType.CLOCK_ACCEPTED:
558+
raise MessageError(
559+
f"Unknown NodeResponseType '{response.response_type.name}' received"
560+
)
561+
544562
async def set_energy_intervals(
545563
self, mac: str, consumption: int, production: int
546564
) -> None:

0 commit comments

Comments
 (0)