feat: Added create, remove, list, edit subcommads for battery
- fix: Used correct `str.join` method to join list into string
This commit is contained in:
parent
57900844f1
commit
c2b21e80d3
41
grocy/battery.py
Normal file
41
grocy/battery.py
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
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
|
159
grocy/cli.py
159
grocy/cli.py
@ -10,6 +10,7 @@ from grocy.recipe import Recipe
|
|||||||
from grocy.table import Table
|
from grocy.table import Table
|
||||||
from grocy.entity import Entity
|
from grocy.entity import Entity
|
||||||
from grocy.chore import Chore
|
from grocy.chore import Chore
|
||||||
|
from grocy.battery import Battery
|
||||||
from grocy.stock import Stock
|
from grocy.stock import Stock
|
||||||
from grocy.schema import get_schema
|
from grocy.schema import get_schema
|
||||||
from grocy.shoppinglist import ShoppingList
|
from grocy.shoppinglist import ShoppingList
|
||||||
@ -744,11 +745,11 @@ def list(name, template):
|
|||||||
else:
|
else:
|
||||||
chores = entity.get()
|
chores = entity.get()
|
||||||
|
|
||||||
data = {'chores': []}
|
data = {'fields': []}
|
||||||
for chore in chores:
|
for chore in chores:
|
||||||
chore_details = Chore(chore['id'])
|
chore_details = Chore(chore['id'])
|
||||||
chore.update(chore_details.execution_times)
|
chore.update(chore_details.execution_times)
|
||||||
data['chores'].append({'fields': chore})
|
data['fields'].append(chore)
|
||||||
|
|
||||||
if template:
|
if template:
|
||||||
loaded_template = cfg.templates(template)
|
loaded_template = cfg.templates(template)
|
||||||
@ -786,3 +787,157 @@ def create(template):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(e)
|
logger.error(e)
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
|
|
||||||
|
@main.group()
|
||||||
|
def battery():
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
@battery.command()
|
||||||
|
@click.option('-t', 'template')
|
||||||
|
@click.option('--name', '-n', 'name')
|
||||||
|
def list(name, template):
|
||||||
|
logger = logging.getLogger('cli.battery.list')
|
||||||
|
cfg = Configuration()
|
||||||
|
cfg.load()
|
||||||
|
query = {}
|
||||||
|
try:
|
||||||
|
entity = Entity(name='batteries')
|
||||||
|
if name:
|
||||||
|
batteries = entity.find_by_name(name)
|
||||||
|
if len(batteries) == 0:
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
batteries = entity.get()
|
||||||
|
|
||||||
|
for battery in batteries:
|
||||||
|
battery_details = Battery(id=battery['id']).details
|
||||||
|
battery.update(battery_details)
|
||||||
|
|
||||||
|
data = {'fields': {'batteries': batteries}}
|
||||||
|
if template:
|
||||||
|
loaded_template = cfg.templates(template)
|
||||||
|
else:
|
||||||
|
loaded_template = cfg.templates('battery/list')
|
||||||
|
|
||||||
|
click.echo(loaded_template.render(grocy=data))
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(e)
|
||||||
|
raise e
|
||||||
|
|
||||||
|
|
||||||
|
@battery.command()
|
||||||
|
@click.option('-t', 'template')
|
||||||
|
def create(template):
|
||||||
|
logger = logging.getLogger('cli.battery.create')
|
||||||
|
try:
|
||||||
|
cfg = Configuration()
|
||||||
|
cfg.load()
|
||||||
|
util = Util(cfg=cfg)
|
||||||
|
meta = Meta()
|
||||||
|
data = {'meta': meta.generate()}
|
||||||
|
if template:
|
||||||
|
loaded_template = cfg.templates(template)
|
||||||
|
else:
|
||||||
|
loaded_template = cfg.templates('battery/create')
|
||||||
|
created_battery = click.edit(loaded_template.render(grocy=data), extension='.yml')
|
||||||
|
if not created_battery:
|
||||||
|
return
|
||||||
|
parsed_new_battery = util.load_yaml(created_battery)[0]
|
||||||
|
|
||||||
|
if template == 'debug':
|
||||||
|
click.echo(parsed_new_battery)
|
||||||
|
return
|
||||||
|
entity = Entity(name='batteries')
|
||||||
|
entity.create(parsed_new_battery)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(e)
|
||||||
|
raise e
|
||||||
|
|
||||||
|
|
||||||
|
@battery.command()
|
||||||
|
@click.argument('battery_id')
|
||||||
|
@click.option('-t', 'template')
|
||||||
|
def remove(battery_id, template):
|
||||||
|
logger = logging.getLogger('cli.battery.remove')
|
||||||
|
try:
|
||||||
|
entity = Entity(name='batteries')
|
||||||
|
entity.delete(battery_id)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(e)
|
||||||
|
raise e
|
||||||
|
|
||||||
|
|
||||||
|
@battery.command()
|
||||||
|
@click.argument('battery_id', required=False)
|
||||||
|
@click.option('-t', 'template')
|
||||||
|
@click.option('--name', '-n', 'name')
|
||||||
|
def edit(battery_id, name, template):
|
||||||
|
logger = logging.getLogger('cli.battery.edit')
|
||||||
|
try:
|
||||||
|
cfg = Configuration()
|
||||||
|
util = Util(cfg=cfg)
|
||||||
|
cfg.load()
|
||||||
|
if template:
|
||||||
|
loaded_template = cfg.templates(template)
|
||||||
|
else:
|
||||||
|
loaded_template = cfg.templates('battery/edit')
|
||||||
|
|
||||||
|
entity = Entity(name='batteries')
|
||||||
|
if battery_id:
|
||||||
|
battery = entity.get(id=battery_id)
|
||||||
|
batteries = [battery]
|
||||||
|
elif name:
|
||||||
|
# Convert name args to a single string
|
||||||
|
string_name_arg = ' '.join(name) if type(name) == list else name
|
||||||
|
batteries = entity.find({'name': string_name_arg})
|
||||||
|
else:
|
||||||
|
raise click.BadParameter('Missing BATTERY_ID or QUERY')
|
||||||
|
|
||||||
|
if batteries is None:
|
||||||
|
click.echo('Could not find batteries')
|
||||||
|
return
|
||||||
|
|
||||||
|
data = {'fields': {'batteries': batteries}}
|
||||||
|
edited_batteries = click.edit(loaded_template.render(grocy=data), extension='.yml')
|
||||||
|
if edited_batteries is None:
|
||||||
|
return
|
||||||
|
|
||||||
|
schema = entity.schema
|
||||||
|
|
||||||
|
parsed_edited_batteries = util.load_yaml(edited_batteries)
|
||||||
|
for index, edited_battery in enumerate(parsed_edited_batteries):
|
||||||
|
edited_battery['id'] = batteries[index]['id']
|
||||||
|
util.verify_integrity(edited_battery, schema)
|
||||||
|
entity.update(edited_battery, id=batteries[index]['id'])
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(e)
|
||||||
|
raise e
|
||||||
|
|
||||||
|
|
||||||
|
@battery.command()
|
||||||
|
@click.option('-t', 'template')
|
||||||
|
@click.argument('battery_id')
|
||||||
|
def track(template, battery_id):
|
||||||
|
logger = logging.getLogger('cli.battery.track')
|
||||||
|
cfg = Configuration()
|
||||||
|
cfg.load()
|
||||||
|
try:
|
||||||
|
if template:
|
||||||
|
loaded_template = cfg.templates(template)
|
||||||
|
else:
|
||||||
|
loaded_template = cfg.templates('battery/track')
|
||||||
|
|
||||||
|
created_battery = click.edit(loaded_template.render(), extension='.yml')
|
||||||
|
parsed_created_battery = yaml.safe_load(created_battery)
|
||||||
|
if template == 'debug':
|
||||||
|
click.echo(parsed_created_battery)
|
||||||
|
return
|
||||||
|
|
||||||
|
battery = Battery(id=battery_id)
|
||||||
|
battery.track_charge()
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(e)
|
||||||
|
raise e
|
||||||
|
@ -31,9 +31,8 @@ class Util(object):
|
|||||||
for prop in new_data.keys():
|
for prop in new_data.keys():
|
||||||
if prop not in schema_keys:
|
if prop not in schema_keys:
|
||||||
keys_not_found.append(prop)
|
keys_not_found.append(prop)
|
||||||
print('{}'.format(keys_not_found))
|
|
||||||
if len(keys_not_found) > 0:
|
if len(keys_not_found) > 0:
|
||||||
raise Exception('{} is not valid property'.format(keys_not_found.join(', ')))
|
raise Exception('{} are/is not valid properties'.format(', '.join(keys_not_found)))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(e)
|
logger.error(e)
|
||||||
raise e
|
raise e
|
||||||
|
Loading…
Reference in New Issue
Block a user