Browse Source

Very rough initial version - can turn on and off my lights!

tags/v1.0.0
Rob Connolly 4 years ago
commit
f6a688c479
4 changed files with 70 additions and 0 deletions
  1. +2
    -0
      .gitignore
  2. +65
    -0
      __init__.py
  3. +1
    -0
      lib/.gitignore
  4. +2
    -0
      vocab/en-us/TurnOnOffKeyword.voc

+ 2
- 0
.gitignore View File

@@ -0,0 +1,2 @@
*.swp
*.pyc

+ 65
- 0
__init__.py View File

@@ -0,0 +1,65 @@
from os.path import dirname, join

from adapt.intent import IntentBuilder
from mycroft.skills.core import MycroftSkill
from requests import post
import json

__author__ = 'robconnolly'


class HomeAssistantClient(object):

def __init__(self, host, password, port=8123):
self.url = "http://%s:%d" % (host, port)
self.headers = {
'x-ha-access': password,
'Content-Type': 'application/json'
}

def execute_service(self, domain, service, data):
post("%s/api/services/%s/%s" % (self.url, domain, service), headers=self.headers, data=json.dumps(data))

# TODO - Localization
class HomeAssistantSkill(MycroftSkill):
def __init__(self):
super(HomeAssistantSkill, self).__init__(name="HomeAssistantSkill")
self.ha = HomeAssistantClient(self.config.get('host'),
self.config.get('password'))

def initialize(self):
self.load_vocab_files(join(dirname(__file__), 'vocab', 'en-us'))

prefixes = ['turn', 'switch']
self.__register_prefixed_regex(prefixes, "(?P<Action>on|off) (?P<Entity>.*)")

intent = IntentBuilder("TurnOnOffIntent").require("TurnOnOffKeyword").require("Action").require("Entity").build()
self.register_intent(intent, self.handle_intent)

def __register_prefixed_regex(self, prefixes, suffix_regex):
for prefix in prefixes:
self.register_regex(prefix + ' ' + suffix_regex)

def handle_intent(self, message):
entity = message.metadata.get("Entity")
action = message.metadata.get("Action")

# TODO: we should use the HA api to search for a matching entity
ha_entity = {"entity_id": "light." + entity.replace(" ", "_")}
print ha_entity

if action == "on":
self.speak("Turning on %s" % entity)
self.ha.execute_service("light", "turn_on", ha_entity)
elif action == "off":
self.speak("Turning off %s" % entity)
self.ha.execute_service("light", "turn_off", ha_entity)
else:
self.speak("I don't know what you want me to do.")

def stop(self):
pass


def create_skill():
return HomeAssistantSkill()

+ 1
- 0
lib/.gitignore View File

@@ -0,0 +1 @@
*

+ 2
- 0
vocab/en-us/TurnOnOffKeyword.voc View File

@@ -0,0 +1,2 @@
turn
switch

Loading…
Cancel
Save