1
- from __future__ import absolute_import , division , print_function , unicode_literals
2
1
# -*- coding: utf-8 -*-
3
2
"""python library for accessing the openHAB REST API"""
4
3
5
4
#
6
- # Georges Toth (c) 2016 <[email protected] >
5
+ # Georges Toth (c) 2016-present <[email protected] >
7
6
#
8
7
# python-openhab is free software: you can redistribute it and/or modify
9
8
# it under the terms of the GNU General Public License as published by
24
23
import requests
25
24
from requests .auth import HTTPBasicAuth
26
25
from openhab .items import Item , DateTimeItem , SwitchItem , NumberItem , ContactItem
26
+ import warnings
27
+ import typing
27
28
28
29
__author__ = 'Georges Toth <[email protected] >'
29
30
__license__ = 'AGPLv3+'
30
31
31
32
32
-
33
- class openHAB (object ):
33
+ class OpenHAB :
34
34
"""openHAB REST API client
35
35
"""
36
36
37
- def __init__ (self , base_url , username = None , password = None , http_auth = None ):
37
+ def __init__ (self , base_url : str ,
38
+ username : typing .Optional [str ] = None ,
39
+ password : typing .Optional [str ] = None ,
40
+ http_auth : typing .Optional [requests .auth .AuthBase ] = None ) -> None :
38
41
"""
39
42
Args:
40
43
base_url (str): The openHAB REST URL, e.g. http://example.com/rest
@@ -46,7 +49,7 @@ def __init__(self, base_url, username=None, password=None, http_auth=None):
46
49
specify a custom http authentication object of type :class:`requests.auth.AuthBase`.
47
50
48
51
Returns:
49
- openHAB : openHAB class instance.
52
+ OpenHAB : openHAB class instance.
50
53
"""
51
54
self .base_url = base_url
52
55
@@ -55,10 +58,11 @@ def __init__(self, base_url, username=None, password=None, http_auth=None):
55
58
56
59
if http_auth is not None :
57
60
self .session .auth = http_auth
58
- elif username is not None and password is not None :
61
+ elif not ( username is None or password is None ) :
59
62
self .session .auth = HTTPBasicAuth (username , password )
60
63
61
- def _check_req_return (self , req ):
64
+ @staticmethod
65
+ def _check_req_return (req : requests .Response ) -> None :
62
66
"""Internal method for checking the return value of a REST HTTP request.
63
67
64
68
Args:
@@ -71,12 +75,12 @@ def _check_req_return(self, req):
71
75
ValueError: Raises a ValueError exception in case of a non-successful
72
76
REST request.
73
77
"""
74
- if not (req . status_code >= 200 and req .status_code < 300 ):
78
+ if not (200 <= req .status_code < 300 ):
75
79
req .raise_for_status ()
76
80
77
81
return None
78
82
79
- def req_get (self , uri_path ) :
83
+ def req_get (self , uri_path : str ) -> typing . Any :
80
84
"""Helper method for initiating a HTTP GET request. Besides doing the actual
81
85
request, it also checks the return value and returns the resulting decoded
82
86
JSON data.
@@ -91,7 +95,7 @@ def req_get(self, uri_path):
91
95
self ._check_req_return (r )
92
96
return r .json ()
93
97
94
- def req_post (self , uri_path , data = None ):
98
+ def req_post (self , uri_path : str , data : typing . Optional [ dict ] = None ) -> None :
95
99
"""Helper method for initiating a HTTP POST request. Besides doing the actual
96
100
request, it also checks the return value and returns the resulting decoded
97
101
JSON data.
@@ -108,7 +112,7 @@ def req_post(self, uri_path, data=None):
108
112
109
113
return None
110
114
111
- def req_put (self , uri_path , data = None ):
115
+ def req_put (self , uri_path : str , data : typing . Optional [ dict ] = None ) -> None :
112
116
"""Helper method for initiating a HTTP PUT request. Besides doing the actual
113
117
request, it also checks the return value and returns the resulting decoded
114
118
JSON data.
@@ -126,13 +130,13 @@ def req_put(self, uri_path, data=None):
126
130
return None
127
131
128
132
# fetch all items
129
- def fetch_all_items (self ):
133
+ def fetch_all_items (self ) -> dict :
130
134
"""Returns all items defined in openHAB except for group-items
131
135
132
136
Returns:
133
137
dict: Returns a dict with item names as key and item class instances as value.
134
138
"""
135
- items = {}
139
+ items = {} # type: dict
136
140
res = self .req_get ('/items/' )
137
141
138
142
for i in res :
@@ -145,7 +149,7 @@ def fetch_all_items(self):
145
149
146
150
return items
147
151
148
- def get_item (self , name ) :
152
+ def get_item (self , name : str ) -> object :
149
153
"""Returns an item with its state and type as fetched from openHAB
150
154
151
155
Args:
@@ -158,7 +162,7 @@ def get_item(self, name):
158
162
159
163
return self .json_to_item (json_data )
160
164
161
- def json_to_item (self , json_data ) :
165
+ def json_to_item (self , json_data : dict ) -> object :
162
166
"""This method takes as argument the RAW (JSON decoded) response for an openHAB
163
167
item. It checks of what type the item is and returns a class instance of the
164
168
specific item filled with the item's state.
@@ -180,7 +184,7 @@ def json_to_item(self, json_data):
180
184
else :
181
185
return Item (self , json_data )
182
186
183
- def get_item_raw (self , name ) :
187
+ def get_item_raw (self , name : str ) -> typing . Any :
184
188
"""Private method for fetching a json configuration of an item.
185
189
186
190
Args:
@@ -190,3 +194,10 @@ def get_item_raw(self, name):
190
194
dict: A JSON decoded dict.
191
195
"""
192
196
return self .req_get ('/items/{}' .format (name ))
197
+
198
+
199
+ class openHAB (OpenHAB ):
200
+ def __init__ (self , * args , ** kwargs ):
201
+ super ().__init__ (* args , ** kwargs )
202
+
203
+ warnings .warn ('The use of the "openHAB" class is deprecated, please use "OpenHAB" instead.' , DeprecationWarning )
0 commit comments