diff --git a/README.md b/README.md index e8e925e..8eeabf9 100644 --- a/README.md +++ b/README.md @@ -148,13 +148,14 @@ 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: ``` @@ -162,7 +163,8 @@ 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 ``` diff --git a/pymyenergi/cli.py b/pymyenergi/cli.py index 7bdf3a7..9782a46 100644 --- a/pymyenergi/cli.py +++ b/pymyenergi/cli.py @@ -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") @@ -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="*") diff --git a/pymyenergi/libbi.py b/pymyenergi/libbi.py index 0131464..7fcf7cd 100644 --- a/pymyenergi/libbi.py +++ b/pymyenergi/libbi.py @@ -16,6 +16,7 @@ 6: "Discharging", 7: "Duration Charging", 8: "Duration Drain", + 12: "Target Charge", 51: "Boosting", 53: "Boosting", 55: "Boosting", @@ -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.""" @@ -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): @@ -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" @@ -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, @@ -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 = "" @@ -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"