# Home Assistant Pyscript to Cleanup "orphaned entertainment areas" in the HUE brigde. # Those are created when powering cycling certain Philips HUE Ambilight TV's. # This script only removes groups where: # - name property is "Entertainment area" (default input value) # - class property is "TV" (default input value) # - Owner property is Null. (hard coded) # - Group type property is "entertainment" (hard coded) # Notes: # - Please review by using the "list groups service" if this filter is selective enough in your setup, # before calling the cleanup script (only needed if you more entertainment area's in use) # - Before you can use the "list" or "clean" services, execute the link bridge service one time. # This script creates a persisted entity 'pyscript.hue_base_url' containing the HUE bride IP and username for future use import aiohttp import json import socket state.persist('pyscript.hue_base_url', default_value="") @service(supports_response="only") def Hue_Link_Bridge(IPAddress=None): """yaml description: Link the HUE bridge. IP address and Username are persisted over restarts. Press bridge button before calling this script! fields: IPAddress: description: IP Address of the HUE bridge example: 192.168.178.10 default: 192.168.178.10 required: true selector: text: """ if (IPAddress==None): return {"result": "failed", "reason": "No HUE IP address provided" } body={"devicetype":"pyscript#"+socket.gethostname()} print(body) BaseURL="http://"+IPAddress+"/api" async with aiohttp.ClientSession() as session: async with session.post(BaseURL,json=body) as resp: if resp.status == 200: response = json.loads(resp.text()) if isinstance(response, list): if 'success' in response[0]: pyscript.hue_base_url = BaseURL+"/"+response[0]['success']['username'] return {"result": "success", "username": response[0]['success']['username'] } if 'error' in response[0]: return {"result": "failed", "error": response[0]['error']['description'] } return {"result": "failed", "returned": resp.status, "response": resp.text() } @service(supports_response="only") def Hue_List_Groups(): """yaml description: Retreive the full HUE groups listing (link with brigde 'one time' before using this script) """ if pyscript.hue_base_url == "": return { "result": "failed", "reason": "not Linked to bridge" } async with aiohttp.ClientSession() as session: async with session.get(pyscript.hue_base_url+"/groups") as resp: if resp.status == 200: groups = json.loads(resp.text()) if isinstance(groups, dict): return {"result": "success", "groups": groups } return { "result": "failed" } @service(supports_response="only") def Hue_List_Lights(): """yaml description: Retreive the full HUE lights listing (link with brigde 'one time' before using this script) """ if pyscript.hue_base_url == "": return { "result": "failed", "reason": "not Linked to bridge" } async with aiohttp.ClientSession() as session: async with session.get(pyscript.hue_base_url+"/lights") as resp: if resp.status == 200: lights = json.loads(resp.text()) if isinstance(lights, dict): return {"result": "success", "lights": lights } return { "result": "failed" } @service(supports_response="only") def Hue_Entertainment_Cleanup(ClassName="TV", GroupName="Entertainment area"): """yaml description: Cleanup orphaned HUE enterainment TV groups (link with brigde 'one time' before using this script) fields: GroupName: description: filter option on name property example: entertainment default: entertainment selector: text: ClassName: description: filter option on class property example: TV default: TV selector: text: """ deleted=[] failed=[] if pyscript.hue_base_url == "": return { "result": "failed", "reason": "not Linked to bridge" } async with aiohttp.ClientSession() as session: async with session.get(pyscript.hue_base_url+"/groups") as resp: if resp.status == 200: groups = json.loads(resp.text()) if isinstance(groups, dict): for groupNumber in groups: group = groups[groupNumber] if group['type'] == "Entertainment" and group['name'] == GroupName and group['class'] == ClassName and group['stream']['owner'] == None: async with session.delete(pyscript.hue_base_url+"/groups/"+groupNumber) as resp2: if resp2.status == 200: deleted.append(groupNumber) else: failed.append(groupNumber) return {"result": "success", "deleted": deleted, "failed": failed } return { "result": "failed", "reason": "HTTP error:"+str(resp.status) } return { "result": "failed", "reason": "unknown" }