Skip to content

Commit 39e2ef1

Browse files
dccoteclaude
andcommitted
Call CommunicationPort primitives directly in PwrUSBDevice
Problem: The do* methods went through private _sendCommandByte and _queryCommand wrappers, which added nothing over the CommunicationPort API and hid the actual port calls. Solution: Drop both helpers. Each write now calls self.port.writeData(...) directly, and each query inlines flush()/writeData()/readData() (the flush stays because a report can exceed the requested length, leaving a buffered remainder). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent ebb4e7e commit 39e2ef1

1 file changed

Lines changed: 15 additions & 16 deletions

File tree

hardwarelibrary/powerstrips/pwrusb.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -172,18 +172,13 @@ def _validateOutlet(self, outlet: int):
172172
raise ValueError("Outlet must be 1..{0}, got {1}".format(
173173
self.switchableOutletCount, outlet))
174174

175-
def _sendCommandByte(self, byte: int):
176-
self.port.writeData(bytearray([byte]))
177-
178-
def _queryCommand(self, byte: int, replyLength: int) -> bytearray:
179-
# Drop any leftover bytes from a previous report so this reply is read
180-
# from a clean buffer (reports can be longer than the bytes we need).
181-
self.port.flush()
182-
self._sendCommandByte(byte)
183-
return self.port.readData(length=replyLength)
184-
185175
def deviceType(self) -> str:
186-
reply = self._queryCommand(self.getDeviceTypeCommand, 1)
176+
# flush() first so the reply is read from a clean buffer: a report can be
177+
# longer than the bytes we need, leaving a remainder buffered from the
178+
# previous query.
179+
self.port.flush()
180+
self.port.writeData(bytearray([self.getDeviceTypeCommand]))
181+
reply = self.port.readData(length=1)
187182
return self.deviceTypes.get(reply[0], "Unknown")
188183

189184
def doGetOutletCount(self) -> int:
@@ -192,7 +187,7 @@ def doGetOutletCount(self) -> int:
192187
def doSetOutletState(self, outlet: int, isOn: bool):
193188
self._validateOutlet(outlet)
194189
key = "on" if isOn else "off"
195-
self._sendCommandByte(self.outletCommands[key][outlet - 1])
190+
self.port.writeData(bytearray([self.outletCommands[key][outlet - 1]]))
196191
self._outletStateCache[outlet - 1] = bool(isOn)
197192

198193
def doGetOutletState(self, outlet: int) -> bool:
@@ -202,20 +197,24 @@ def doGetOutletState(self, outlet: int) -> bool:
202197
def doSetOutletDefaultState(self, outlet: int, isOn: bool):
203198
self._validateOutlet(outlet)
204199
key = "defaultOn" if isOn else "defaultOff"
205-
self._sendCommandByte(self.outletCommands[key][outlet - 1])
200+
self.port.writeData(bytearray([self.outletCommands[key][outlet - 1]]))
206201

207202
def doGetCurrent(self) -> float:
208-
reply = self._queryCommand(self.getCurrentCommand, 2)
203+
self.port.flush()
204+
self.port.writeData(bytearray([self.getCurrentCommand]))
205+
reply = self.port.readData(length=2)
209206
milliamps = (reply[0] << 8) | reply[1]
210207
return milliamps / 1000.0
211208

212209
def doGetAccumulatedCharge(self) -> float:
213-
reply = self._queryCommand(self.getChargeCommand, 4)
210+
self.port.flush()
211+
self.port.writeData(bytearray([self.getChargeCommand]))
212+
reply = self.port.readData(length=4)
214213
milliampMinutes = (reply[0] << 24) | (reply[1] << 16) | (reply[2] << 8) | reply[3]
215214
return milliampMinutes / 60000.0
216215

217216
def doResetAccumulatedCharge(self):
218-
self._sendCommandByte(self.resetChargeCommand)
217+
self.port.writeData(bytearray([self.resetChargeCommand]))
219218

220219

221220
class DebugPwrUSBPort(DebugPort):

0 commit comments

Comments
 (0)