12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- #!/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, types=[]):
- self.mac_address = mac_address
- self.last_update = last_update
- if len(types):
- self.var_types = {
- key: val
- for key,val in VariableType.getall().items()
- if key in types
- }
- else:
- 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
|