Skip to content

Commit

Permalink
Added support for HTTP basic authentication.
Browse files Browse the repository at this point in the history
Signed-off-by: Rick Osgood <rick@richardosgood.com>
  • Loading branch information
rickoooooo committed Jan 13, 2021
1 parent 2e679da commit c372f40
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
12 changes: 8 additions & 4 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from rapidfuzz import fuzz

import requests
from requests.auth import HTTPBasicAuth
import json

# v 0.1 - just switch on and switch off a fix light
Expand Down Expand Up @@ -54,6 +55,7 @@ def __init__(self):
self.polling_headers = {"Accept": "application/json"}

self.url = None
self.credentials = None
self.lightingItemsDic = dict()
self.switchableItemsDic = dict()
self.currentTempItemsDic = dict()
Expand Down Expand Up @@ -104,6 +106,8 @@ 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('username') is not None and self.get_config('password') is not None:
self.credentials = HTTPBasicAuth(self.get_config('username'), self.get_config('password'))
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()
Expand All @@ -129,7 +133,7 @@ def getTaggedItems(self):
requestUrl = self.url+"/items?recursive=false"

try:
req = requests.get(requestUrl, headers=self.polling_headers)
req = requests.get(requestUrl, headers=self.polling_headers, auth=self.credentials)
if req.status_code == 200:
json_response = req.json()
for x in range(0,len(json_response)):
Expand Down Expand Up @@ -362,13 +366,13 @@ def handle_setTemp_status_intent(self, message):

def sendStatusToItem(self, ohItem, command):
requestUrl = self.url+"/items/%s/state" % (ohItem)
req = requests.put(requestUrl, data=command, headers=self.command_headers)
req = requests.put(requestUrl, data=command, headers=self.command_headers, auth=self.credentials)

return req.status_code

def sendCommandToItem(self, ohItem, command):
requestUrl = self.url+"/items/%s" % (ohItem)
req = requests.post(requestUrl, data=command, headers=self.command_headers)
req = requests.post(requestUrl, data=command, headers=self.command_headers, auth=self.credentials)

return req.status_code

Expand All @@ -377,7 +381,7 @@ def getCurrentItemStatus(self, ohItem):
state = None

try:
req = requests.get(requestUrl, headers=self.command_headers)
req = requests.get(requestUrl, headers=self.command_headers, auth=self.credentials)

if req.status_code == 200:
state = req.text
Expand Down
8 changes: 8 additions & 0 deletions settingsmeta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,11 @@ skillMetadata:
label: openHAB server port
type: number
value: 8080
- name: username
label: openHAB username (optional)
type: text
value: ""
- name: password
label: openHAB password (optional)
type: password
value: ""

0 comments on commit c372f40

Please sign in to comment.