Handle unconfigured state (#73)

* Set default value for host in settingsmeta and remove deprecated option
Check for None in config values
Move getTaggedItems to initialize so that say_dialog works and handle
request error

Signed-off-by: Håvard Moen <post@haavard.name>

* add settings.json to .gitignore

Signed-off-by: Håvard Moen <post@haavard.name>

* handle change of settings from web

Signed-off-by: Håvard Moen <post@haavard.name>
pull/77/head
Håvard Moen 2019-10-15 08:34:41 +02:00 committed by mortommy
parent a09bf3b1ee
commit 0ee11e8366
3 changed files with 21 additions and 4 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
*.pyc
settings.json

View File

@ -48,7 +48,10 @@ class openHABSkill(MycroftSkill):
def __init__(self):
super(openHABSkill, self).__init__(name="openHABSkill")
self.url = "http://%s:%s/rest" % (self.settings.get('host'), self.settings.get('port'))
if self.settings.get('host') is not None and self.settings.get('port') is not None:
self.url = "http://%s:%s/rest" % (self.settings.get('host'), self.settings.get('port'))
else:
self.url = None
self.command_headers = {"Content-type": "text/plain"}
@ -62,8 +65,6 @@ class openHABSkill(MycroftSkill):
self.targetTemperatureItemsDic = dict()
#self.homekitHeatingCoolingModeDic = dict()
self.getTaggedItems()
def initialize(self):
supported_languages = ["en-US", "it-IT", "de-DE", "es-ES"]
@ -72,6 +73,11 @@ class openHABSkill(MycroftSkill):
self.log.warning("Unsupported language for " + self.name + ", shutting down skill.")
self.shutdown()
if self.url is not None:
self.getTaggedItems()
else:
self.speak_dialog('GetItemsListError')
refresh_tagged_items_intent = IntentBuilder("RefreshTaggedItemsIntent").require("RefreshTaggedItemsKeyword").build()
self.register_intent(refresh_tagged_items_intent, self.handle_refresh_tagged_items_intent)
@ -90,6 +96,8 @@ class openHABSkill(MycroftSkill):
list_items_intent = IntentBuilder("ListItemsIntent").require("ListItemsKeyword").build()
self.register_intent(list_items_intent, self.handle_list_items_intent)
self.settings_change_callback = self.handle_websettings_update
def getTaggedItems(self):
#find all the items tagged Lighting and Switchable from openHAB
#the labeled items are stored in dictionaries
@ -131,6 +139,9 @@ class openHABSkill(MycroftSkill):
except KeyError:
pass
except Exception:
LOGGER.error("Some issues with the command execution!")
self.speak_dialog('GetItemsListError')
def findItemName(self, itemDictionary, messageItem):
@ -355,6 +366,11 @@ class openHABSkill(MycroftSkill):
LOGGER.error("Item not found!")
self.speak_dialog('ItemNotFoundError')
def handle_websettings_update(self):
if self.settings.get('host') is not None and self.settings.get('port') is not None:
self.url = "http://%s:%s/rest" % (self.settings.get('host'), self.settings.get('port'))
self.getTaggedItems()
def sendStatusToItem(self, ohItem, command):
requestUrl = self.url+"/items/%s/state" % (ohItem)
req = requests.put(requestUrl, data=command, headers=self.command_headers)

View File

@ -1,4 +1,3 @@
name: openHAB
skillMetadata:
sections:
- name: Connection
@ -6,6 +5,7 @@ skillMetadata:
- name: host
label: openHAB server IP
type: text
value: 127.0.0.1
- name: port
label: openHAB server port
type: number