Week ahead in the weather strip, reverse LCD at night

The weekday strip now shows today and the next six days (icon, max, min),
the current temperature and feels-like sit next to the clock, and the rain
graph spans the full width with the hour under every bar. From sunset to
sunrise the whole frame, seconds patch included, is drawn as a reverse LCD.
The rain label is now spelled DÉŠŤ.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
rickzkmandClaude Opus 5.5 committed 2026-10-28 22:40:34 +01:00
1 parent 45508b1b68
commit 6c9a1420d2
10 files changed
+341 -122

No files matched your search

+127 -37
View File
@@ -1,15 +1,18 @@
from datetime import datetime
from datetime import date, datetime, time, timedelta
import numpy as np
from PIL import ImageChops
from lcdkit import INK
from lcdkit import BG, INK
from tests.helpers import count_colour
from view import Patch
from weather_data import Hour, Weather
from weather_view import WeatherView
from weather_data import Day, Hour, Weather
from weather_view import WeatherView, is_night
NOW = datetime(2026, 10, 22, 12, 45, 8) # a Thursday
CODES = (63, 45, 61, 3, 80, 0, 71)
CELL = 65 # width of one day in the forecast strip
BAR_X, BAR_PITCH, BAR_W = 22, 37, 26
class StubPoller:
@@ -27,9 +30,19 @@ class StubPoller:
pass
def weather(temp=14.2, rain=(0, 0, 10, 20, 40, 70, 60, 50, 30, 20, 10, 0), code=2):
def day(d: date, sunrise=time(7, 20), sunset=time(17, 45)) -> Day:
"""A forecast day whose values depend only on its date."""
return Day(d, CODES[d.toordinal() % 7], float(5 + d.day % 10), float(d.day % 5),
datetime.combine(d, sunrise), datetime.combine(d, sunset))
def week(start=date(2026, 10, 22), n=7):
return tuple(day(start + timedelta(days=i)) for i in range(n))
def weather(temp=14.2, rain=(0, 0, 10, 20, 40, 70, 60, 50, 30, 20, 10, 0), code=2, days=None):
hours = tuple(Hour(datetime(2026, 10, 22, 12 + i, 0), p, 0.2) for i, p in enumerate(rain))
return Weather(temp=temp, code=code, today_max=16.0, today_min=7.0, hours=hours)
return Weather(temp=temp, feels=12.1, code=code, days=week() if days is None else days, hours=hours)
def view(**kw):
@@ -45,6 +58,20 @@ def ink_bbox(img, box):
return (box[0] + xs.min(), box[1] + ys.min(), box[0] + xs.max(), box[1] + ys.max())
def icon_box(i):
cx = 12 + i * CELL + CELL // 2
return (cx - 16, 34, cx + 16, 68)
def hour_label_box(i):
x = BAR_X + i * BAR_PITCH
return (x, 290, x + BAR_W, 304)
def same(a, b):
return ImageChops.difference(a.convert("RGB"), b.convert("RGB")).getbbox() is None
def test_render_size_is_480x320():
assert view(value=weather()).render(NOW).size == (480, 320)
@@ -77,20 +104,20 @@ def test_patch_matches_full_render_region():
assert isinstance(patch, Patch)
full = v.render(NOW)
region = full.crop((patch.x, patch.y, patch.x + patch.image.width, patch.y + patch.image.height))
assert ImageChops.difference(region.convert("RGB"), patch.image.convert("RGB")).getbbox() is None
assert same(region, patch.image)
def test_patch_changes_with_the_second():
v = view(value=weather())
a = v.patch(datetime(2026, 10, 22, 12, 45, 8)).image
b = v.patch(datetime(2026, 10, 22, 12, 45, 9)).image
assert ImageChops.difference(a, b).getbbox() is not None
assert not same(a, b)
def test_no_weather_renders_without_error():
img = view(value=None, version=0, age=None).render(NOW)
assert img.size == (480, 320)
# weekday strip, clock and dashes are still drawn
# day names, clock and dashes are still drawn
assert count_colour(img, INK) > 1000
@@ -99,35 +126,54 @@ def test_stale_tag_only_when_old():
old = view(value=weather(), age=7200.0).render(NOW)
bbox = ImageChops.difference(fresh.convert("RGB"), old.convert("RGB")).getbbox()
assert bbox is not None
assert bbox[0] >= 20 and bbox[1] >= 160 and bbox[2] <= 204 and bbox[3] <= 186
assert bbox[0] >= 20 and bbox[1] >= 200 and bbox[2] <= 300 and bbox[3] <= 226 # on the rain header line
def test_hundred_percent_fits():
rain = (100,) * 12
img = view(value=weather(rain=rain)).render(NOW)
box = ink_bbox(img, (14, 262, 204, 312))
assert box is not None and box[2] < 200
def test_today_is_the_first_day_and_inverted():
img = view(value=weather()).render(NOW)
assert img.getpixel((12 + 5, 14)) == INK
assert img.getpixel((12 + CELL + 5, 14)) != INK
def test_negative_temp_fits_and_draws_minus():
def test_days_before_today_are_skipped():
# just after midnight the last fetch still starts with yesterday
from_yesterday = view(value=weather(days=week(start=date(2026, 10, 21)))).render(NOW)
from_today = view(value=weather(days=week(start=date(2026, 10, 22), n=6))).render(NOW)
assert same(from_yesterday, from_today)
def test_only_days_with_a_forecast_get_an_icon():
img = view(value=weather(days=week(n=3))).render(NOW)
for i in range(3):
assert ink_bbox(img, icon_box(i)) is not None, i
for i in range(3, 7):
assert ink_bbox(img, icon_box(i)) is None, i
def test_every_bar_has_its_hour_label():
img = view(value=weather()).render(NOW)
for i in range(12):
assert ink_bbox(img, hour_label_box(i)) is not None, i
def test_hour_labels_only_for_known_hours():
img = view(value=weather(rain=(10, 20))).render(NOW)
assert ink_bbox(img, hour_label_box(1)) is not None
for i in range(2, 12):
assert ink_bbox(img, hour_label_box(i)) is None, i
def test_negative_temps_stay_inside_the_frame():
for temp in (-3.4, -10.4, -19.6):
img = view(value=weather(temp=temp)).render(NOW)
box = ink_bbox(img, (380, 104, 471, 150))
assert box is not None and box[2] < 466, temp
def test_negative_temp_draws_a_minus():
cold = view(value=weather(temp=-3.4)).render(NOW)
warm = view(value=weather(temp=3.4)).render(NOW)
cold_box = ink_bbox(cold, (84, 180, 204, 236))
assert cold_box is not None and cold_box[2] < 200
assert ImageChops.difference(cold.convert("RGB"), warm.convert("RGB")).getbbox() is not None
def test_sunday_inverts_last_weekday_cell():
sunday = datetime(2026, 10, 25, 9, 0, 0)
img = view(value=weather()).render(sunday)
assert img.getpixel((402 + 1, 15)) == INK # NE cell
assert img.getpixel((12 + 1, 15)) != INK # PO cell
def test_thursday_inverts_fourth_cell():
img = view(value=weather()).render(NOW)
assert img.getpixel((12 + 3 * 65 + 1, 15)) == INK # ČT
assert img.getpixel((12 + 1, 15)) != INK
assert not same(cold, warm)
def test_fewer_than_twelve_hours_still_renders():
@@ -139,8 +185,52 @@ def test_unknown_weather_code_renders():
assert view(value=weather(code=999)).render(NOW).size == (480, 320)
def test_two_digit_negative_temps_stay_clear_of_the_divider():
for temp in (-10.4, -19.6):
img = view(value=weather(temp=temp)).render(NOW)
box = ink_bbox(img, (84, 180, 204, 236))
assert box is not None and box[2] < 200, temp
# night: from sunset to sunrise the frame is a reverse LCD
def test_is_night_between_sunset_and_sunrise():
w = weather() # sunrise 7:20, sunset 17:45
assert is_night(datetime(2026, 10, 22, 21, 0), w)
assert is_night(datetime(2026, 10, 22, 5, 30), w)
assert not is_night(datetime(2026, 10, 22, 12, 45), w)
def test_is_night_flips_exactly_at_sunrise_and_sunset():
w = weather()
assert not is_night(datetime(2026, 10, 22, 7, 20), w)
assert is_night(datetime(2026, 10, 22, 7, 19), w)
assert not is_night(datetime(2026, 10, 22, 17, 44), w)
assert is_night(datetime(2026, 10, 22, 17, 45), w)
def test_is_night_uses_todays_sun_times():
days = (day(date(2026, 10, 21), sunset=time(16, 0)), day(date(2026, 10, 22), sunset=time(17, 45)))
assert not is_night(datetime(2026, 10, 22, 16, 30), weather(days=days))
def test_is_night_without_today_uses_the_closest_known_day():
# data a week old (fetches failing): sunrise moves only minutes a day, so old times are close enough
days = (day(date(2026, 10, 15), sunrise=time(7, 8), sunset=time(17, 58)),)
assert is_night(datetime(2026, 10, 22, 21, 0), weather(days=days))
assert not is_night(datetime(2026, 10, 22, 12, 0), weather(days=days))
def test_is_night_without_any_forecast_stays_day():
assert not is_night(datetime(2026, 10, 22, 23, 0), None)
assert not is_night(datetime(2026, 10, 22, 23, 0), weather(days=()))
def test_night_frame_is_dark():
night = view(value=weather()).render(datetime(2026, 10, 22, 21, 0, 0))
day_ = view(value=weather()).render(NOW)
assert sum(night.getpixel((2, 2))) < 100
assert day_.getpixel((2, 2)) == BG
def test_night_patch_matches_full_render_region():
v = view(value=weather())
at = datetime(2026, 10, 22, 21, 0, 8)
patch = v.patch(at)
full = v.render(at)
region = full.crop((patch.x, patch.y, patch.x + patch.image.width, patch.y + patch.image.height))
assert same(region, patch.image)
assert sum(patch.image.getpixel((0, 0))) < 100