Skip to main content

webthing

https://github.com/WebThingsIO/webthing-python/workflows/Python%20package/badge.svg https://img.shields.io/pypi/v/webthing.svg https://img.shields.io/badge/license-MPL--2.0-blue.svg

Implementation of an HTTP Web Thing. This library is compatible with Python 2.7 and 3.5+.

Installation

webthing can be installed via pip, as such:

$ pip install webthing

Running the Sample

$ wget https://raw.githubusercontent.com/WebThingsIO/webthing-python/master/example/single-thing.py
$ python3 single-thing.py

This starts a server and lets you search for it from your gateway through mDNS. To add it to your gateway, navigate to the Things page in the gateway’s UI and click the + icon at the bottom right. If both are on the same network, the example thing will automatically appear.

Example Implementation

In this code-walkthrough we will set up a dimmable light and a humidity sensor (both using fake data, of course). Both working examples can be found in the examples directory.

Dimmable Light

Imagine you have a dimmable light that you want to expose via the web of things API. The light can be turned on/off and the brightness can be set from 0% to 100%. Besides the name, description, and type, a Light is required to expose two properties:

  • on: the state of the light, whether it is turned on or off

    • Setting this property via a PUT {"on": true/false} call to the REST API toggles the light.

  • brightness: the brightness level of the light from 0-100%

    • Setting this property via a PUT call to the REST API sets the brightness level of this light.

First we create a new Thing:

light = Thing(
    'urn:dev:ops:my-lamp-1234',
    'My Lamp',
    ['OnOffSwitch', 'Light'],
    'A web connected lamp'
)

Now we can add the required properties.

The on property reports and sets the on/off state of the light. For this, we need to have a Value object which holds the actual state and also a method to turn the light on/off. For our purposes, we just want to log the new state if the light is switched on/off.

light.add_property(
    Property(
        light,
        'on',
        Value(True, lambda v: print('On-State is now', v)),
        metadata={
            '@type': 'OnOffProperty',
            'title': 'On/Off',
            'type': 'boolean',
            'description': 'Whether the lamp is turned on',
        }))

The brightness property reports the brightness level of the light and sets the level. Like before, instead of actually setting the level of a light, we just log the level.

light.add_property(
    Property(
        light,
        'brightness',
        Value(50, lambda v: print('Brightness is now', v)),
        metadata={
            '@type': 'BrightnessProperty',
            'title': 'Brightness',
            'type': 'number',
            'description': 'The level of light from 0-100',
            'minimum': 0,
            'maximum': 100,
            'unit': 'percent',
        }))

Now we can add our newly created thing to the server and start it:

# If adding more than one thing, use MultipleThings() with a name.
# In the single thing case, the thing's name will be broadcast.
server = WebThingServer(SingleThing(light), port=8888)

try:
    server.start()
except KeyboardInterrupt:
    server.stop()

This will start the server, making the light available via the WoT REST API and announcing it as a discoverable resource on your local network via mDNS.

Sensor

Let’s now also connect a humidity sensor to the server we set up for our light.

A MultiLevelSensor (a sensor that returns a level instead of just on/off) has one required property (besides the name, type, and optional description): level. We want to monitor this property and get notified if the value changes.

First we create a new Thing:

sensor = Thing(
    'urn:dev:ops:my-humidity-sensor-1234',
    'My Humidity Sensor',
     ['MultiLevelSensor'],
     'A web connected humidity sensor'
)

Then we create and add the appropriate property:

  • level: tells us what the sensor is actually reading

    • Contrary to the light, the value cannot be set via an API call, as it wouldn’t make much sense, to SET what a sensor is reading. Therefore, we are creating a readOnly property.

      level = Value(0.0);
      
      sensor.add_property(
          Property(
              sensor,
              'level',
              level,
              metadata={
                  '@type': 'LevelProperty',
                  'title': 'Humidity',
                  'type': 'number',
                  'description': 'The current humidity in %',
                  'minimum': 0,
                  'maximum': 100,
                  'unit': 'percent',
                  'readOnly': True,
              }))

Now we have a sensor that constantly reports 0%. To make it usable, we need a thread or some kind of input when the sensor has a new reading available. For this purpose we start a thread that queries the physical sensor every few seconds. For our purposes, it just calls a fake method.

self.sensor_update_task = \
    get_event_loop().create_task(self.update_level())

async def update_level(self):
    try:
        while True:
            await sleep(3)
            new_level = self.read_from_gpio()
            logging.debug('setting new humidity level: %s', new_level)
            self.level.notify_of_external_update(new_level)
    except CancelledError:
        pass

This will update our Value object with the sensor readings via the self.level.notify_of_external_update(read_from_gpio()) call. The Value object now notifies the property and the thing that the value has changed, which in turn notifies all websocket listeners.

Adding to Gateway

To add your web thing to the WebThings Gateway, install the “Web Thing” add-on and follow the instructions here.

Metadata

Release files for webthing 0.15.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for webthing 0.15.0
File Size Uploaded
webthing-0.15.0.tar.gz 21.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for webthing 0.15.0
File Interpreter ABI Platform
webthing-0.15.0-py3-none-any.whl Python 3 none any Details

Total release size: 43.2 kB

Release files / webthing-0.15.0.tar.gz

Download URL webthing-0.15.0.tar.gz
Size 21.7 kB
Tags Source
SHA-256 checksum
How to use checksums
fbbe53e4a76f9e3aec75e8d816a217097c77a29c3643472975b05b3eb3158055
BLAKE2b-256 checksum
How to use checksums
c0b88c9d29c0bc1402134e95503fdca1bb05cdac57dcb2f049831905767d65de
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.55.1 CPython/3.9.1

Release files / webthing-0.15.0-py3-none-any.whl

Download URL webthing-0.15.0-py3-none-any.whl
Size 21.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8331d621c95177b27fe07753a6952f8c80fe566b369a5f9d54accdd9f000bc8b
BLAKE2b-256 checksum
How to use checksums
d697abb0608f88b36b12f58bc4cc98f7a88f9bbc7aae46649371c758526772df
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.55.1 CPython/3.9.1

Release history Release notifications | RSS feed

This release

0.15.0 This release

2 release files

0.14.0

2 release files

0.13.2

2 release files

0.13.1

2 release files

0.12.2

2 release files

0.12.1

2 release files

0.12.0

2 release files

0.11.3

2 release files

0.11.2

2 release files

0.11.1

2 release files

0.11.0

2 release files

0.10.0

2 release files

0.9.2

2 release files

0.9.1

2 release files

0.9.0

2 release files

0.8.1

2 release files

0.8.0

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.4

2 release files

0.5.3

2 release files

0.5.2

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.1

3 release files

0.4.0

3 release files

0.3.0

3 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page