Converted shopping list to use json util and added default override for json util (#12478)
* Converted shopping list to use json util, Added default override for json util * Reverted accidental revert * Fixed pylint issue
This commit is contained in:
parent
635d36c6ba
commit
60148f3e83
2 changed files with 8 additions and 12 deletions
|
@ -1,8 +1,6 @@
|
||||||
"""Component to manage a shopping list."""
|
"""Component to manage a shopping list."""
|
||||||
import asyncio
|
import asyncio
|
||||||
import json
|
|
||||||
import logging
|
import logging
|
||||||
import os
|
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
@ -14,7 +12,7 @@ from homeassistant.components.http.data_validator import (
|
||||||
RequestDataValidator)
|
RequestDataValidator)
|
||||||
from homeassistant.helpers import intent
|
from homeassistant.helpers import intent
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
from homeassistant.util.json import load_json, save_json
|
||||||
|
|
||||||
DOMAIN = 'shopping_list'
|
DOMAIN = 'shopping_list'
|
||||||
DEPENDENCIES = ['http']
|
DEPENDENCIES = ['http']
|
||||||
|
@ -101,18 +99,13 @@ class ShoppingData:
|
||||||
"""Load items."""
|
"""Load items."""
|
||||||
def load():
|
def load():
|
||||||
"""Load the items synchronously."""
|
"""Load the items synchronously."""
|
||||||
path = self.hass.config.path(PERSISTENCE)
|
return load_json(self.hass.config.path(PERSISTENCE), default=[])
|
||||||
if not os.path.isfile(path):
|
|
||||||
return []
|
|
||||||
with open(path) as file:
|
|
||||||
return json.loads(file.read())
|
|
||||||
|
|
||||||
self.items = yield from self.hass.async_add_job(load)
|
self.items = yield from self.hass.async_add_job(load)
|
||||||
|
|
||||||
def save(self):
|
def save(self):
|
||||||
"""Save the items."""
|
"""Save the items."""
|
||||||
with open(self.hass.config.path(PERSISTENCE), 'wt') as file:
|
save_json(self.hass.config.path(PERSISTENCE), self.items)
|
||||||
file.write(json.dumps(self.items, sort_keys=True, indent=4))
|
|
||||||
|
|
||||||
|
|
||||||
class AddItemIntent(intent.IntentHandler):
|
class AddItemIntent(intent.IntentHandler):
|
||||||
|
|
|
@ -8,8 +8,11 @@ from homeassistant.exceptions import HomeAssistantError
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
_UNDEFINED = object()
|
||||||
|
|
||||||
def load_json(filename: str) -> Union[List, Dict]:
|
|
||||||
|
def load_json(filename: str, default: Union[List, Dict] = _UNDEFINED) \
|
||||||
|
-> Union[List, Dict]:
|
||||||
"""Load JSON data from a file and return as dict or list.
|
"""Load JSON data from a file and return as dict or list.
|
||||||
|
|
||||||
Defaults to returning empty dict if file is not found.
|
Defaults to returning empty dict if file is not found.
|
||||||
|
@ -26,7 +29,7 @@ def load_json(filename: str) -> Union[List, Dict]:
|
||||||
except OSError as error:
|
except OSError as error:
|
||||||
_LOGGER.exception('JSON file reading failed: %s', filename)
|
_LOGGER.exception('JSON file reading failed: %s', filename)
|
||||||
raise HomeAssistantError(error)
|
raise HomeAssistantError(error)
|
||||||
return {} # (also evaluates to False)
|
return {} if default is _UNDEFINED else default
|
||||||
|
|
||||||
|
|
||||||
def save_json(filename: str, config: Union[List, Dict]):
|
def save_json(filename: str, config: Union[List, Dict]):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue