def calculate_light_brightness(self, current_time = None, light_policy = None):
self.__logger.debug('Calculating light brightness..')
if current_time is None:
current_time = datetime.now().replace(tzinfo = tz.tzlocal())
calculated_light_brigtness = []
compiled_policy = self.__compiled_policy
if light_policy is not None:
compiled_policy = self.__compile_policy(light_policy, current_time)
if compiled_policy is None:
self.__logger.error("No policy is found. Skip light update")
return calculated_light_brigtness
for bulb in compiled_policy:
calculated_light = { "bulb_ip" : bulb["bulb_ip"] }
if 'light_on_only_when_device_online' in bulb and not self.__at_least_one_device_online(bulb["light_on_only_when_device_online"]):
# if required devices are not online, turn off the light
calculated_light["calculated_brightness"] = 0
else:
for policy in bulb['policies']:
brightness = self.__calculate_light_brightness(current_time, policy)
if brightness > -1:
calculated_light["calculated_brightness"] = brightness
calculated_light["policy_matched"] = policy
break
if 'calculated_brightness' in calculated_light:
calculated_light_brigtness.append(calculated_light)
self.__logger.debug('Calculated light brightness: %s', calculated_light_brigtness)
return calculated_light_brigtness
评论列表
文章目录