update light icon based on status
This commit is contained in:
@@ -86,8 +86,11 @@ in
|
|||||||
|
|
||||||
"custom/lights" = {
|
"custom/lights" = {
|
||||||
tooltip = false;
|
tooltip = false;
|
||||||
format = "";
|
exec = "waybar-hass --get_light light.living_room_lights";
|
||||||
|
interval = "once";
|
||||||
|
format = "{text}";#"";
|
||||||
on-click = "waybar-hass --toggle_light light.living_room_lights";
|
on-click = "waybar-hass --toggle_light light.living_room_lights";
|
||||||
|
return-type = "json";
|
||||||
};
|
};
|
||||||
|
|
||||||
temperature = {
|
temperature = {
|
||||||
|
|||||||
@@ -42,25 +42,37 @@ let
|
|||||||
|
|
||||||
waybar-hass = pkgs.writeScriptBin "waybar-hass" ''
|
waybar-hass = pkgs.writeScriptBin "waybar-hass" ''
|
||||||
#!${pythonEnv}/bin/python
|
#!${pythonEnv}/bin/python
|
||||||
|
"""run with the special python"""
|
||||||
import os
|
|
||||||
import argparse
|
import argparse
|
||||||
import time
|
import time
|
||||||
from homeassistant_api import Client
|
import json
|
||||||
|
from homeassistant_api import WebsocketClient
|
||||||
|
|
||||||
hass_url = 'http://homeassistant.local:8123/api'
|
HASS_URL = 'ws://homeassistant.local:8123/api/websocket'
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(prog='hass python wrapper')
|
parser = argparse.ArgumentParser(prog='hass python wrapper')
|
||||||
parser.add_argument('--toggle_light')
|
parser.add_argument('--toggle_light')
|
||||||
|
parser.add_argument('--get_light')
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
def loadKey():
|
def load_key():
|
||||||
|
"""Read the api key"""
|
||||||
token_path = "/run/secrets/desktop/hass_token"
|
token_path = "/run/secrets/desktop/hass_token"
|
||||||
with open(token_path, "r") as key_file:
|
with open(token_path, "r") as key_file:
|
||||||
key = key_file.readline()
|
key = key_file.readline()
|
||||||
return key
|
return key
|
||||||
|
|
||||||
|
def get_light_state(client, light):
|
||||||
|
"""Get light status"""
|
||||||
|
light_entity = client.get_entity(entity_id=light)
|
||||||
|
state = light_entity.get_state()
|
||||||
|
if state.state == 'on':
|
||||||
|
return ""
|
||||||
|
return ""
|
||||||
|
|
||||||
def toggle_light(client, light):
|
def toggle_light(client, light):
|
||||||
|
"""Toggle light status"""
|
||||||
lights = client.get_domain("light")
|
lights = client.get_domain("light")
|
||||||
lights.toggle(entity_id=light)
|
lights.toggle(entity_id=light)
|
||||||
time.sleep(0.5)
|
time.sleep(0.5)
|
||||||
@@ -68,19 +80,25 @@ let
|
|||||||
state = light_entity.get_state()
|
state = light_entity.get_state()
|
||||||
if state.state == 'on':
|
if state.state == 'on':
|
||||||
return ""
|
return ""
|
||||||
else:
|
return ""
|
||||||
return ""
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
token = loadKey()
|
"""Main"""
|
||||||
|
token = load_key()
|
||||||
|
status = "err"
|
||||||
|
|
||||||
with Client(hass_url, token) as client:
|
with WebsocketClient(HASS_URL, token) as client:
|
||||||
|
# toggle a light
|
||||||
if args.toggle_light is not None:
|
if args.toggle_light is not None:
|
||||||
status = toggle_light(client=client, light=args.toggle_light)
|
status = toggle_light(client=client, light=args.toggle_light)
|
||||||
|
# get the current light status
|
||||||
print("{ text: \"" + status + "\" }")
|
if args.get_light is not None:
|
||||||
|
status = get_light_state(client=client, light=args.get_light)
|
||||||
|
|
||||||
|
print(json.dumps({ "text": status }))
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
|
||||||
'';
|
'';
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user