mirror of
https://github.com/aljazceru/addons.git
synced 2025-12-17 21:24:20 +01:00
Add device register to assistant (#294)
* Update config.json * Update run.sh * Update hassio_gassistant.py * Update hassio_gassistant.py
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "Google Assistant",
|
||||
"version": "1.2",
|
||||
"version": "1.3",
|
||||
"slug": "google_assistant",
|
||||
"description": "A virtual personal assistant developed by Google",
|
||||
"url": "https://home-assistant.io/addons/google_assistant/",
|
||||
@@ -15,10 +15,12 @@
|
||||
"webui": "http://[HOST]:[PORT:9324]",
|
||||
"options": {
|
||||
"client_secrets": "google_assistant.json",
|
||||
"project_id": null,
|
||||
"model_id": null
|
||||
},
|
||||
"schema": {
|
||||
"client_secrets": "str",
|
||||
"project_id": "str",
|
||||
"model_id": "str"
|
||||
},
|
||||
"image": "homeassistant/{arch}-addon-google_assistant"
|
||||
|
||||
@@ -7,6 +7,9 @@ import google.oauth2.credentials
|
||||
|
||||
from google.assistant.library import Assistant
|
||||
from google.assistant.library.event import EventType
|
||||
from google.assistant.library.device_helpers import register_device
|
||||
|
||||
DEVICE_CONFIG = "/data/device.json"
|
||||
|
||||
|
||||
def process_event(event):
|
||||
@@ -21,12 +24,37 @@ def process_event(event):
|
||||
|
||||
if __name__ == '__main__':
|
||||
cred_json = Path(sys.argv[1])
|
||||
device_json = Path(DEVICE_CONFIG)
|
||||
|
||||
# open credentials
|
||||
with cred_json.open('r') as data:
|
||||
credentials = google.oauth2.credentials.Credentials(token=None, **json.load(data))
|
||||
|
||||
# Read device info
|
||||
if device_json.exists():
|
||||
with device_json.open('r') as data:
|
||||
device_info = json.load(data)
|
||||
|
||||
device_model_id = device_config['model_id']
|
||||
last_device_id = device_config.get('last_device_id', None)
|
||||
else:
|
||||
device_model_id = sys.argv[3]
|
||||
last_device_id = None
|
||||
|
||||
# run assistant
|
||||
with Assistant(credentials, sys.argv[2]) as assistant:
|
||||
with Assistant(credentials, device_model_id) as assistant:
|
||||
device_id = assistant.device_id
|
||||
print("device_model_id: {}".format(device_model_id))
|
||||
print("device_id: {}".format(device_id))
|
||||
|
||||
# Register device
|
||||
if last_device_id != device_id:
|
||||
register_device(sys.argv[2], credentials, device_model_id, device_id)
|
||||
with device_json.open('w') as dev_file:
|
||||
dev_file.write(json.dump({
|
||||
'last_device_id': device_id,
|
||||
'model_id': device_model_id,
|
||||
})
|
||||
|
||||
for event in assistant.start():
|
||||
process_event(event)
|
||||
|
||||
@@ -6,6 +6,7 @@ CLIENT_JSON=/data/client.json
|
||||
CRED_JSON=/data/cred.json
|
||||
|
||||
CLIENT_SECRETS=$(jq --raw-output '.client_secrets' $CONFIG_PATH)
|
||||
PROJECT_ID=$(jq --raw-output '.project_id' $CONFIG_PATH)
|
||||
MODEL_ID=$(jq --raw-output '.model_id' $CONFIG_PATH)
|
||||
|
||||
# check if a new assistant file exists
|
||||
@@ -22,4 +23,4 @@ elif [ ! -f "$CRED_JSON" ]; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
exec python3 /hassio_gassistant.py "$CRED_JSON" "$MODEL_ID" < /dev/null
|
||||
exec python3 /hassio_gassistant.py "$CRED_JSON" "$PROJECT_ID" "$MODEL_ID" < /dev/null
|
||||
|
||||
Reference in New Issue
Block a user