platypush/platypush/entities/three_axis.py
Fabio Manganiello d872835093
New API to check if a table class exists before defining it.
- Check if it's part of the metadata through a function call rather than
  checking `Base.metadata` in every single module.

- Make it possible to override them (mostly for doc generation logic
  that needs to be able to import those classes).

- Make it possible to extend them.
2023-10-09 01:33:44 +02:00

54 lines
1.6 KiB
Python

from typing import Iterable, Mapping, Optional, Union
from sqlalchemy import Column, Integer, ForeignKey
from platypush.common.db import is_defined
from platypush.common.sensors import Numeric
from .sensors import RawSensor
if not is_defined('three_axis_sensor'):
class ThreeAxisSensor(RawSensor):
"""
An entity that measures a time duration.
"""
__tablename__ = 'three_axis_sensor'
id = Column(
Integer, ForeignKey(RawSensor.id, ondelete='CASCADE'), primary_key=True
)
__table_args__ = {'extend_existing': True}
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}
@RawSensor.value.setter
def value(
self, value: Optional[Union[Iterable[Numeric], Mapping[str, Numeric]]]
):
"""
Validates and normalizes the given value to a list of 3 numeric
values.
"""
if value is None:
return
if isinstance(value, dict):
assert set(value.keys()) == {
'x',
'y',
'z',
}, f'Invalid keys for entity of type {self.__class__}: "{value}"'
value = [value[k] for k in ['x', 'y', 'z']] # type: ignore
assert (
isinstance(value, (list, tuple))
and len(value) == 3 # type: ignore
and all(isinstance(v, (int, float)) for v in value)
), f'Invalid 3-axis value: {value}'
super(ThreeAxisSensor, type(self)).value.fset(self, value) # type: ignore