Skip to content

Commit b0c7518

Browse files
committed
Break out _validate_energy_node() function as suggested
1 parent f4f42b6 commit b0c7518

File tree

1 file changed

+15
-22
lines changed

1 file changed

+15
-22
lines changed

plugwise_usb/network/__init__.py

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -547,17 +547,7 @@ async def allow_join_requests(self, state: bool) -> None:
547547

548548
async def energy_reset_request(self, mac: str) -> None:
549549
"""Send an energy-reset to a Node."""
550-
if not validate_mac(mac):
551-
raise NodeError(f"MAC '{mac}' invalid")
552-
553-
if mac not in self._nodes:
554-
raise NodeError(f"Node {mac} not present in network")
555-
556-
if self._nodes[mac].node_info.node_type.value not in ENERGY_NODE_TYPES:
557-
raise NodeError(
558-
f"Energy-resetting not supported for {self._nodes[mac].node_info.node_type.name}"
559-
)
560-
550+
self._validate_energy_node(mac)
561551
node_protocols = self._nodes[mac].node_protocols
562552
request = CircleClockSetRequest(
563553
self._controller.send,
@@ -582,17 +572,7 @@ async def set_energy_intervals(
582572
Default: consumption = 60, production = 0.
583573
For logging energy in both directions set both to 60.
584574
"""
585-
if not validate_mac(mac):
586-
raise NodeError(f"MAC '{mac}' invalid")
587-
588-
if mac not in self._nodes:
589-
raise NodeError(f"Node {mac} not present in network")
590-
591-
if self._nodes[mac].node_info.node_type.value not in ENERGY_NODE_TYPES:
592-
raise NodeError(
593-
f"Setting energy-intervals not supported for {self._nodes[mac].node_info.node_type.name}"
594-
)
595-
575+
self._validate_energy_node(mac)
596576
# Validate input parameters
597577
if consumption <= 0:
598578
raise ValueError("Consumption interval must be positive")
@@ -613,6 +593,19 @@ async def set_energy_intervals(
613593
f"Unknown NodeResponseType '{response.response_type.name}' received"
614594
)
615595

596+
def _validate_energy_node(self, mac: str) -> None:
597+
"""Validate node for energy operations."""
598+
if not validate_mac(mac):
599+
raise NodeError(f"MAC '{mac}' invalid")
600+
601+
if mac not in self._nodes:
602+
raise NodeError(f"Node {mac} not present in network")
603+
604+
if self._nodes[mac].node_info.node_type.value not in ENERGY_NODE_TYPES:
605+
raise NodeError(
606+
f"Energy operations not supported for {self._nodes[mac].node_info.node_type.name}"
607+
)
608+
616609
def subscribe_to_node_events(
617610
self,
618611
node_event_callback: Callable[[NodeEvent, str], Coroutine[Any, Any, None]],

0 commit comments

Comments
 (0)