# Webthing-python documentation of events as examples

**URL:** <https://discourse.mozilla.org/t/webthing-python-documentation-of-events-as-examples/60078>\
**Category:** WebThings\
**Created:** [May 15, 2020, 11:19am UTC](https://discourse.mozilla.org/t/webthing-python-documentation-of-events-as-examples/60078 "2020-05-15T11:19:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Novski](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/novski/32/4829_2.png) [@Novski](https://discourse.mozilla.org/u/Novski)\
**Post date:** [May 15, 2020, 11:19am UTC](https://discourse.mozilla.org/t/webthing-python-documentation-of-events-as-examples/60078/1 "2020-05-15T11:19:25Z")

</div>

Let’s start a list of code examples.  
If you have an example and give me a link or a snippet I will add it to documentation.  
Let me know…

---

<div class="post-metadata">

**Author:** ![Novski](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/novski/32/4829_2.png) [@Novski](https://discourse.mozilla.org/u/Novski)\
**Post date:** [May 15, 2020, 11:33am UTC](https://discourse.mozilla.org/t/webthing-python-documentation-of-events-as-examples/60078/2 "2020-05-15T11:33:25Z")

</div>

@mstegeman once shared me this example of an event:  
from this resource:  
[https://github.com/mrstegeman/mpd-webthing/blob/master/python/mpd-webthing.py](https://github.com/mrstegeman/mpd-webthing/blob/master/python/mpd-webthing.py)

L293:

```auto
        # Add a 'playlistUpdated' event.
        self.add_available_event(
            'playlistUpdated',
            {'description': 'The current playlist has been updated',
             'type': 'string'})

```

L97:

```auto
class PlaylistUpdatedEvent(Event):
    """Event to indicate that the current playlist was updated."""

    def __init__ (self, thing, data):
        """Initialize the event."""
        Event. __init__ (self, thing, 'playlistUpdated', data=data)

```

It gets triggerd once a Playlist is activated (L548) and eatch time that Playlist adds a random track. on Line 94 from an Action:

```auto
# Since we just updated the playlist, emit an event.
playlist = self.thing.get_playlist()
if playlist is not None:
    self.thing.add_event(
    PlaylistUpdatedEvent(self.thing, playlist))

```

L548 from inside it `self.`:

```auto
# The current playlist was updated, so emit an event.
playlist = self.get_playlist()
if playlist is not None:
    self.add_event(
    PlaylistUpdatedEvent(self, playlist))

```
