labthings_fastapi.descriptors.property
Define an object to represent an Action, as a descriptor.
Module Contents
Classes
A property that can be accessed via the HTTP API |
|
A setting can be accessed via the HTTP API and is persistent between sessions |
API
- class labthings_fastapi.descriptors.property.ThingProperty(model: type, initial_value: Any = None, readonly: bool = False, observable: bool = False, description: Optional[str] = None, title: Optional[str] = None, getter: Optional[Callable] = None, setter: Optional[Callable] = None)
A property that can be accessed via the HTTP API
By default, a ThingProperty is “dumb”, i.e. it acts just like a normal variable.
- model: type[pydantic.BaseModel]
None
- __init__(model: type, initial_value: Any = None, readonly: bool = False, observable: bool = False, description: Optional[str] = None, title: Optional[str] = None, getter: Optional[Callable] = None, setter: Optional[Callable] = None)
- property title
A human-readable title
- property description
A description of the property
- __get__(obj, type=None) Any
The value of the property
If
objis none (i.e. we are getting the attribute of the class), we return the descriptor.If no getter is set, we’ll return either the initial value, or the value from the object’s dict, i.e. we behave like a variable.
If a getter is set, we will use it, unless the property is observable, at which point the getter is only ever used once, to set the initial value.
- __set__(obj, value)
Set the property’s value
- _observers_set(obj)
A set used to notify changes
- emit_changed_event(obj: labthings_fastapi.thing.Thing, value: Any) None
Notify subscribers that the property has changed
This function is run when properties are upadated. It must be run from within a thread. This could be the
Invocationthread of a running action, or the property should be updated over via a client/http. It must be run from a thread as it is communicating with the event loop via anasyncioblocking portal.- Raises:
NotConnectedToServerError – if the Thing that is calling the property update is not connected to a server with a running event loop.
- async emit_changed_event_async(obj: labthings_fastapi.thing.Thing, value: Any)
Notify subscribers that the property has changed
- property name
The name of the property
- add_to_fastapi(app: fastapi.FastAPI, thing: labthings_fastapi.thing.Thing)
Add this action to a FastAPI app, bound to a particular Thing.
- property_affordance(thing: labthings_fastapi.thing.Thing, path: Optional[str] = None) labthings_fastapi.thing_description.model.PropertyAffordance
Represent the property in a Thing Description.
- class labthings_fastapi.descriptors.property.ThingSetting(model: type, initial_value: Any = None, readonly: bool = False, observable: bool = False, description: Optional[str] = None, title: Optional[str] = None, getter: Optional[Callable] = None, setter: Optional[Callable] = None)
Bases:
labthings_fastapi.descriptors.property.ThingPropertyA setting can be accessed via the HTTP API and is persistent between sessions
A ThingSetting is a ThingProperty with extra functionality for triggering a Thing to save its settings.
Note: If a setting is mutated rather than assigned to, this will not trigger saving. For example: if a Thing has a setting called
dictsettingholding the dictionary{"a": 1, "b": 2}thenself.dictsetting = {"a": 2, "b": 2}would trigger saving butself.dictsetting[a] = 2would not, as the setter fordictsettingis never called.The setting otherwise acts just like a normal variable.
- __set__(obj, value)
Set the property’s value
- set_without_emit(obj, value)
Set the property’s value, but do not emit event to notify the server
This function is not expected to be used externally. It is called during initial setup so that the setting can be set from disk before the server is fully started.