Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add set_limit and helpers for enabling and disabling #16

Merged
merged 3 commits into from
Mar 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions pyjuicenet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Charger:
def __init__(self, json_settings, api):
"""Create a Charger."""
self.json_settings = json_settings
self.json_info = {}
self.json_state = {}
self.api = api
self.last_updated_at = 0
Expand Down Expand Up @@ -45,6 +46,13 @@ async def update_state(self, force=False) -> bool:
self.json_state = json_state
return json_state["success"]

async def update_info(self) -> bool:
"""Update device info with latest info from API."""
self.last_updated_at = time.time()
json_info = await self.api.get_info(self)
self.json_info = json_info
return json_info["success"]

@property
def voltage(self) -> int:
"""Get the voltage."""
Expand Down Expand Up @@ -85,6 +93,40 @@ def override_time(self) -> int:
"""Get the override time."""
return self.json_state.get("override_time")

@property
def max_charging_amperage(self) -> int:
"""Get the maximum charging limit time from the smaller of the wire rating and unit rating.
This can be used along with set limit to enable the charger at full capacity"""
return min(self.json_info.get("amps_wire_rating"), self.json_info.get("amps_unit_rating"))

@property
def current_charging_amperage_limit(self) -> int:
"""Get the current amperage charging limit for the charger."""
return self.json_state.get("charging", {}).get("amps_limit")

@property
def charger_enabled(self) -> bool:
"""Is the charger allowing amps to flow."""
return self.current_charging_amperage_limit > 0

async def set_charging_amperage_limit(self, amperage: int) -> bool:
"""Set the amperage limit of the charger. 0 will disable the charger."""
response = await self.api.set_limit(self, amperage)

# Update state so that the amperage limit is correctly reflected
if response['success']:
await self.update_state(True)

return response['success']

async def enable_charger(self):
"""Enable charger for max support amperage"""
return await self.set_charging_amperage_limit(self.max_charging_amperage)

async def disable_charger(self):
"""Disable all charging activity"""
return await self.set_charging_amperage_limit(0)

async def set_override(self, charge_now) -> bool:
"""Set to override schedule or not."""
override_time = 0
Expand Down Expand Up @@ -164,6 +206,7 @@ async def get_devices(self):
for unit in units_json:
device = Charger(unit, self)
await device.update_state()
await device.update_info()
devices.append(device)

return devices
Expand Down Expand Up @@ -196,6 +239,21 @@ async def get_info(self, charger: Charger):
)
return await response.json()

async def set_limit(self, charger: Charger, amperage_limit: int):
"""Set the amperage limit of the charger. 0 will disable the charger."""
data = {
"cmd": "set_limit",
"device_id": self.uuid,
"token": charger.token,
"amperage": amperage_limit,
"account_token": self.api_token
}

response = await self.session.post(
f"{BASE_URL}/box_api_secure", json=data,
)
return await response.json()

async def set_override(
self,
charger: Charger,
Expand Down