So here is my complete solution wich works flawlessly for now on rpi 4b+ running latest raspbian os (debian bookworm).
- to autostart chromium kiosk and hass-screenapi (python)
Edit user autostart file ânano .config/lxsession/LXDE-pi/autostartâ to this:
Note, when uncommenting (remove #) âlxpanelâ and âpcmanfmâ line, you can use the rpi desktop to set things up.
#################################################
# LXDE-pi autostart script #
# #
# This file must be in the user's home e.g. #
# /home/pi/.config/lxsession/LXDE-pi/autostart. #
#################################################
##enable/disable this for desktop
#@lxpanel --profile LXDE-pi
#@pcmanfm --desktop --profile LXDE-pi
## enable/disable screen saver
#@xscreensaver -no-splash # comment this line out to disable screensaver
# Set the current xsession not to blank out the screensaver and then disables the screensaver altogether.
@xset s noblank
@xset s off
# set DPMS timeouts (Standby Suspend Off)
@xset dpms 43200 43200 43200
# disables the display power management system (we dont want this because we use DPMS to switch on/off the display)
#@xset -dpms
# Run the wanted app
@bash /home/pi/kiosk.sh
- create/edit kiosk bash script file ânano kiosk.shâ to this:
#!/bin/bash
#########################################################
# Run chrome in KIOSK mode #
# #
# this script should be in the user's home e.g. #
# /home/pi/kiosk.sh #
# and should have exec right #
# $ chmod 750 kiosk.sh #
# #
# This script should be executed by the XLDE autostart #
# /home/pi/.config/lxsession/LXDE-pi/autostart #
#########################################################
### Use unclutter to hide the mouse
# unclutter -idle 0.5 -root &
### Use xdotool to simulate keyboard events
# xdotool keydown ctrl+r; xdotool keyup ctrl+r;
### These two lines of the script use sed to search through the Chromium preferences file and clear out any flags that would make the warning bar appear, a behavior you donât really want happening on your Raspberry Pi Kio>
#sed -i 's/"exited_cleanly":false/"exited_cleanly":true/' /home/pi/.config/chromium/Default/Preferences
#sed -i 's/"exit_type":"Crashed"/"exit_type":"Normal"/' /home/pi/.config/chromium/Default/Preferences
#launch python screenapi for homeassistant
/usr/bin/python3 /home/pi/hass-screenapi/manage.py runserver 0:8000 &
### This line launches Chromium with our parameters. We will go through each of these parameters so you know what you can modify, and how you can modify it.
### --kiosk : operate in kiosk mode (limited acces to browser and OS e.g. no system bar, no tabs)
### --noerrdialogs : do not show error dialogs
### --disable-infobars : disable info bar (e.g. "chroium is not de default browser")
### --start-fullscreen (not necessary in kiosk mode)
### --incognito
#we start the browser in a loop so we can kill it to reload the site
while true; do
rm /tmp/restart_chromium 2> /dev/null
chromium-browser http://homeassistant.local:8123/dashboard-kiosk/0 --kiosk --noerrdialogs --disable-infobars --no-first-run --enable-features=OverlayScrollbar --start-maximized
until [ -f /tmp/restart_chromium ]
do
sleep 0.1
done
done
- Install GitHub - muddyland/hass-screenapi: This is a simple API for Home Assistant to be able to turn off the monitor attached to a Raspberry Pi (installed in /home/pi/hass-screenapi) with git and manually test it (maybe some pip packages are missing)!
Modify views.py with ânano /home/pi/hass-screenapi/screenapi/views.pyâ to this:
from django.http import JsonResponse
import json
import subprocess
import pyautogui
import time
from .decorators import *
@key_check
def index(request):
return JsonResponse({"message":"Hello, world. This is just a dumb api"})
# Command to turn off the monitor
off_command = ["xset", "-display", ":0", "dpms", "force", "off"]
# Comamnd to turn on the monitor
on_command = ["xset", "-display", ":0", "dpms", "force", "on"]
# This will print out all the info about the screen, which we can parse to see if the monitor is on
status = ["xset", "-display", ":0", "q"]
#this will kill the browser
kill_command = ["killall", "-w", "chromium-browser"]
#this will restart the browser
restart_command = ["touch", "/tmp/restart_chromium"]
# Get Status of monitor
def is_on():
run = subprocess.run(status, stdout=subprocess.PIPE)
if "Monitor is On" in str(run.stdout):
return True
else:
return False
# If post, check for action and apply if needed
# If Get, return the status of the moitor. this allows Home Assistant to poll for the status
@key_check
def monitor(request):
if request.method == "POST":
body = request.body
do_action = json.loads(body)
if do_action.get("on") and is_on() == False:
pyautogui.hotkey('f5')
time.sleep(4) # Sleep for 4 seconds because reload with F5 takes some time
run = subprocess.run(on_command)
return JsonResponse({'success': "True", "exit_code" : "0" })
elif do_action.get("off") and is_on():
run = subprocess.run(off_command)
return JsonResponse({'success': "True", "exit_code" : run.returncode })
elif do_action.get("restart"):
run = subprocess.run(kill_command)
run = subprocess.run(restart_command)
return JsonResponse({'success': "True", "exit_code" : run.returncode })
elif do_action.get("reload"):
pyautogui.hotkey('f5')
return JsonResponse({'success': "True", "exit_code" : "0" })
else:
return JsonResponse({"no_action": True})
elif request.method == "GET":
return JsonResponse({"status" : is_on()})
- HAâs âhomeassistant.yamlâ (included in configuration.yaml) for trusted networks (so you dont have to login). You have to check/change your ipâs & change the user id:
#################################################### Auth providers ###########################################
#incl. bypass login for magic mirror 192.168.0.210
auth_providers:
- type: trusted_networks
trusted_networks:
- 192.168.0.0/24
- fd00::/8
trusted_users:
192.168.0.210: [!!!! user-id = long uuid number of user to logon !!!!!!!]
allow_bypass_login: true
- type: homeassistant
- in HAâs âswitches.yamlâ (included in configuration.yaml) create the switch for the monitor:
#monitor RPI magic mirror
- platform: rest
name: MagicMirror Monitor
resource: http://192.168.0.210:8000/monitor/
body_on: '{"on" : "true"}'
body_off: '{"off" : "true"}'
headers:
AUTH: !secret magic_mirror_rest_headers_secret
is_on_template: "{{ value_json.status }}"
- in HAâs âsecrets.yamlâ (included in configuration.yaml) create the secret:
################################################### MagicMirror hass screen api auth ###########################################
magic_mirror_rest_headers_secret: xxxxxx
- in HAâs ârest_commands.yamlâ (included in configuration.yaml) create the rest command for the monitor (reload, restart etc.):
#monitor RPI magic mirror
magicmirror_reload:
url: http://192.168.0.210:8000/monitor/
content_type: 'application/json; charset=utf-8'
verify_ssl: false
method: POST
timeout: 20
headers:
AUTH: !secret magic_mirror_rest_headers_secret
accept: "application/json, text/html"
payload: '{"reload" : "true"}'
magicmirror_restart:
url: http://192.168.0.210:8000/monitor/
content_type: 'application/json; charset=utf-8'
verify_ssl: false
method: POST
timeout: 20
headers:
AUTH: !secret magic_mirror_rest_headers_secret
accept: "application/json, text/html"
payload: '{"restart" : "true"}'
- use this in HAâs automations to refresh the monitor (F5) and turn it on after 4 seconds:
- service: switch.turn_on
target:
entity_id: switch.magicmirror_monitor
data: {}
and this to turn it off:
- service: switch.turn_off
target:
entity_id: switch.magicmirror_monitor
data: {}
I hope this helps
Have fun
pOpY