mirror of
https://github.com/home-assistant/core.git
synced 2025-11-14 13:30:43 +00:00
Restore states through a JSON store instead of recorder (#17270)
* Restore states through a JSON store * Accept entity_id directly in restore state helper * Keep states stored between runs for a limited time * Remove warning
This commit is contained in:
committed by
Paulus Schoutsen
parent
a039c3209b
commit
5c3a4e3d10
@@ -1,6 +1,6 @@
|
||||
"""JSON utility functions."""
|
||||
import logging
|
||||
from typing import Union, List, Dict
|
||||
from typing import Union, List, Dict, Optional
|
||||
|
||||
import json
|
||||
import os
|
||||
@@ -41,7 +41,8 @@ def load_json(filename: str, default: Union[List, Dict, None] = None) \
|
||||
|
||||
|
||||
def save_json(filename: str, data: Union[List, Dict],
|
||||
private: bool = False) -> None:
|
||||
private: bool = False, *,
|
||||
encoder: Optional[json.JSONEncoder] = None) -> None:
|
||||
"""Save JSON data to a file.
|
||||
|
||||
Returns True on success.
|
||||
@@ -49,7 +50,7 @@ def save_json(filename: str, data: Union[List, Dict],
|
||||
tmp_filename = ""
|
||||
tmp_path = os.path.split(filename)[0]
|
||||
try:
|
||||
json_data = json.dumps(data, sort_keys=True, indent=4)
|
||||
json_data = json.dumps(data, sort_keys=True, indent=4, cls=encoder)
|
||||
# Modern versions of Python tempfile create this file with mode 0o600
|
||||
with tempfile.NamedTemporaryFile(mode="w", encoding='utf-8',
|
||||
dir=tmp_path, delete=False) as fdesc:
|
||||
|
||||
Reference in New Issue
Block a user