Projet de remplacement du "RPiPasserelle" d'Otec.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

heatpump.py 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/env python3
  2. from typing import Dict
  3. from datetime import datetime
  4. from .variable_type import VariableType
  5. from .variable import Variable
  6. from .variable_value import VariableValue
  7. from pyheatpump.logger import logger_init
  8. logger = logger_init()
  9. class Heatpump:
  10. mac_address: str
  11. var_types: Dict
  12. last_update: int = None
  13. def __init__(self, mac_address, last_update):
  14. self.mac_address = mac_address
  15. self.last_update = last_update
  16. self.var_types = VariableType.getall()
  17. def __str__(self):
  18. return str(self.__dict__())
  19. def __dict__(self):
  20. r = {'macAddress': self.macformat}
  21. if self.last_update is None:
  22. return { **r, **self.get_var_values() }
  23. return { **r, **self.get_var_values_since() }
  24. @property
  25. def macformat(self):
  26. return ''.join(self.mac_address.split(':')).lower()
  27. def get_var_values(self):
  28. r = {}
  29. for label, var_type in self.var_types.items():
  30. r[label] = var_type.get_variables_values()
  31. return r
  32. def get_var_values_since(self):
  33. r = {}
  34. for label, var_type in self.var_types.items():
  35. r[label] = var_type.get_variables_values_since(self.last_update)
  36. return r
  37. def control(self, data):
  38. logger.warn('Received orders data :')
  39. logger.warn(data)
  40. if 'macAddress' not in data.keys():
  41. logger.info(f'No orders')
  42. return True
  43. if data['macAddress'] != self.macformat:
  44. logger.warn(f'Orders are not for the current heatpump ({self.mac_address} != {data["macAddress"]})')
  45. raise Exception('Wrong mac_address')
  46. for label, var_type in self.var_types.items():
  47. if var_type.label not in data.keys():
  48. continue
  49. var_type.control(data[var_type.label])
  50. return True