Skip to content

Commit

Permalink
adjust currents based on @FalconFour comments on JuiceRescue#39
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanfmartinez committed Jun 2, 2024
1 parent 5ad8db2 commit 9886c3a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
7 changes: 5 additions & 2 deletions juicebox_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,13 @@ def build_payload(self) -> None:
# Instant amperage may need to be represented using 4 digits (e.g. 0040) on newer Juicebox versions.
# mine wich send data using version 09u works with 4 digits on offline and 3 digit on instant
# sizes got from original packet dump when communicating with enel x server
#
# https://github.com/snicker/juicepassproxy/issues/39#issuecomment-2002312548
# @FalconFour definition of currents
if self.new_version:
self.payload_str = f"CMD{weekday}{self.time.strftime('%H%M')}A{self.offline_amperage:04d}M{self.instant_amperage:03d}C{self.command:03d}S{self.counter:03d}"
self.payload_str = f"CMD{weekday}{self.time.strftime('%H%M')}A{self.instant_amperage:04d}M{self.offline_amperage:03d}C{self.command:03d}S{self.counter:03d}"
else:
self.payload_str = f"CMD{weekday}{self.time.strftime('%H%M')}A{self.offline_amperage:02d}M{self.instant_amperage:02d}C{self.command:03d}S{self.counter:03d}"
self.payload_str = f"CMD{weekday}{self.time.strftime('%H%M')}A{self.instant_amperage:02d}M{self.offline_amperage:02d}C{self.command:03d}S{self.counter:03d}"
self.checksum_str = self.checksum_computed()

def parse_values(self):
Expand Down
12 changes: 8 additions & 4 deletions juicebox_mqtthandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ async def start(self):
if self._kwargs.get("initial_state", None) is not None:
await self.set(self._kwargs.get("initial_state", None))
elif self.entity_type == 'number':
# The state will came on juicebox messages
_LOGGER.warning(f"{self.name} has no initial_state")
else:
await self.set(self.name)
Expand All @@ -161,7 +162,7 @@ async def _callback_async(self, client: Client, user_data, message: MQTTMessage)
_LOGGER.debug(f"Sending to MITM: {state}")
await self._mitm_handler.send_data_to_juicebox(state.encode("utf-8"))
else:
# Internal state must be set before seding message to juicebox
# Internal state must be set before sending message to juicebox
await self.set(state)
await self._mitm_handler.send_cmd_message_to_juicebox(new_values=True)
else:
Expand Down Expand Up @@ -266,19 +267,22 @@ def __init__(
device_class="current",
unit_of_measurement="A",
),
# Maximum supported by device
"current_rating": JuiceboxMQTTSensor(
name="Current Rating",
state_class="measurement",
device_class="current",
unit_of_measurement="A",
),
# Offline max
"current_max": JuiceboxMQTTNumber(
name="Max Current",
device_class="current",
unit_of_measurement="A",
min=0,
max=self._max_current,
),
# Instant / Charging current
"current_max_charging": JuiceboxMQTTNumber(
name="Max Charging Current",
device_class="current",
Expand Down Expand Up @@ -424,10 +428,10 @@ async def _basic_message_parse(self, data: bytes):
)
elif part[0] == "m":
message["current_rating"] = float(part.split("m")[1])
elif part[0] == "M":
message["current_max"] = float(part.split("M")[1])
elif part[0] == "C":
message["current_max_charging"] = float(part.split("C")[1])
message["current_max"] = float(part.split("C")[1])
elif part[0] == "M":
message["current_max_charging"] = float(part.split("M")[1])
elif part[0] == "f":
message["frequency"] = round(float(part.split("f")[1]) * 0.01, 2)
elif part[0] == "L":
Expand Down
8 changes: 4 additions & 4 deletions test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ class TestMessage(unittest.TestCase):
def test_message_building(self):
m = JuiceboxCommand()
m.time = datetime.datetime(2012, 3, 23, 23, 24, 55, 173504)
m.offline_amperage = 20
m.instant_amperage = 16
m.offline_amperage = 16
m.instant_amperage = 20
print(m.build())
print(m.inspect())
self.assertEqual(m.build(), "CMD52324A20M16C006S001!5RE$")
Expand All @@ -17,8 +17,8 @@ def test_message_building(self):
def test_message_building_new(self):
m = JuiceboxCommand(new_version=True)
m.time = datetime.datetime(2012, 3, 23, 23, 24, 55, 173504)
m.offline_amperage = 20
m.instant_amperage = 16
m.offline_amperage = 16
m.instant_amperage = 20
print(m.build())
print(m.inspect())
self.assertEqual(m.build(), "CMD52324A0020M016C006S001!YUK$")
Expand Down

0 comments on commit 9886c3a

Please sign in to comment.