Package: python-openhab-crud
Python openHAB CRUDA simpleCRUDfor accessing the openHAB REST API. Whether via the cloud or locally. TheCRUDcan be used only for openHABItems.CRUDmeans that you can runCreate,Read,UpdateandDeleteonItems. The openHAB Cloud does not allow to useCreateandDelete. This is the case for security reasons. In this case you have to create or delete an item locally.InstallationYou can install it by usingpip:python3 -m pip install python-openhab-crudUsageCRUDmeans that you can runCreate,Read,UpdateandDeleteonItems.ReadinganItemcan have two different meanings: You canReadthe wholeItemor only theStateof anItem. The latter can be done with thegetStatefunction. Also canUpdatinganItemhave two different meanings. In openHAB you can change anStateby usingsendCommandorpostUpdate. The functionupdateis an alias forsendCommandorpostUpdate. You can specifysendCommandorpostUpdateas parameters forupdate. If no parameter is specified,postUpdateis used automatically.At first you have to import theCRUDmodule:from openhab import CRUDCreate connection to the openHAB Cloud (as example myopenhab.org)If you want to create a connection to theopenHAB Cloudyou have to run:crud = CRUD("https://myopenhab.org", "<your_email>@<your_provider>", "<email_password>")Please make sure to replace<your_email>@<your_provider>with your email address and<email_password>with your password that you used for youropenHAB Cloud account.Create connection to your local openHAB instanceIf you want to create a connection to your localopenHABinstance you have to replace<username>and<password>with the username and password of your localopenHAB account:crud = CRUD("http://<your_ip>:8080", "<username>", "<password>")Get all ItemsIf you want to retrieve all Items via theREST APIofopenHAByou can run:items = crud.getAllItems()The return value is alistofdictionaries(dict). Each item is represented asdict. You can check it by usingtype:print(type(items))You can print each item separately by running:for item in items:
    print(item)And of course you can access each property by using thegetmethod:for item in items:
    item_type = item.get("type")
    item_name = item.get("name")
    item_state = item.get("state")

    print("Received item type " + item_type + " with name " + item_name + " and state " + item_state)Creating new itemsYou can only create a newItemif you have access to your localopenHABinstance. For security reasons, this is prohibited via the cloud.AnItemmust contain minimum itsNameand itsType. ALabel, a givenStateorGroupswill be optional.So at least you can run as example following:crud.create("testItem", "String", "my test string", ["testGroup"], "ON")
crud.create("testItem2", "String", "my test string", ["testGroup"])
crud.create("testItem3", "Switch", "my test switch")
crud.create("testItem4", "Switch")Of course you can use a different order:crud.create(state="ON", type="String", name="testItem")Groupsmust be passed as alist, since anItemcan be assigned to severalGroups:crud.create("testItem", "String", "my test string", ["group1", "group2", "group3"])The function automatically checks if theTypeof theItemexists in openHAB and thus can be created. If you want to set aStatefor thisItem, it is also automatically checked whether theStateis consistently valid for thisType.There will be no response if it is correct!Reading an itemReadinganItemmeans that all information about thisItemis queried and not only theState. You can do this as follows:testItem = crud.read("testItem")You will receive anItemas adictionary(dict):print(type(testItem))And of course you can access each property by using thegetmethod:item_type = testItem.get("type")
item_name = testItem.get("name")
item_state = testItem.get("state")

print("Received item type " + item_type + " with name " + item_name + " and state " + item_state)Updating an itemUpdatinganItemhave two different meanings. In openHAB you can change anStateby usingsendCommandorpostUpdate. The functionupdateis an alias forsendCommandorpostUpdate. You can specifysendCommandorpostUpdateas parameters forupdate. If no parameter is specified,postUpdateis used automatically.So you can run as examplecrud.update("testItem", "Hello World")orcrud.update("testItem", "Hello World", "postUpdate")instead ofcrud.postUpdate("testItem", "Hello World")Or if you want to usesendCommandinopenHAByou can runcrud.update("testItem", "Lorem ipsum", "sendCommand")instead ofcrud.sendCommand("testItem", "Lorem ipsum")Note:postUpdateandsendCommandhave validation capabilities. Withtypeyou can set thetypeof theItem. This will call a function which checks that thevaluewhich should change theStateof theItemispersistent. However, this does not mean that you are checking the correctType, becauseopenHABmay have a differentTypethan the one you want to update. If you will set thevalidatevariable toTruetheItemis queried via theREST APIand theTypeis checked.For checking if you put a right value for theStateof the assumedTypeyou can run as examplecrud.sendCommand("testItem", "Lorem ipsum", "Switch")orcrud.postUpdate("testItem", "Hello World", "Switch")If you want to check if the actualTypeon openHAB is correct and you can set theStateto this value you can run:crud.sendCommand("testItem", "Lorem ipsum", None, True)orcrud.postUpdate("testItem", "Hello World", None, True)Another possibility is: name:str, value, type:str = None, validate:bool = None):crud.sendCommand("testItem", "Lorem ipsum", validate=True)orcrud.postUpdate("testItem", "Hello World", validate=True)Of course you can change the order as example to:crud.postUpdate(validate=True, type="Switch", name="testItem")There will be no response if it is correct!Deleting new itemsYou can only delete anItemif you have access to your localopenHABinstance. For security reasons, this is prohibited via the cloud.In this case you can run following:crud.delete("testItem")There will be no response if it is correct!Getting the state of an itemIn most cases you only want to query the currentStateof anItemand not get all informations of anItem. ThegetStatefunction can be used for this purpose:state = crud.getState("testItem")You get the state as a string:print(type(state))This is due to the data transmission viahttp. Numerical values must be parsed.openHABspecific values likeUP,DOWN,PLAY,PAUSE,ON,OFFetc. have no data type in Python, i.e. here a string (str) still makes sense. Here the capitalization must be considered!At least you can print theStatewith:print(state)Close sessionSince we may want to make several requests in a row, asessionis opened when the CRUDobjectis created. This can of course also be terminated at the end:crud.close()