Tutorial

Adding an unobservable property

Adding an unobservable property for the door state.

Unlike the observable property we implemented in the previous section, the following unobservable property does not provide a stream of constantly updating values but only returns the door_open once. This might be useful in some use cases, however here it is mostly shown to highlight the difference between an observable and an unobservable property.

Creating the base method

We return to the door_controller_base.py file, where we previously defined the observable property. We now add the unobservable property using the @sila.UnobservableProperty() decorator:

door_controller_base.py
@abc.abstractmethod
@sila.UnobservableProperty()
async def get_door_open_once(self) -> bool:
    """
    Retrieve the door state once.
    """

Code explanation

  • @sila.UnobservableProperty(): "An unobservable property is a property that can be read at any time, but no subscription mechanism is provided to observe its changes."
  • async def get_door_open_once(self) -> bool: the only noteworthy difference to the observable subscription method is that only a boolean value is returned instead of a sila.Stream[bool].

Implementation of the door_open_once property

As before, we have to write the actual functionality of the method in the door_controller.py file:

async def get_door_open_once(self):
    return self._door_open

Code explanation

In this concise implementation, note that we use the return keyword instead of yield. We access the _door_open property of the class itself (self) and return its value.

Testing

As before, it is advised to test the code after every implementation. For this, we restart the SiLA Browser and the connector and try to see if we can execute the get_door_open_once method. Again, we expect a return value of "No" in the SiLA Browser.

How the unobservable property should look like in the sila browser


Copyright © 2024