123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- #!/usr/bin/env python3
- from typing import Dict
-
- from .variable_type import VariableType
-
- 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=0, types=None):
- self.mac_address = mac_address
- self.last_update = last_update
- if types is not None:
- 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):
- res = f'{self.mac_address} ['
- res += ', '.join([
- var_type.slabel
- for var_type in self.var_types
- ])
- res += ']'
-
- @property
- def packet(self):
- """
- Returns an object containing all values and the current macAddress
- """
- r = {'macAddress': self.macformat}
- if not self.last_update:
- # Since beginning
- 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):
- return {
- var_type.label: var_type.get_variables_values()
- for var_type in self.var_types.values()
- }
-
- 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.warning('Received orders data :')
- logger.warning(data)
- if 'macAddress' not in data.keys():
- logger.info('No orders')
- return True
-
- if data['macAddress'] != self.macformat:
- logger.warning('Orders are not for the current heatpump (%s != %s)',
- self.mac_address, data['macAddress'])
- raise Exception('Wrong mac_address')
-
- for _, 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
|