42 lines
1.3 KiB
Python
42 lines
1.3 KiB
Python
|
import logging
|
||
|
from grocy.conf import Configuration
|
||
|
from grocy.request import Request
|
||
|
|
||
|
|
||
|
class Battery(object):
|
||
|
TRACK_CHARGE = '{domain}/api/batteries/{batteryId}/charge'
|
||
|
GET_BATTERY = '{domain}/api/batteries/{batteryId}'
|
||
|
|
||
|
def __init__(self, id=None):
|
||
|
self.conf = Configuration()
|
||
|
self.conf.load()
|
||
|
self.id = id
|
||
|
|
||
|
@property
|
||
|
def details(self):
|
||
|
logger = logging.getLogger('battery.details')
|
||
|
try:
|
||
|
if self.id is None:
|
||
|
raise Exception('battery id is required')
|
||
|
url = self.GET_BATTERY.format(domain=self.conf.domain, batteryId=self.id)
|
||
|
request = Request('get', url)
|
||
|
battery_details = request.send()
|
||
|
# Remove general battery info
|
||
|
del battery_details['battery']
|
||
|
return battery_details
|
||
|
except Exception as e:
|
||
|
logger.error(e)
|
||
|
raise e
|
||
|
|
||
|
def track_charge(self):
|
||
|
logger = logging.getLogger('battery.track_charge')
|
||
|
try:
|
||
|
if self.id is None:
|
||
|
raise Exception('battery id is required')
|
||
|
url = self.TRACK_CHARGE.format(domain=self.conf.domain, batteryId=self.id)
|
||
|
request = Request('post', url)
|
||
|
batteries = request.send()
|
||
|
except Exception as e:
|
||
|
logger.error(e)
|
||
|
raise e
|