Skip to content
This repository has been archived by the owner on Apr 23, 2024. It is now read-only.

Commit

Permalink
Merge branch 'release/0.29.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
SebRut committed Mar 3, 2021
2 parents 2118661 + 221d2da commit ed2e839
Show file tree
Hide file tree
Showing 19 changed files with 580 additions and 32 deletions.
20 changes: 19 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
# Changelog

## [v0.29.0](https://github.com/SebRut/pygrocy/tree/v0.29.0) (2021-03-03)

[Full Changelog](https://github.com/SebRut/pygrocy/compare/v0.28.0...v0.29.0)

**Implemented enhancements:**

- Missing generic put and get option [\#155](https://github.com/SebRut/pygrocy/issues/155)
- add "get all products" method [\#97](https://github.com/SebRut/pygrocy/issues/97)

**Closed issues:**

- 404 errors and unresponsive sensors after configuring integration in HA [\#154](https://github.com/SebRut/pygrocy/issues/154)

**Merged pull requests:**

- add all\_products method [\#157](https://github.com/SebRut/pygrocy/pull/157) ([SebRut](https://github.com/SebRut))
- add support for generic objcts [\#156](https://github.com/SebRut/pygrocy/pull/156) ([SebRut](https://github.com/SebRut))

## [v0.28.0](https://github.com/SebRut/pygrocy/tree/v0.28.0) (2021-02-20)

[Full Changelog](https://github.com/SebRut/pygrocy/compare/v0.27.0...v0.28.0)
Expand Down Expand Up @@ -196,6 +214,7 @@
- Update coveralls requirement from ~=2.0.0 to ~=2.1.1 [\#104](https://github.com/SebRut/pygrocy/pull/104) ([dependabot-preview[bot]](https://github.com/apps/dependabot-preview))
- Update pdoc3 requirement from ~=0.8.1 to ~=0.8.3 [\#102](https://github.com/SebRut/pygrocy/pull/102) ([dependabot-preview[bot]](https://github.com/apps/dependabot-preview))
- Update responses requirement from ~=0.10.14 to ~=0.10.15 [\#101](https://github.com/SebRut/pygrocy/pull/101) ([dependabot-preview[bot]](https://github.com/apps/dependabot-preview))
- add all fields from chore details api call [\#98](https://github.com/SebRut/pygrocy/pull/98) ([SebRut](https://github.com/SebRut))

## [v0.15.0](https://github.com/SebRut/pygrocy/tree/v0.15.0) (2020-05-25)

Expand All @@ -219,7 +238,6 @@

- upgrade used grocy version [\#100](https://github.com/SebRut/pygrocy/pull/100) ([SebRut](https://github.com/SebRut))
- Feature/91 task api [\#99](https://github.com/SebRut/pygrocy/pull/99) ([SebRut](https://github.com/SebRut))
- add all fields from chore details api call [\#98](https://github.com/SebRut/pygrocy/pull/98) ([SebRut](https://github.com/SebRut))
- finish abstraction from api [\#96](https://github.com/SebRut/pygrocy/pull/96) ([SebRut](https://github.com/SebRut))
- fix travis build [\#93](https://github.com/SebRut/pygrocy/pull/93) ([SebRut](https://github.com/SebRut))
- Update responses requirement from ~=0.10.12 to ~=0.10.14 [\#89](https://github.com/SebRut/pygrocy/pull/89) ([dependabot-preview[bot]](https://github.com/apps/dependabot-preview))
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
[![Development Build Status](https://api.travis-ci.com/SebRut/pygrocy.svg?branch=develop)](https://travis-ci.com/SebRut/pygrocy)
[![PyPI](https://img.shields.io/pypi/v/pygrocy.svg)](https://pypi.org/project/pygrocy/)
![Python Version](https://img.shields.io/badge/python-3.6%20%7C%203.8%20%7C%203.9-blue)
![Grocy Version](https://img.shields.io/badge/grocy-3.0.1-yellow)
[![Coverage Status](https://coveralls.io/repos/github/SebRut/pygrocy/badge.svg?branch=master)](https://coveralls.io/github/SebRut/pygrocy?branch=master)
[![CodeFactor](https://www.codefactor.io/repository/github/sebrut/pygrocy/badge)](https://www.codefactor.io/repository/github/sebrut/pygrocy)
[![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white)](https://github.com/pre-commit/pre-commit)
Expand Down
1 change: 1 addition & 0 deletions pygrocy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
The pygrocy module
"""
from .data_models.generic import EntityType # noqa: F401
from .grocy import Grocy # noqa: F401
from .grocy_api_client import TransactionType # noqa: F401

Expand Down
25 changes: 25 additions & 0 deletions pygrocy/data_models/generic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from enum import Enum


class EntityType(str, Enum):
PRODUCTS = "products"
CHORES = "chores"
PRODUCT_BARCODES = "product_barcodes"
BATTERIES = "batteries"
LOCATIONS = "locations"
QUANTITY_UNITS = "quantity_units"
QUANTITY_UNIT_CONVERSIONS = "quantity_unit_conversions"
SHOPPING_LIST = "shopping_list"
SHOPPING_LISTS = "shopping_lists"
SHOPPING_LOCATIONS = "shopping_locations"
RECIPES = "recipes"
RECIPES_POS = "recipes_pos"
RECIPES_NESTINGS = "recipes_nestings"
TASKS = "tasks"
TASK_CATEGORIES = "task_categories"
PRODUCT_GROUPS = "product_groups"
EQUIPMENT = "equipment"
USER_FIELDS = "userfields"
USER_ENTITIES = "userentities"
USER_OBJECTS = "userobjects"
MEAL_PLAN = "meal_plan"
32 changes: 20 additions & 12 deletions pygrocy/data_models/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,23 @@
LocationData,
MissingProductResponse,
ProductBarcode,
ProductData,
ProductDetailsResponse,
ShoppingListItem,
)


class Product(DataModel):
def __init__(self, response):
def __init__(self, data):
self._init_empty()
if isinstance(response, CurrentStockResponse):
self._init_from_CurrentStockResponse(response)
elif isinstance(response, MissingProductResponse):
self._init_from_MissingProductResponse(response)
elif isinstance(response, ProductDetailsResponse):
self._init_from_ProductDetailsResponse(response)
if isinstance(data, CurrentStockResponse):
self._init_from_CurrentStockResponse(data)
elif isinstance(data, MissingProductResponse):
self._init_from_MissingProductResponse(data)
elif isinstance(data, ProductDetailsResponse):
self._init_from_ProductDetailsResponse(data)
elif isinstance(data, ProductData):
self._init_from_ProductData(data)

def _init_empty(self):
self._name = None
Expand All @@ -39,9 +42,9 @@ def _init_from_CurrentStockResponse(self, response: CurrentStockResponse):
self._id = response.product_id
self._available_amount = response.amount
self._best_before_date = response.best_before_date

if response.product:
self._name = response.product.name
self._product_group_id = response.product.product_group_id
self._init_from_ProductData(response.product)

def _init_from_MissingProductResponse(self, response: MissingProductResponse):
self._id = response.product_id
Expand All @@ -50,13 +53,18 @@ def _init_from_MissingProductResponse(self, response: MissingProductResponse):
self._is_partly_in_stock = response.is_partly_in_stock

def _init_from_ProductDetailsResponse(self, response: ProductDetailsResponse):
self._id = response.product.id
self._product_group_id = response.product.product_group_id
self._available_amount = response.stock_amount
self._best_before_date = response.next_best_before_date
self._name = response.product.name
self._barcodes = response.barcodes

if response.product:
self._init_from_ProductData(response.product)

def _init_from_ProductData(self, product: ProductData):
self._id = product.id
self._product_group_id = product.product_group_id
self._name = product.name

def get_details(self, api_client: GrocyApiClient):
details = api_client.get_product(self.id)
if details:
Expand Down
25 changes: 22 additions & 3 deletions pygrocy/grocy.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from .base import DataModel # noqa: F401
from .data_models.battery import Battery
from .data_models.chore import Chore
from .data_models.generic import EntityType
from .data_models.meal_items import MealPlanItem, RecipeItem
from .data_models.product import Group, Product, ShoppingListProduct
from .data_models.task import Task
Expand Down Expand Up @@ -81,6 +82,13 @@ def product(self, product_id: int) -> Product:
if resp:
return Product(resp)

def all_products(self) -> List[Product]:
raw_products = self.get_generic_objects_for_type(EntityType.PRODUCTS)
from pygrocy.grocy_api_client import ProductData

product_datas = [ProductData(product) for product in raw_products]
return [Product(product) for product in product_datas]

def chores(self, get_details: bool = False) -> List[Chore]:
raw_chores = self._api_client.get_chores()
chores = [Chore(chore) for chore in raw_chores]
Expand Down Expand Up @@ -192,9 +200,6 @@ def recipe(self, recipe_id: int) -> RecipeItem:
if recipe:
return RecipeItem(recipe)

def add_generic(self, entity_type, data):
return self._api_client.add_generic(entity_type, data)

def batteries(self) -> List[Battery]:
raw_batteries = self._api_client.get_batteries()
return [Battery(bat) for bat in raw_batteries]
Expand All @@ -206,3 +211,17 @@ def battery(self, battery_id: int) -> Battery:

def charge_battery(self, battery_id: int, tracked_time: datetime = datetime.now()):
return self._api_client.charge_battery(battery_id, tracked_time)

def add_generic(self, entity_type: EntityType, data):
return self._api_client.add_generic(entity_type.value, data)

def update_generic(self, entity_type: EntityType, object_id: int, updated_data):
return self._api_client.update_generic(
entity_type.value, object_id, updated_data
)

def delete_generic(self, entity_type: EntityType, object_id: int):
return self._api_client.delete_generic(entity_type, object_id)

def get_generic_objects_for_type(self, entity_type: EntityType):
return self._api_client.get_generic_objects_for_type(entity_type.value)
24 changes: 21 additions & 3 deletions pygrocy/grocy_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,15 @@ def _do_put_request(self, end_url: str, data):
if len(resp.content) > 0:
return resp.json()

def _do_delete_request(self, end_url: str):
req_url = urljoin(self._base_url, end_url)
resp = requests.get(req_url, verify=self._verify_ssl, headers=self._headers)
if resp.status_code >= 400:
raise GrocyError(resp)

if len(resp.content) > 0:
return resp.json()

def get_stock(self) -> List[CurrentStockResponse]:
parsed_json = self._do_get_request("stock")
return [CurrentStockResponse(response) for response in parsed_json]
Expand Down Expand Up @@ -742,9 +751,6 @@ def get_recipe(self, object_id: int) -> RecipeDetailsResponse:
if parsed_json:
return RecipeDetailsResponse(parsed_json)

def add_generic(self, entity_type: str, data: object):
self._do_post_request(f"objects/{entity_type}", data)

def get_batteries(self) -> List[CurrentBatteryResponse]:
parsed_json = self._do_get_request(f"batteries")
if parsed_json:
Expand All @@ -760,3 +766,15 @@ def charge_battery(self, battery_id: int, tracked_time: datetime = datetime.now(
data = {"tracked_time": localized_tracked_time.isoformat()}

return self._do_post_request(f"batteries/{battery_id}/charge", data)

def add_generic(self, entity_type: str, data):
return self._do_post_request(f"objects/{entity_type}", data)

def update_generic(self, entity_type: str, object_id: int, data):
return self._do_put_request(f"objects/{entity_type}/{object_id}", data)

def delete_generic(self, entity_type: str, object_id: int):
return self._do_delete_request(f"objects/{entity_type}/{object_id}")

def get_generic_objects_for_type(self, entity_type: str):
return self._do_get_request(f"objects/{entity_type}")
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="pygrocy",
version="0.28.0",
version="0.29.0",
author="Sebastian Rutofski",
author_email="[email protected]",
description="",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
interactions:
- request:
body: null
headers:
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
User-Agent:
- python-requests/2.25.1
accept:
- application/json
method: GET
uri: https://localhost/api/objects/tasks/30000
response:
body:
string: !!binary |
H4sIAAAAAAAEA6tWSi0qyi+Kz00tLk5MT1WyUvJPykpNLlHIyy9RSMsvzUtRqgUAcMoJfiQAAAA=
headers:
Access-Control-Allow-Headers:
- '*'
Access-Control-Allow-Methods:
- GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Origin:
- '*'
Connection:
- keep-alive
Content-Encoding:
- gzip
Content-Type:
- application/json
Date:
- Wed, 03 Mar 2021 13:12:37 GMT
Server:
- nginx/1.18.0
Transfer-Encoding:
- chunked
X-Powered-By:
- PHP/7.4.14
status:
code: 404
message: Not Found
version: 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
interactions:
- request:
body: null
headers:
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
User-Agent:
- python-requests/2.25.1
accept:
- application/json
method: GET
uri: https://localhost/api/objects/tasks/3
response:
body:
string: !!binary |
H4sIAAAAAAAEA0WNSwoCMRBEryJZO5CPCOYc7kNI2iGYz5DOICLe3W5HmV11Vb2ul0hRWGHEUVRf
gOTV413RGQFDT8tIrQpb15zJWsFFP7ilpVaT1JPSXG2VPfmTbqQCOHxZ/mAgaG796b5jjHjENFeI
bjS3IvQt4d3eHi50IILC/dG+aA7qYk/KmjO1mb0lyBG3rfcHvUz3sNEAAAA=
headers:
Access-Control-Allow-Headers:
- '*'
Access-Control-Allow-Methods:
- GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Origin:
- '*'
Connection:
- keep-alive
Content-Encoding:
- gzip
Content-Type:
- application/json
Date:
- Wed, 03 Mar 2021 13:12:37 GMT
Server:
- nginx/1.18.0
Transfer-Encoding:
- chunked
X-Powered-By:
- PHP/7.4.14
status:
code: 200
message: OK
version: 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
interactions:
- request:
body: '{"eman": "Testbattery"}'
headers:
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
Content-Length:
- '23'
Content-Type:
- application/json
User-Agent:
- python-requests/2.25.1
accept:
- application/json
method: POST
uri: https://localhost/api/objects/batteries
response:
body:
string: '{"error_message":"SQLSTATE[HY000]: General error: 1 table batteries
has no column named eman"}'
headers:
Access-Control-Allow-Headers:
- '*'
Access-Control-Allow-Methods:
- GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Origin:
- '*'
Connection:
- keep-alive
Content-Type:
- application/json
Date:
- Wed, 03 Mar 2021 13:07:14 GMT
Server:
- nginx/1.18.0
Transfer-Encoding:
- chunked
X-Powered-By:
- PHP/7.4.14
status:
code: 400
message: Bad Request
version: 1
Loading

0 comments on commit ed2e839

Please sign in to comment.