11 Commits

7 changed files with 41 additions and 51 deletions
+1 -3
View File
@@ -72,9 +72,7 @@ weather:
- возможен дождь сегодня?
- прогноз погоды на сегодня
- погода
home_assistant_execute:
home_assistant:
- включи телевизор
- выключи телевизор
- начни уборку
home_assistant_get:
- тест
-4
View File
@@ -1,4 +0,0 @@
пылесос:
- entity_id:vacuum.roborock_vacuum_m1s
- state:находится в
- attributes.battery_level:а его уровень зарядки
-36
View File
@@ -1,6 +1,4 @@
import requests
import yaml
from fuzzywuzzy import process
from data import config
@@ -9,7 +7,6 @@ class HomeAssistant:
def __init__(self):
self.url = "http://192.168.0.112:9999/api"
self.token = config.HOME_ASSISTANT_TOKEN
self.HA_CMD_LIST = yaml.safe_load(open('data/home_assistant_entities.yaml', encoding='utf8'))
def get_info(self, state):
response = requests.get(
@@ -18,9 +15,6 @@ class HomeAssistant:
"Authorization": "Bearer " + self.token
}
)
for entity in response.json():
if entity["entity_id"] == state:
return entity
return response
def send_process(self, command="выключи телевизор"):
@@ -35,33 +29,3 @@ class HomeAssistant:
if response.status_code == 200:
return True
return False
def voice_to_name(self, voice: str) -> str:
words = voice.lower().split()
best_match = None
highest_score = 0
for word in words:
result, score = process.extractOne(word, self.HA_CMD_LIST.keys())
if score > highest_score:
highest_score = score
best_match = result
return best_match
def validate_info(self, name: str):
answer = name
entity_config = self.HA_CMD_LIST.get(name)
if entity_config:
# Создание словаря, разделяя каждый элемент конфигурации на ключ и значение
entity_details = {item.split(':')[0]: item.split(':')[1] for item in entity_config}
entity_id = entity_details.pop("entity_id", None)
if entity_id:
responses = self.get_info(entity_id)
for attribute_path, label in entity_details.items():
response = responses
try:
for attribute in attribute_path.split("."):
response = response[attribute]
answer += f" {label} {response}"
except KeyError:
continue
return answer
+2 -2
View File
@@ -11,7 +11,7 @@ from fuzzywuzzy import fuzz
from pvrecorder import PvRecorder
from data import config
from modules import HomeAssistant
from modules.HomeAssistant import HomeAssistant
from utils import download_models, execute_cmd, play
@@ -21,7 +21,7 @@ class Jarvis:
self.recorder = None
self.CDIR = os.getcwd()
self.VA_CMD_LIST = yaml.safe_load(open('data/commands.yaml', encoding='utf8'))
self.home_assistant = HomeAssistant.HomeAssistant()
self.home_assistant = HomeAssistant()
self.porcupine = pvporcupine.create(
access_key=config.PICOVOICE_TOKEN,
keywords=['jarvis'],
+5
View File
@@ -0,0 +1,5 @@
from modules.HomeAssistant import HomeAssistant
home_assistant = HomeAssistant()
response = home_assistant.get_info("")
print(response.text)
+1 -5
View File
@@ -7,9 +7,5 @@ def execute_cmd(self, cmd: str, recognized_phrase: str, voice: str):
self.play("off", True)
self.porcupine.delete()
exit(0)
elif cmd == 'home_assistant_execute':
elif cmd == 'home_assistant':
self.home_assistant.send_process(recognized_phrase)
elif cmd == 'home_assistant_get':
entity_name = self.home_assistant.voice_to_name(voice)
entity_info = self.home_assistant.validate_info(entity_name)
print(entity_info)
Executable
+31
View File
@@ -0,0 +1,31 @@
import time
import sounddevice as sd
import torch
language = 'ru'
model_id = 'ru_v3'
sample_rate = 48000 # 48000
speaker = 'aidar' # aidar, baya, kseniya, xenia, random
put_accent = True
put_yo = True
device = torch.device('cpu') # cpu или gpu
text = "Хауди Хо, друзья!!!"
model, _ = torch.hub.load(repo_or_dir='snakers4/silero-models',
model='silero_tts',
language=language,
speaker=model_id)
model.to(device)
def va_speak(what: str):
audio = model.apply_tts(text=what + "..",
speaker=speaker,
sample_rate=sample_rate,
put_accent=put_accent,
put_yo=put_yo)
sd.play(audio, sample_rate * 1.05)
time.sleep((len(audio) / sample_rate) + 0.5)
sd.stop()