From fc6629a7518a03af1fe9f21917979a976baf3518 Mon Sep 17 00:00:00 2001 From: vondraussen Date: Mon, 3 Aug 2020 22:14:13 +0200 Subject: [PATCH 1/3] Add battery correction array --- inkbird/client.py | 18 +++++++++++++++++- inkbird/const.py | 12 ++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/inkbird/client.py b/inkbird/client.py index 48ce208..2927b92 100644 --- a/inkbird/client.py +++ b/inkbird/client.py @@ -57,11 +57,27 @@ def handleTemperature(self, data): for probe, t in enumerate(temp): self.probes[probe + 1].temperature = t + def __batteryPercentage(self, current, max): + factor = max / 6550.0 + length = len(const.BATTERY_CORRECTION) + + if (current > const.BATTERY_CORRECTION[length - 1] * factor): + return 100 + if (current <= const.BATTERY_CORRECTION[0] * factor): + return 0 + for idx, voltage in enumerate(const.BATTERY_CORRECTION, start=0): + if idx == length: + return 100 + if (current > (const.BATTERY_CORRECTION[idx] * factor)) and \ + (current <= (const.BATTERY_CORRECTION[idx+1] * factor)): + return idx + 1 + return 100 + def handleBattery(self, data): if data[0] != 36: return battery, maxBattery = struct.unpack(" Date: Mon, 3 Aug 2020 17:28:58 -0400 Subject: [PATCH 2/3] chore: Simplify battery percentage calculation --- inkbird/client.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/inkbird/client.py b/inkbird/client.py index 2927b92..f224f5f 100644 --- a/inkbird/client.py +++ b/inkbird/client.py @@ -59,17 +59,13 @@ def handleTemperature(self, data): def __batteryPercentage(self, current, max): factor = max / 6550.0 - length = len(const.BATTERY_CORRECTION) - - if (current > const.BATTERY_CORRECTION[length - 1] * factor): + current /= factor + if current > const.BATTERY_CORRECTION[-1]: return 100 - if (current <= const.BATTERY_CORRECTION[0] * factor): + if current <= const.BATTERY_CORRECTION[0]: return 0 for idx, voltage in enumerate(const.BATTERY_CORRECTION, start=0): - if idx == length: - return 100 - if (current > (const.BATTERY_CORRECTION[idx] * factor)) and \ - (current <= (const.BATTERY_CORRECTION[idx+1] * factor)): + if (current > voltage) and (current <= (const.BATTERY_CORRECTION[idx + 1])): return idx + 1 return 100 From dfde0c4db3922c5501fd57be312c335e2cb0dfce Mon Sep 17 00:00:00 2001 From: Jay Shridharani Date: Mon, 3 Aug 2020 17:29:30 -0400 Subject: [PATCH 3/3] chore: Black linting --- inkbird/const.py | 109 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 100 insertions(+), 9 deletions(-) diff --git a/inkbird/const.py b/inkbird/const.py index 458c8aa..428058e 100644 --- a/inkbird/const.py +++ b/inkbird/const.py @@ -29,13 +29,104 @@ REQ_BATTERY_MESSAGE = bytes([0x08, 0x24, 0x00, 0x00, 0x00, 0x00]) BATTERY_CORRECTION = [ - 5580, 5595, 5609, 5624, 5639, 5644, 5649, 5654, 5661, 5668, 5676, 5683, - 5698, 5712, 5727, 5733, 5739, 5744, 5750, 5756, 5759, 5762, 5765, 5768, - 5771, 5774, 5777, 5780, 5783, 5786, 5789, 5792, 5795, 5798, 5801, 5807, - 5813, 5818, 5824, 5830, 5830, 5830, 5835, 5840, 5845, 5851, 5857, 5864, - 5870, 5876, 5882, 5888, 5894, 5900, 5906, 5915, 5924, 5934, 5943, 5952, - 5961, 5970, 5980, 5989, 5998, 6007, 6016, 6026, 6035, 6044, 6052, 6062, - 6072, 6081, 6090, 6103, 6115, 6128, 6140, 6153, 6172, 6191, 6211, 6230, - 6249, 6265, 6280, 6296, 6312, 6328, 6344, 6360, 6370, 6381, 6391, 6407, - 6423, 6431, 6439, 6455 + 5580, + 5595, + 5609, + 5624, + 5639, + 5644, + 5649, + 5654, + 5661, + 5668, + 5676, + 5683, + 5698, + 5712, + 5727, + 5733, + 5739, + 5744, + 5750, + 5756, + 5759, + 5762, + 5765, + 5768, + 5771, + 5774, + 5777, + 5780, + 5783, + 5786, + 5789, + 5792, + 5795, + 5798, + 5801, + 5807, + 5813, + 5818, + 5824, + 5830, + 5830, + 5830, + 5835, + 5840, + 5845, + 5851, + 5857, + 5864, + 5870, + 5876, + 5882, + 5888, + 5894, + 5900, + 5906, + 5915, + 5924, + 5934, + 5943, + 5952, + 5961, + 5970, + 5980, + 5989, + 5998, + 6007, + 6016, + 6026, + 6035, + 6044, + 6052, + 6062, + 6072, + 6081, + 6090, + 6103, + 6115, + 6128, + 6140, + 6153, + 6172, + 6191, + 6211, + 6230, + 6249, + 6265, + 6280, + 6296, + 6312, + 6328, + 6344, + 6360, + 6370, + 6381, + 6391, + 6407, + 6423, + 6431, + 6439, + 6455, ]