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 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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, types=[]):
  14. self.mac_address = mac_address
  15. self.last_update = last_update
  16. if len(types):
  17. self.var_types = {
  18. key: val
  19. for key,val in VariableType.getall().items()
  20. if key in types
  21. }
  22. else:
  23. self.var_types = VariableType.getall()
  24. def __str__(self):
  25. return str(self.__dict__())
  26. def __dict__(self):
  27. r = {'macAddress': self.macformat}
  28. if self.last_update is None:
  29. return { **r, **self.get_var_values() }
  30. return { **r, **self.get_var_values_since() }
  31. @property
  32. def macformat(self):
  33. return ''.join(self.mac_address.split(':')).lower()
  34. def get_var_values(self):
  35. r = {}
  36. for label, var_type in self.var_types.items():
  37. r[label] = var_type.get_variables_values()
  38. return r
  39. def get_var_values_since(self):
  40. r = {}
  41. for label, var_type in self.var_types.items():
  42. r[label] = var_type.get_variables_values_since(self.last_update)
  43. return r
  44. def control(self, data):
  45. logger.warn('Received orders data :')
  46. logger.warn(data)
  47. if 'macAddress' not in data.keys():
  48. logger.info(f'No orders')
  49. return True
  50. if data['macAddress'] != self.macformat:
  51. logger.warn(f'Orders are not for the current heatpump ({self.mac_address} != {data["macAddress"]})')
  52. raise Exception('Wrong mac_address')
  53. for label, var_type in self.var_types.items():
  54. if var_type.label not in data.keys():
  55. continue
  56. var_type.control(data[var_type.label])
  57. return True