Init
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
import appdaemon.plugins.hass.hassapi as hass
|
||||
import json
|
||||
|
||||
#
|
||||
# Yandex.Alice integration
|
||||
#
|
||||
# Args:
|
||||
#
|
||||
#
|
||||
#
|
||||
#
|
||||
# None
|
||||
#
|
||||
# Release Notes
|
||||
#
|
||||
# Version 1.0:
|
||||
# Initial Version
|
||||
|
||||
class YandexAlice(hass.Hass):
|
||||
|
||||
def initialize(self):
|
||||
self.register_endpoint(self.api_call)
|
||||
self.sessionStorage = {}
|
||||
self.log('started')
|
||||
|
||||
def api_call(self, request):
|
||||
# js = json.dumps(request, indent=4, sort_keys=True)
|
||||
# self.log('Request: %s' % js)
|
||||
|
||||
response = {
|
||||
"version": request['version'],
|
||||
"session": request['session'],
|
||||
"response": {
|
||||
"end_session": False
|
||||
}
|
||||
}
|
||||
self.handle_dialog(request, response)
|
||||
|
||||
# self.log('response: {}'.format(response['response']['text']))
|
||||
# js = json.dumps(response, indent=4, sort_keys=True)
|
||||
# self.log('Response: %s' % js)
|
||||
|
||||
return response, 200
|
||||
|
||||
# Функция для непосредственной обработки диалога.
|
||||
def handle_dialog(self, req, res):
|
||||
user_id = req['session']['user_id']
|
||||
command = req['request']['original_utterance'].lower()
|
||||
command_password = req['request']['command'].lower().replace(" ", "")
|
||||
password = "123456"
|
||||
self.log('command: {}'.format(command))
|
||||
|
||||
if command == "ping":
|
||||
res['response']['text'] = 'pong =)'
|
||||
return
|
||||
|
||||
if user_id not in self.sessionStorage:
|
||||
self.sessionStorage[user_id] = { 'auth': False }
|
||||
res['response']['text'] = 'Привет. Я могу управлять твоим домом. Для начала работы назови пароль.'
|
||||
return
|
||||
|
||||
if command_password != password and self.sessionStorage[user_id]['auth'] == False:
|
||||
res['response']['text'] = 'Неправильный пароль: %s.' % command_password
|
||||
return
|
||||
|
||||
# Обрабатываем ответ пользователя.
|
||||
if command_password == password and self.sessionStorage[user_id]['auth'] == False:
|
||||
self.sessionStorage[user_id]['auth'] = True
|
||||
res['response']['text'] = 'Вы авторизованы.'
|
||||
return
|
||||
|
||||
self.handle_command(req, res)
|
||||
return
|
||||
|
||||
# Функция возвращает две подсказки для ответа.
|
||||
def handle_command(self, req, res):
|
||||
user_id = req['session']['user_id']
|
||||
command = req['request']['original_utterance'].lower()
|
||||
command = command.replace('попроси управление домом', '').strip()
|
||||
command = command.replace('алиса', '').strip()
|
||||
isAuth = self.sessionStorage[user_id]['auth'] == True
|
||||
|
||||
if not isAuth:
|
||||
res['response']['text'] = 'Вы не авторизованы.'
|
||||
return
|
||||
|
||||
if command in ["включи свет", "включить свет"]:
|
||||
res['response']['text'] = 'Включаю свет.'
|
||||
self.turn_on("group.all_lights")
|
||||
self.turn_on("switch.kitchen_mini_light")
|
||||
return
|
||||
|
||||
if command in ["включи свет в зале", "включить свет в зале","включи свет в гостинной", "включить свет в гостинной"]:
|
||||
res['response']['text'] = 'Свет включен.'
|
||||
self.turn_on("group.mainroom_light")
|
||||
return
|
||||
|
||||
if command in ["выключи свет в зале", "выключить свет в зале","выключи свет в гостинной", "выключить свет в гостинной"]:
|
||||
res['response']['text'] = 'Свет выключен.'
|
||||
self.turn_off("group.mainroom_light")
|
||||
return
|
||||
|
||||
if command in ["выключи свет", "выключить свет", "выключи весь свет"]:
|
||||
res['response']['text'] = 'Свет выключен.'
|
||||
self.turn_off("group.all_lights")
|
||||
self.turn_off("switch.kitchen_mini_light")
|
||||
return
|
||||
|
||||
if command in ["хватит", "алиса, хватит", "алиса хватит", "отмена"]:
|
||||
res['response']['text'] = 'Хорошо'
|
||||
res['response']['end_session'] = True
|
||||
return
|
||||
|
||||
if command in ["я ушел", "алиса я ушел"]:
|
||||
self.call_service("alarm_control_panel/alarm_arm_away", entity_id="alarm_control_panel.ha_alarm")
|
||||
res['response']['text'] = 'Приятной дороги.'
|
||||
return
|
||||
|
||||
if command in ["я вернулся", "я пришёл", "я пришел"]:
|
||||
self.call_service("alarm_control_panel/alarm_disarm", entity_id="alarm_control_panel.ha_alarm")
|
||||
res['response']['text'] = 'Добро пожаловать домой.'
|
||||
return
|
||||
|
||||
if command in ["запусти навык управление домом"]:
|
||||
res['response']['text'] = 'Уже запущен.'
|
||||
return
|
||||
|
||||
res['response']['text'] = 'Извините, я не знаю команды %s.' % command
|
||||
return
|
||||
@@ -0,0 +1,9 @@
|
||||
#################################
|
||||
# YANDEX.ALICE #
|
||||
#################################
|
||||
alice:
|
||||
module: alice
|
||||
class: YandexAlice
|
||||
global_dependencies:
|
||||
- globals
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
import appdaemon.plugins.hass.hassapi as hass
|
||||
import datetime
|
||||
import globals
|
||||
|
||||
#
|
||||
# App recieves xiaomi scale readings, parse user weight and set sensor in home-assistant
|
||||
#
|
||||
# Args:
|
||||
#
|
||||
# sensor = generated sensor name
|
||||
# friendly_name (optional) = sensor friendly name
|
||||
#
|
||||
# None
|
||||
#
|
||||
# Release Notes
|
||||
#
|
||||
# Version 1.0:
|
||||
# Initial Version
|
||||
|
||||
class MiScale(hass.Hass):
|
||||
def initialize(self):
|
||||
if not globals.check_args(self, ["sensor"]):
|
||||
return
|
||||
self.register_endpoint(self.api_call)
|
||||
self.log('started')
|
||||
|
||||
def api_call(self, data):
|
||||
# filter only my data
|
||||
prev_weight = 70
|
||||
weight_date = datetime.datetime.now()
|
||||
for current in data:
|
||||
if abs(current['Weight'] - prev_weight) > 5:
|
||||
continue
|
||||
# self.log(current)
|
||||
prev_weight = current['Weight']
|
||||
weight_date = current['Time']
|
||||
|
||||
attributes = {}
|
||||
attributes['datetime'] = weight_date
|
||||
attributes['unit_of_measurement'] = 'kg'
|
||||
attributes['description'] = 'Created and updated from appdaemon (mi_scale)'
|
||||
if 'friendly_name' in self.args:
|
||||
attributes['friendly_name'] = self.args['friendly_name']
|
||||
|
||||
self.set_state(self.args['sensor'], state = prev_weight, attributes = attributes)
|
||||
return 'ok', 200
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
#################################
|
||||
# SCALE #
|
||||
#################################
|
||||
mi_scale:
|
||||
module: mi_scale
|
||||
class: MiScale
|
||||
sensor: sensor.xiaomi_scale
|
||||
friendly_name: "Весы"
|
||||
global_dependencies:
|
||||
- globals
|
||||
@@ -0,0 +1,34 @@
|
||||
import appdaemon.plugins.hass.hassapi as hass
|
||||
|
||||
#
|
||||
# App to serve tags list via api call
|
||||
#
|
||||
# Args:
|
||||
#
|
||||
# secrets_file = path to secrets file
|
||||
#
|
||||
# None
|
||||
#
|
||||
# Release Notes
|
||||
#
|
||||
# Version 1.0:
|
||||
# Initial Version
|
||||
|
||||
class TagsList(hass.Hass):
|
||||
secrets_file = '/conf/secrets.yaml'
|
||||
def initialize(self):
|
||||
self.register_endpoint(self.api_call)
|
||||
if 'secrets_file' in self.args:
|
||||
self.secrets_file = self.args['secrets_file']
|
||||
|
||||
def api_call(self, data):
|
||||
tags = []
|
||||
|
||||
with open(self.secrets_file) as f:
|
||||
content = f.readlines()
|
||||
for line in content:
|
||||
if (line.startswith('tag_')):
|
||||
line = line.replace('\n','')
|
||||
tag = line.split(':')[1].strip()
|
||||
tags.append(tag)
|
||||
return tags, 200
|
||||
@@ -0,0 +1,9 @@
|
||||
#################################
|
||||
# TAGS #
|
||||
#################################
|
||||
tags:
|
||||
module: tags
|
||||
class: TagsList
|
||||
secrets_file: /conf/secrets.yaml
|
||||
global_dependencies:
|
||||
- globals
|
||||
Reference in New Issue
Block a user