From bcfa019b2e8ae3ba519d5222b037e791baa8df4d Mon Sep 17 00:00:00 2001 From: Robert Date: Mon, 17 Jul 2023 16:31:05 -0400 Subject: [PATCH] Update __init__.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit • Add support to iterate through available CL switches to achieve a more accurate brightness value • Add support to parse HA logs to find brightness values from CL debug logging if the above is not available • Fallback to a very poor method of calculating brightness from retrieved colortemp values --- .../hue_circadian_lighting/__init__.py | 84 +++++++++++++++++-- 1 file changed, 75 insertions(+), 9 deletions(-) diff --git a/custom_components/hue_circadian_lighting/__init__.py b/custom_components/hue_circadian_lighting/__init__.py index d5dfd76..17882b1 100755 --- a/custom_components/hue_circadian_lighting/__init__.py +++ b/custom_components/hue_circadian_lighting/__init__.py @@ -13,6 +13,8 @@ DOMAIN = 'circadian_lighting_bridge' BRIDGE_DATA_KEY = "circadian_lighting_bridge_bridge" +ENTITY_DOMAIN = "switch" +ENTITY_PREFIX = "circadian_lighting" async def async_setup(hass, config): @@ -78,21 +80,69 @@ async def update_scene_lights(session, hue_gateway, key, scene, brightness, xy, _LOGGER.error(f"light id: {val} body {body} status code: {r_response.status}") + async def update_hue_scenes(hass, new_state): try: bridges = get_hue_gateway_and_key() async with ClientSession() as session: + await asyncio.sleep(10) tasks = [] + switches = [ + entity_id + for entity_id in hass.states.async_entity_ids("switch") + if entity_id.startswith("switch.circadian_lighting") + ] + + brightness = None + + for switch in switches: + switch_state = hass.states.get(switch) + brightness = switch_state.attributes.get('brightness') + if brightness is not None: + _LOGGER.info( + "Found switch '%s' with brightness: %s", + switch, + brightness, + ) + break + else: + _LOGGER.info("No switch found with brightness.") + + if brightness is None: + if new_state and new_state.attributes.get("brightness"): + log_message = str(new_state) + brightness_match = re.search(r"brightness=(\d+)", log_message, re.IGNORECASE) + if brightness_match: + brightness = int(brightness_match.group(1)) + _LOGGER.info( + "Extracted brightness from log message: %s", + brightness, + ) + else: + brightness = new_state.attributes.get("brightness") + _LOGGER.info( + "Extracted brightness from state attributes: %s", + brightness, + ) + else: + _LOGGER.info("No switch found with brightness and no valid brightness value extracted.") + + if brightness is None or brightness == "Unknown": + brightness = get_brightness(hass, new_state) + _LOGGER.info( + "Using get_brightness function. Calculated brightness: %s", + brightness, + ) + + xy = get_xy_color(hass, new_state) + mired = get_colortemp(hass, new_state) + for bridge_ip, bridge_username in bridges: hue_gateway = bridge_ip key = bridge_username - brightness = get_brightness(hass, new_state) - xy = get_xy_color(hass, new_state) - mired = get_colortemp(hass, new_state) - url = f"http://{hue_gateway}/api/{key}/scenes" async with session.get(url) as response: r = await response.json() @@ -103,14 +153,31 @@ async def update_hue_scenes(hass, new_state): scenes.append(val) for val in scenes: - tasks.append(update_scene_lights(session, hue_gateway, key, val, brightness, xy, mired)) + _LOGGER.info( + "Updating scene '%s' with brightness: %s, xy: %s, colortemp: %s", + val, + brightness if brightness is not None else "N/A", + xy, + mired, + ) + + tasks.append( + update_scene_lights( + session, + hue_gateway, + key, + val, + brightness, + xy, + mired, + ) + ) await asyncio.gather(*tasks) except Exception as e: raise e - def get_colortemp(hass, new_state): entity_id = 'sensor.circadian_values' state = hass.states.get(entity_id) @@ -121,7 +188,6 @@ def get_colortemp(hass, new_state): if colortemp_kelvin is None: raise ValueError(f'colortemp attribute not found for entity {entity_id}') - # Convert Kelvin to Mireds (Philips Hue uses Mireds) colortemp_mireds = int(round(1000000 / colortemp_kelvin)) return colortemp_mireds @@ -198,7 +264,7 @@ async def retry_connect(): "Retrying connection to the Philips Hue bridge at %s...", bridge_ip, ) - await asyncio.sleep(5) # Wait for 5 seconds before retrying + await asyncio.sleep(5) return False if not await retry_connect(): @@ -256,4 +322,4 @@ async def async_unload_entry(hass, config_entry): bridge = hass.data[DOMAIN].pop(BRIDGE_DATA_KEY, None) if bridge is not None: await bridge.close() - return True \ No newline at end of file + return True