Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for HTTP basic authentication. #105

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: ""