Skip to content

Commit

Permalink
fix: force setup of skill, change to class properties
Browse files Browse the repository at this point in the history
  • Loading branch information
mikejgray committed Feb 5, 2024
1 parent 162bac4 commit 9308378
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 28 deletions.
44 changes: 17 additions & 27 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,37 +39,23 @@

class openHABSkill(OVOSSkill):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def __init__(self, *args, bus=None, skill_id='', **kwargs):
super().__init__(*args, bus=bus, skill_id=skill_id, **kwargs)

self.command_headers = {"Content-type": "text/plain"}

self.polling_headers = {"Accept": "application/json"}

self.url = None
self.lightingItemsDic = dict()
self.switchableItemsDic = dict()
self.currentTempItemsDic = dict()
self.currentHumItemsDic = dict()
#self.currentThermostatItemsDic = dict()
self.targetTemperatureItemsDic = dict()
#self.homekitHeatingCoolingModeDic = dict()
self.getTaggedItems()

def initialize(self):

supported_languages = ["en-us", "it-it", "de-de", "es-es"]

if self.lang not in supported_languages:
self.log.warning("Unsupported language for " + self.name + ", shutting down skill.")
self.shutdown()

self.handle_websettings_update()

if self.url is not None:
self.getTaggedItems()
else:
self.speak_dialog('ConfigurationNeeded')

refresh_tagged_items_intent = IntentBuilder("RefreshTaggedItemsIntent").require("RefreshTaggedItemsKeyword").build()
self.register_intent(refresh_tagged_items_intent, self.handle_refresh_tagged_items_intent)

Expand All @@ -91,17 +77,21 @@ def initialize(self):
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 get_config(self, key):
return (self.settings.get(key) or self.config_core.get('openHABSkill', {}).get(key))

def handle_websettings_update(self):
if self.get_config('host') is not None and self.get_config('port') is not None:
self.url = "http://%s:%s/rest" % (self.get_config('host'), self.get_config('port'))
self.getTaggedItems()
else:
self.url = None
@property
def host(self):
return self.get_config('host')

@property
def port(self):
return self.get_config('port')

@property
def url(self):
if self.host and self.port:
return f"http://{self.host}:{self.port}/rest"

def getTaggedItems(self):
#find all the items tagged Lighting and Switchable from openHAB
Expand All @@ -118,7 +108,7 @@ def getTaggedItems(self):
if self.url is None:
self.log.error("Configuration needed!")
self.speak_dialog('ConfigurationNeeded')
else:
else:
requestUrl = self.url+"/items?recursive=false"

try:
Expand All @@ -128,7 +118,7 @@ def getTaggedItems(self):
for x in range(0,len(json_response)):
if ("Lighting" in json_response[x]['tags']):
self.lightingItemsDic.update({json_response[x]['name']: json_response[x]['label']})
elif ("Switchable" in json_response[x]['tags']):
elif ("Switch" in json_response[x]['tags'] or "Switchable" in json_response[x]['tags']):
self.switchableItemsDic.update({json_response[x]['name']: json_response[x]['label']})
elif ("CurrentTemperature" in json_response[x]['tags']):
self.currentTempItemsDic.update({json_response[x]['name']: json_response[x]['label']})
Expand Down
28 changes: 28 additions & 0 deletions test/test_main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,38 @@
# pylint: disable=missing-docstring
from threading import Event
from unittest.mock import Mock
import pytest
from ovos_skill_openhab import openHABSkill
from ovos_plugin_manager.skills import find_skill_plugins
from ovos_utils.fakebus import FakeBus
# TODO: Mock settings path, set up fixtures


def test_skill_is_a_valid_plugin():
assert "ovos-skill-openhab.mikejgray" in find_skill_plugins().keys()


def test_no_config_calls_speak_dialog():
fake_bus = FakeBus()
fake_bus.emitter = fake_bus.ee
fake_bus.connected_event = Event()
fake_bus.connected_event.set()
fake_bus.run_forever()
skill = openHABSkill(bus=fake_bus, settings={"host": "", "port": ""}, skill_id="ovos-skill-openhab.mikejgray")
skill.speak_dialog = Mock()
skill.speak_dialog.assert_called_with({})


def test_list_all_devices_from_demo():
fake_bus = FakeBus()
fake_bus.emitter = fake_bus.ee
fake_bus.connected_event = Event()
fake_bus.connected_event.set()
fake_bus.run_forever()
skill = openHABSkill(bus=fake_bus, settings={"host": "localhost", "port": "8080"}, skill_id="ovos-skill-openhab.mikejgray")
skill.speak_dialog = Mock()
skill.speak_dialog.assert_not_called()


if __name__ == "__main__":
pytest.main()
2 changes: 1 addition & 1 deletion version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION_MAJOR = 2
VERSION_MINOR = 0
VERSION_BUILD = 0
VERSION_ALPHA = 0
VERSION_ALPHA = 0

0 comments on commit 9308378

Please sign in to comment.