123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- #!/usr/bin/env python3
- from typing import Dict
- from datetime import datetime
-
- from .variable_type import VariableType
- from .variable import Variable
- from .variable_value import VariableValue
-
- from pyheatpump.logger import logger_init
- logger = logger_init()
-
- class Heatpump:
- mac_address: str
- var_types: Dict
- last_update: int = None
-
- def __init__(self, mac_address, last_update):
- self.mac_address = mac_address
- self.last_update = last_update
- self.var_types = VariableType.getall()
-
- def __str__(self):
- return str(self.__dict__())
-
- def __dict__(self):
- r = {'macAddress': self.macformat}
- if self.last_update is None:
- return { **r, **self.get_var_values() }
- return { **r, **self.get_var_values_since() }
-
- @property
- def macformat(self):
- return ''.join(self.mac_address.split(':')).lower()
-
- def get_var_values(self):
- r = {}
- for label, var_type in self.var_types.items():
- r[label] = var_type.get_variables_values()
- return r
-
- def get_var_values_since(self):
- r = {}
- for label, var_type in self.var_types.items():
- r[label] = var_type.get_variables_values_since(self.last_update)
- return r
-
- def control(self, data):
- logger.warn('Received orders data :')
- logger.warn(data)
- if 'macAddress' not in data.keys():
- logger.info(f'No orders')
- return True
-
- if data['macAddress'] != self.macformat:
- logger.warn(f'Orders are not for the current heatpump ({self.mac_address} != {data["macAddress"]})')
- raise Exception('Wrong mac_address')
-
- for label, var_type in self.var_types.items():
- if var_type.label not in data.keys():
- continue
-
- var_type.control(data[var_type.label])
- return True
|