from pyheatpump.db import RowClass from pyheatpump.db import sql from datetime import datetime from pprint import pprint class VariableValue(RowClass): type: str = None address: int = None time: datetime = None value: int = None def __init__(self, **kwargs): super().__init__(**kwargs) @staticmethod def get(type, address, time=datetime.now()): try: row = next(sql( """ SELECT * FROM var_value WHERE type = '{}' AND address = {} AND time <= {} ORDER BY time DESC LIMIT 1 """.format( type, address, int(time.strftime('%s'))+1) )) return VariableValue(**dict(row)) except StopIteration as e: raise e