Package: python-openhab
python library for accessing the openHAB REST APIThis library allows for easily accessing the openHAB REST API. A number of features are implemented but not all, this is
work in progress.Requirementspython >= 3.8python :: dateutilpython :: httpxpython :: authlibopenHAB version 3 / 4InstallationInstall the latest version using pip:pipinstallpython-openhabExampleExample usage of the library:fromopenhabimportOpenHABbase_url='http://localhost:8080/rest'openhab=OpenHAB(base_url)# fetch all itemsitems=openhab.fetch_all_items()sunset=items.get('Sunset')print(sunset.state)# fetch a single itemitem=openhab.get_item('light_switch')# turn a switch onitem.on()# send a state update (this only update the state)item.state='OFF'# send a commanditem.command('ON')# check if item state is NULLifitem.stateisNoneanditem.is_state_null():pass# check if item state is UNDEFifitem.stateisNoneanditem.is_state_undef():pass# fetch some grouplights_group=openhab.get_item('lights_group')# send command to grouplights_group.on()# send update to each memberforvinlights_group.members.values():v.update('OFF')Note on NULL and UNDEFIn openHAB items may have two states named NULL and UNDEF, which have distinct meanings but basically indicate that an
item has no usable value. This library sets the state of an item, regardless of their openHAB value being NULL or UNDEF,
to None. This in order to ease working with the library as we do cast certain types to native types.In order to check if an item's state is either NULL or UNDEF, you can use the helper functions:item.is_state_null()item.is_state_undef()Experimental OAuth2 SupportIn order to try out OAuth2 authentication, you first need to register with the openHAB endpoint in order to retrieve a
token and refresh token.Assuming your openHAB instance runs athttp://127.0.0.1:8080(replace with the correct one), use the following snippet
to retrieve a token:importpathlibimportopenhab.oauth2_helperimportosimportjsonurl_base='http://127.0.0.1:8080'api_username='admin'api_password='admin'oauth2_client_id='http://127.0.0.1/auth'oauth2_token_cache=pathlib.Path(__file__).resolve().parent/'.oauth2_token_test'# this must be set for oauthlib to work on http (do not set for https!)os.environ['OAUTHLIB_INSECURE_TRANSPORT']='1'oauth2_token=openhab.oauth2_helper.get_oauth2_token(url_base,username=api_username,password=api_password)withoauth2_token_cache.open('w')asfhdl:json.dump(oauth2_token,fhdl,indent=2,sort_keys=True)The JSON that is returned is required for authenticating to openHAB using OAuth2 as well as a refresh token which is
used for refreshing a session.Next try connecting to openHAB using this library as follows:importopenhabimportpathlibimportjsonimportosurl_base='http://127.0.0.1:8080'url_rest=f'{url_base}/rest'oauth2_client_id='http://127.0.0.1/auth'oauth2_token_cache=pathlib.Path(__file__).resolve().parent/'.oauth2_token_test'# this must be set for oauthlib to work on http (do not set for https!)os.environ['OAUTHLIB_INSECURE_TRANSPORT']='1'oauth2_config={'client_id':oauth2_client_id,'token_cache':str(oauth2_token_cache)}withoauth2_token_cache.open('r')asfhdl:oauth2_config['token']=json.load(fhdl)oh=openhab.OpenHAB(base_url=url_rest,oauth2_config=oauth2_config)o=oh.get_item('test_item')print(o)