def __build_devices(self, devices): sensors = [] heaters = [] water_heaters = [] for device in devices: device_class = DeviceType.from_str(device["widget"]) if device_class in [DeviceType.HEATER, DeviceType.HEATER_PASV]: heaters.append(device) if device_class == DeviceType.WATER_HEATER: water_heaters.append(device) else: sensors.append(device) for heater in heaters: place = self.__find_place(heater) heater_url = self.__extract_id(heater["deviceURL"]) heater_sensors = self.__link_sensors(sensors, place, heater_url) heater = CozytouchDevice.build(heater, self.client, place, sensors=heater_sensors) self.heaters.append(heater) for water_heater in water_heaters: place = self.__find_place(water_heater) water_heater_url = self.__extract_id(water_heater["deviceURL"]) wh_sensors = self.__link_sensors(sensors, place, water_heater_url) water_heater = CozytouchDevice.build(water_heater, self.client, place, sensors=wh_sensors) self.water_heaters.append(water_heater)
def __link_sensors(self, sensors, place, device_url): device_sensors = [] for sensor in sensors: if self.__extract_id(sensor["deviceURL"]) == device_url: device_sensors.append( CozytouchDevice.build(sensor, self.client, place)) return device_sensors
def __build_devices(self, devices): sensors = [] heaters = [] for device in devices: if device["widget"] in [ DeviceType.HEATER.value, DeviceType.HEATER_PASV.value ]: heaters.append(device) else: sensors.append(device) def extract_id(url): if '#' not in url: return url return url[0:url.index("#")] for heater in heaters: place: CozytouchPlace = self.__find_place(heater["placeOID"]) if place is None: raise CozytouchException("Place %s not found" % heater["placeOID"]) heater_url = extract_id(heater["deviceURL"]) heater_sensors = [ CozytouchDevice.build(sensor, self.client, place) for sensor in sensors if extract_id(sensor["deviceURL"]) == heater_url ] heater = CozytouchHeater(heater) heater.sensors = heater_sensors heater.place = place heater.client = self.client self.heaters.append(heater)
def __build_devices(self, devices): sensors = [] heaters = [] water_heaters = [] for device in devices: device_class = DeviceType.from_str(device["widget"]) if device_class in [DeviceType.HEATER, DeviceType.HEATER_PASV]: heaters.append(device) if device_class in [DeviceType.WATER_HEATER]: water_heaters.append(device) else: sensors.append(device) def extract_id(url): if '#' not in url: return url return url[0:url.index("#")] def link_sensors(sensors, place, device_url): device_sensors = [] for sensor in sensors: if extract_id(sensor["deviceURL"]) == device_url: device_sensors.append(CozytouchDevice.build(sensor, self.client, place)) return device_sensors def find_place(device): place: CozytouchPlace = self.__find_place(device["placeOID"]) if place is None: raise CozytouchException( "Place {name} not found".format(name=device["placeOID"]) ) return place for heater in heaters: place = find_place(heater) heater_url = extract_id(heater["deviceURL"]) heater_sensors = link_sensors(sensors, place, heater_url) heater = CozytouchDevice.build(heater, self.client, place, sensors=heater_sensors) self.heaters.append(heater) for water_heater in water_heaters: place = find_place(water_heater) water_heater_url = extract_id(water_heater["deviceURL"]) wh_sensors = link_sensors(sensors, place, water_heater_url) water_heater = CozytouchDevice.build(water_heater, self.client, place, sensors=wh_sensors) self.water_heaters.append(water_heater)
def __build_devices(self, data): for device in data: self.devices.append(CozytouchDevice.build(device, self))