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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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=0, 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 not self.last_update:
  29. # Since beginning
  30. return { **r, **self.get_var_values() }
  31. return { **r, **self.get_var_values_since() }
  32. @property
  33. def macformat(self):
  34. return ''.join(self.mac_address.split(':')).lower()
  35. def get_var_values(self):
  36. r = {}
  37. for label, var_type in self.var_types.items():
  38. r[label] = var_type.get_variables_values()
  39. return r
  40. def get_var_values_since(self):
  41. r = {}
  42. for label, var_type in self.var_types.items():
  43. r[label] = var_type.get_variables_values_since(self.last_update)
  44. return r
  45. def control(self, data):
  46. logger.warning('Received orders data :')
  47. logger.warning(data)
  48. if 'macAddress' not in data.keys():
  49. logger.info(f'No orders')
  50. return True
  51. if data['macAddress'] != self.macformat:
  52. logger.warn(f'Orders are not for the current heatpump ({self.mac_address} != {data["macAddress"]})')
  53. raise Exception('Wrong mac_address')
  54. for label, var_type in self.var_types.items():
  55. if var_type.label not in data.keys():
  56. continue
  57. var_type.control(data[var_type.label])
  58. return True