pyg90alarm.dataclass.load_save
Base class for loading/saving dataclasses to a device.
Functions
|
Helper function to create a dataclass field with ReadOnlyIfNotProvided descriptor. |
Classes
Defines strategy for loading dataclass-backed panel entity. |
|
Base class for loading/saving dataclasses to a device. |
|
Loads entity from the device once per |
|
|
Metadata keys for DataclassLoadSave fields. |
Always loads entity from the panel. |
|
|
Descriptor for dataclass fields to be read-only if not provided during initialization. |
|
Reuses loaded entity for a given parent within a TTL. |
Exceptions
Raised when assigning to a |
- class pyg90alarm.dataclass.load_save.DataclassLoadPolicy
Bases:
objectDefines strategy for loading dataclass-backed panel entity.
- class pyg90alarm.dataclass.load_save.NoCacheDataclassLoadPolicy
Bases:
DataclassLoadPolicyAlways loads entity from the panel.
- class pyg90alarm.dataclass.load_save.TtlDataclassLoadPolicy(ttl_seconds)
Bases:
DataclassLoadPolicyReuses loaded entity for a given parent within a TTL.
- class pyg90alarm.dataclass.load_save.LoadOnceDataclassLoadPolicy
Bases:
DataclassLoadPolicyLoads entity from the device once per
G90Alarminstance.Subsequent
loadcalls return the same in-memory object untilforce=True(or the parent alarm instance is garbage-collected).
- class pyg90alarm.dataclass.load_save.Metadata
Bases:
objectMetadata keys for DataclassLoadSave fields.
- exception pyg90alarm.dataclass.load_save.ReadOnlyIfNotProvidedError
Bases:
ValueErrorRaised when assigning to a
ReadOnlyIfNotProvidedfield that was omitted during instance construction.- add_note(note, /)
Add a note to the exception
- with_traceback(tb, /)
Set self.__traceback__ to tb and return self.
- class pyg90alarm.dataclass.load_save.ReadOnlyIfNotProvided(default=<pyg90alarm.dataclass.validation._DefaultNotSet object>, trust_initial_value=False)
Bases:
ValidatorBase[T]Descriptor for dataclass fields to be read-only if not provided during initialization.
The field can be read, but attempts to modify it will raise
ReadOnlyIfNotProvidedError(aValueErrorsubclass) if the field was not provided during initialization. In other words, the only way to set the value is during object creation.Example usage:
@dataclass class Example:
- read_only_field: Optional[int] = field_readonly_if_not_provided(
default=None
)
# Works ok ex = Example(read_only_field=42) print(ex.read_only_field) # Outputs: 42 ex.read_only_field = 100 # Works ok
# Raises ReadOnlyIfNotProvidedError ex2 = Example() print(ex2.read_only_field) # Outputs: None ex2.read_only_field = 100 # Raises ReadOnlyIfNotProvidedError
- Parameters:
default (
TypeVar(T) |_DefaultNotSet) – Default value to return upon read if not provided during initialization.
- pyg90alarm.dataclass.load_save.field_readonly_if_not_provided(*args, default=None, **kwargs)
Helper function to create a dataclass field with ReadOnlyIfNotProvided descriptor.
- Parameters:
- Return type:
TypeVar(T)- Returns:
A dataclass field with ReadOnlyIfNotProvided descriptor attached.
- class pyg90alarm.dataclass.load_save.DataclassLoadSave
Bases:
objectBase class for loading/saving dataclasses to a device.
- There are multiple ways to implement the functionality:
Encapsulate the dataclass inside another class that handles loading/saving and exposes dataclass fields as properties. The latter part gets complex as properties need to be asynchronous, as well as added dynamically at runtime to improve maintainability.
Inherit from this class, which provides load and save methods on top of standard dataclasses. This is believed to be more concise and easier to understand.
Implementing classes must define LOAD_COMMAND and SAVE_COMMAND class variables to specify which commands to use for loading and saving data.
Example usage:
@dataclass class G90ExampleConfig(DataclassLoadSave):
LOAD_COMMAND = G90Commands.GETEXAMPLECONFIG SAVE_COMMAND = G90Commands.SETEXAMPLECONFIG field1: int field2: str
# Loading data config = await G90ExampleConfig.load(G90_alarm_instance) print(config.field1, config.field2)
# Modifying and saving data config.field1 = 42 await config.save()
- serialize()
Returns the dataclass fields as a list.
Handles specific metadata for the fields. :seealso:`Metadata`.
- async save()
Save the current data to the device.
Refreshes the load policy cache (if any): the initial
load(..., force=True)repopulates the policy’s entry for this parent with the newly loaded instance used for read-modify-write.- Return type:
- async classmethod load(parent, force=False)
Create an instance with values loaded from the device.