Files
homeassistant_espscreen/tests/test_shutdown.py
T
Max Gramser 1a6308aba7 Fix what a full test round of today's releases found: a ticking single clock, a switch that works in standby, no taps from a swipe (0.2.78)
A test and code-analysis round of 0.2.72 to 0.2.76 (suites at every tag, sanitizers, ESP32 and GitHub builds, host
harnesses on both boards, the editor in a browser, best practice checked against ESPHome, LVGL, Home Assistant and
Vue) found these, fixed here with Max's choices for the product questions:

- Firmware 0.2.65: the second hand of a single analog clock moves again; on a page that is one full on/off tile the
  waking push also switches it; on the Guition a flick is never a tap and a finger that slides off a tile doesn't tap
  it; Previous/Next react to a firm press; the same Go to page tile may sit on several pages; a layout the screen has
  no memory for is refused instead of crashing; 64-bit redraw mask for 48 tiles; Montserrat 14 no longer built.
- App: Save refuses a layout too long for one message or too big for eight pages; an offline screen keeps its
  firmware version from the device registry; HA refusals reach the editor; 128 KB requests; changelog out of the
  live stream; cached hashed assets; tini as init and a clean stop; changelog parsing without asterisks or crashes.
- Editor: clicking the open screen keeps unsaved edits, edits during a save stay unsaved, a Page row and phone
  dragging to other pages, repeated page tiles, limits from the server, HA's error messages, a type-checked build.
- tools/check.sh + GitHub Actions + a CYD flash budget; docs match 48 tiles and the new editor.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-18 17:25:23 +02:00

76 lines
3.4 KiB
Python

"""Stopping the app (app 0.2.78).
`init: false` came from the first add-on, whose image was Home Assistant's s6 base with its own init. The ESPHome base
image has none, so python3 ran as process 1: it ignored SIGTERM (every stop waited ten seconds for SIGKILL) and never
reaped a killed build. The app now gets Home Assistant's default init (tini), Docker Compose `init: true`, and the
server ends on SIGTERM through its own cleanup, which also stops a running build.
"""
import importlib.util
import os
from pathlib import Path
import signal
import subprocess
import sys
import tempfile
import time
import unittest
ROOT = Path(__file__).resolve().parents[1]
APP = ROOT / 'screen_manager/app'
HAS_AIOHTTP = importlib.util.find_spec('aiohttp') is not None
# The real server.py as `python3 server.py` runs it, with every port the OS's choice instead of 8099 and 8098.
LAUNCH = '''
import runpy, sys
from aiohttp import web
class Site(web.TCPSite):
def __init__(self, runner, host=None, port=None, **kwargs):
super().__init__(runner, '127.0.0.1', 0, **kwargs)
web.TCPSite = Site
sys.path.insert(0, sys.argv[1])
runpy.run_path(sys.argv[1] + '/server.py', run_name='__main__')
'''
class Configuration(unittest.TestCase):
def test_the_app_and_compose_run_an_init(self):
config = (ROOT / 'screen_manager/config.yaml').read_text()
self.assertNotRegex(config, r'(?m)^init:', "Home Assistant's default (init: true) gives the app tini as process 1")
self.assertRegex((ROOT / 'docker/compose.yaml').read_text(), r'(?m)^ init: true$')
self.assertIn('handle_signals=True', (APP / 'server.py').read_text())
@unittest.skipUnless(HAS_AIOHTTP, 'Run using .venv-portal/bin/python for server tests')
class Signal(unittest.TestCase):
def test_sigterm_ends_the_app_at_once_and_cleanly(self):
with tempfile.TemporaryDirectory() as tmp:
token = Path(tmp) / 'token'
token.write_text('not-a-real-token\n')
env = {**os.environ, 'SCREEN_DEV': '1', 'HA_TOKEN_FILE': str(token), 'HA_API': 'http://127.0.0.1:9/api',
'SCREEN_DATA': tmp, 'ESPHOME_CONFIG': str(Path(tmp) / 'esphome'), 'HA_CONFIG': tmp, 'PYTHONUNBUFFERED': '1'}
process = subprocess.Popen([sys.executable, '-c', LAUNCH, str(APP)], env=env, cwd=tmp,
stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, text=True)
try:
# Up once it tried Home Assistant (nothing listens on port 9 here).
deadline, lines = time.monotonic() + 20, []
while time.monotonic() < deadline:
line = process.stderr.readline()
lines.append(line)
if 'Home Assistant temporarily unavailable' in line or not line:
break
self.assertIn('Home Assistant temporarily unavailable', ''.join(lines), ''.join(lines))
started = time.monotonic()
process.send_signal(signal.SIGTERM)
code = process.wait(timeout=10)
self.assertEqual(code, 0, 'a normal stop, not killed by the signal')
self.assertLess(time.monotonic() - started, 5)
finally:
if process.poll() is None:
process.kill()
process.wait()
process.stderr.close()
if __name__ == '__main__':
unittest.main()