cremalink.local_server_app.embedded module
In-process hosting for the cremalink local LAN protocol server.
Lets a consumer (e.g. cremalink_ha) run the exact same aiohttp
application used by the standalone cremalink-server process, in-process
on its own asyncio event loop – no separate OS process, no Supervisor
add-on required (spec: 002-embedded-local-server, FR-001/FR-002).
Nothing about the local LAN protocol itself changes here: create_app()
already builds a fully asyncio-native aiohttp application (state, protocol,
device adapter, background jobs all reuse existing code unchanged). This
module only changes how that application is hosted – via
aiohttp.web.AppRunner/SockSite bound to a pre-bound socket, rather
than a separate OS process.
Migrated from a programmatically driven uvicorn.Server (spec 002’s
original implementation) to aiohttp (specs/002-embedded-local-server
research.md #1 “Revision”): aiohttp is already a mandatory Home
Assistant dependency, whereas fastapi/starlette/uvicorn were
three extra packages pulled in solely for local mode. Unlike uvicorn’s
Server.serve(), aiohttp’s AppRunner/SockSite do not run as a
single cancellable coroutine task we own – connections are accepted
directly by the asyncio event loop’s own server machinery. There is
therefore no equivalent “serve task exited unexpectedly” signal to
monitor; state only becomes "failed" from a start()-time error
(e.g. the port-fallback range being exhausted), not from a later runtime
fault in the HTTP layer itself.
- cremalink.local_server_app.embedded.DEFAULT_PORT = 10280
Default port, matching the standalone server/add-on’s historical default.
- cremalink.local_server_app.embedded.DEFAULT_PORT_FALLBACK_RANGE = 50
How many ports past the preferred one to try before giving up (FR-007).
- cremalink.local_server_app.embedded.DEFAULT_SESSION_KEEPALIVE_SECONDS = 20.0
the app polls local_reg every ~10 s; 20–25 s held a session 15 min in field tests.
- Type:
LAN session keep-alive cadence (lodzen/cremalink#4)
- class cremalink.local_server_app.embedded.EmbeddedLocalServer(dsn: str, device_ip: str, lan_key: str, device_map_path: str | None = None, *, advertised_ip: str | None = None, monitor_poll_interval: float = 5.0, nudger_poll_interval: float = 1.0, cloud_token_path: str | None = None, event_logger: Logger | None = None, preferred_port: int = 10280, port_fallback_range: int = 50, bind_host: str = '0.0.0.0')[source]
Bases:
objectIn-process host for a single device’s cremalink local LAN server.
See
specs/002-embedded-local-server/contracts/embedded-server-api.mdfor the full behavioral contract.- log(event: str, details: dict | None = None, *, level: int = 20) None[source]
Route an integration-owned event through this server’s log path.
- log_telemetry(event: str, details: dict, *, level: int = 20) None[source]
Forward telemetry through the HA logger without buffering it.
- set_machine_asleep(asleep: bool) None[source]
Tell the server jobs the machine’s sleep state.
The consumer (HA coordinator) knows the monitor status; in standby the monitor/nudger jobs drop to their standby cadence so a sleeping machine is not polled every few seconds.
- async start() None[source]
Resolve advertised IP + a free port, then start serving.
- Raises:
RuntimeError – if already starting/running.
OSError – if no port in the fallback range could be bound.
- cremalink.local_server_app.embedded.SITE_SHUTDOWN_TIMEOUT = 5.0
Bound on how long stop() can take waiting for in-flight connections to close (aiohttp’s own default is 60s) – a stuck device connection must never make a config-entry reload/unload/removal hang for that long (research.md #7, tasks.md Phase 9).
- cremalink.local_server_app.embedded.detect_advertised_ip(device_ip: str) str[source]
Return the local IP the OS would route through to reach
device_ip.Uses the “UDP connect” trick: connecting a UDP socket never actually sends a packet, it only asks the OS to resolve the outbound route – fast and side-effect free (research.md #3, FR-013).