Skip to content

Commit

Permalink
Merge pull request #4 from trizmark/add-libbi-charge-target-handling
Browse files Browse the repository at this point in the history
Add libbi charge target handling
  • Loading branch information
trizmark authored Oct 29, 2023
2 parents d3e369e + a4a5b8c commit 6e063c1
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,21 +148,23 @@ loop.run_until_complete(get_data())
```

## Libbi support
Very early and basic support of Libbi.
Currently supported features:

- Reads a few values such as State of Charge, DCPV CT
- Battery in and out energy
- Gets and sets the current operating mode (normal/stopped/export)
- Change priority of Libbi
- Enable/Disable charging from the grid
- Set charge target (in Wh)

cli examples:
```
myenergi libbi show
myenergi libbi mode normal
myenergi libbi priority 1
myenergi libbi energy
myenergi libbi chargefromgrid disable
myenergi libbi chargefromgrid false
myenergi libbi chargetarget 10200
```


Expand Down
19 changes: 15 additions & 4 deletions pymyenergi/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,15 @@ async def main(args):
if len(args.arg) < 1 or args.arg[0].capitalize() not in [
"True",
"False",
"Enable",
"Disable",
]:
sys.exit(f"A mode must be specifed, one of enable or disable")
sys.exit("A mode must be specifed, one of true or false")
await device.set_charge_from_grid(args.arg[0])
print(f"Charge from grid was set to {args.arg[0].capitalize()}")
elif args.action == "chargetarget" and args.command == LIBBI:
if len(args.arg) < 1 or not args.arg[0].isnumeric():
sys.exit("The charge target must be specified in Wh")
await device.set_charge_target(args.arg[0])
print(f"Charge target was set to {args.arg[0]}Wh")
elif args.action == "mingreen" and args.command == ZAPPI:
if len(args.arg) < 1:
sys.exit("A minimum green level must be provided")
Expand Down Expand Up @@ -236,7 +239,15 @@ def cli():
)
subparser_libbi.add_argument("-s", "--serial", dest="serial", default=None)
subparser_libbi.add_argument(
"action", choices=["show", "mode", "priority", "energy", "chargefromgrid"]
"action",
choices=[
"show",
"mode",
"priority",
"energy",
"chargefromgrid",
"chargetarget",
],
)
subparser_libbi.add_argument("arg", nargs="*")

Expand Down
30 changes: 24 additions & 6 deletions pymyenergi/libbi.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
6: "Discharging",
7: "Duration Charging",
8: "Duration Drain",
12: "Target Charge",
51: "Boosting",
53: "Boosting",
55: "Boosting",
Expand All @@ -38,6 +39,7 @@
}
"""The myenergi app defines other modes as well (capture, charge, match), but these cannot be set"""


class Libbi(BaseDevice):
"""Libbi Client for myenergi API."""

Expand All @@ -54,6 +56,11 @@ async def refresh_extra(self):
self._extra_data["charge_from_grid"] = chargeFromGrid["content"][
str(self.serial_number)
]
chargeTarget = await self._connection.get(
"/api/AccountAccess/" + str(self.serial_number) + "/LibbiChargeSetup",
oauth=True,
)
self._extra_data["charge_target"] = chargeTarget["content"]["energyTarget"]

@property
def kind(self):
Expand Down Expand Up @@ -193,6 +200,11 @@ def charge_from_grid(self):
"""Is charging from the grid enabled?"""
return self._extra_data.get("charge_from_grid")

@property
def charge_target(self):
"""Libbi charge target"""
return self._extra_data.get("charge_target", 0) / 1000

@property
def prefix(self):
return "L"
Expand All @@ -216,10 +228,6 @@ async def set_operating_mode(self, mode: str):

async def set_charge_from_grid(self, charge_from_grid: bool):
"""Set charge from grid"""
if charge_from_grid.capitalize() in ["Enable", "Disable"]:
charge_from_grid = (
"True" if charge_from_grid.capitalize() == "Enable" else "False"
)
await self._connection.put(
f"/api/AccountAccess/LibbiMode?chargeFromGrid={charge_from_grid}&serialNo={self._serialno}",
oauth=True,
Expand All @@ -235,6 +243,15 @@ async def set_priority(self, priority):
self._data["pri"] = int(priority)
return True

async def set_charge_target(self, charge_target: float):
"""Set charge target"""
await self._connection.put(
f"/api/AccountAccess/{self._serialno}/TargetEnergy?targetEnergy={charge_target}",
oauth=True,
)
self._extra_data["charge_target"] = charge_target
return True

def show(self, short_format=False):
"""Returns a string with all data in human readable format"""
ret = ""
Expand All @@ -252,13 +269,14 @@ def show(self, short_format=False):
ret = ret + f"State of Charge: {self.state_of_charge}%\n"
ret = ret + f"Generating: {self.power_generated}W\n"
ret = ret + f"Grid: {self.power_grid}W\n"
ret = ret + f"Status : {self.status}\n"
ret = ret + "Local Mode : " + self.get_mode_description(self.local_mode) + "\n"
ret = ret + f"Status: {self.status}\n"
ret = ret + "Local Mode: " + self.get_mode_description(self.local_mode) + "\n"
ret = ret + "Charge from Grid: "
if self.charge_from_grid:
ret = ret + "Enabled\n"
else:
ret = ret + "Disabled\n"
ret = ret + f"Charge target: {self.charge_target}kWh\n"
ret = ret + f"CT 1 {self.ct1.name} {self.ct1.power}W phase {self.ct1.phase}\n"
ret = ret + f"CT 2 {self.ct2.name} {self.ct2.power}W phase {self.ct2.phase}\n"
ret = ret + f"CT 3 {self.ct3.name} {self.ct3.power}W phase {self.ct3.phase}\n"
Expand Down

0 comments on commit 6e063c1

Please sign in to comment.