cremalink.local_server_app.state module

This module defines the state management for the local server application. It centralizes all runtime data, including device configuration, cryptographic keys, command queues, and the latest received device data.

class cremalink.local_server_app.state.LocalServerState(settings: ServerSettings, logger)[source]

Bases: object

Manages the runtime state of the local server in a thread-safe manner.

This class acts as a state machine, holding all information related to the device connection, including credentials, cryptographic session keys, pending commands, and the most recent data snapshots (monitor and properties). An asyncio.Lock is used to prevent race conditions when accessing state from different asynchronous tasks.

async configure(dsn: str, device_ip: str, lan_key: str, device_scheme: str = 'https', monitor_property_name: str | None = None, data_request_property_name: str | None = None) → None[source]

Configures the state with new device details and resets the session. If the device details are unchanged, this is a no-op.

async drain_responses() → list[dict[str, Any]][source]

Returns all queued data_response entries (oldest→newest) and clears them.

enqueue_seq

Incremented alongside work_enqueued on every queue_* append — the nudger’s edge check compares against the value seen at its previous tick, so enqueues landing between standby ticks count as fresh work even when the queue never read empty in between.

async get_named_property(name: str) → dict[str, Any] | None[source]

Returns the stored entry for a datapoint name, if seen.

async get_property_value(property_name: str) → Any | None[source]

Retrieves a single property value from the last known snapshot.

async handle_datapoint(decrypted_json: dict) → None[source]

Processes a decrypted data payload from the device, updating the appropriate data snapshot (properties or monitor).

async init_crypto(random_1: str, time_1: str | int) → None[source]

Derives and initializes all session keys using values from the key exchange.

is_configured() → bool[source]

Returns True if the server has been configured with device details.

keys_ready() → bool[source]

Returns True if the cryptographic session keys have been derived.

log(event: str, details: dict | None = None, *, level: int = 20) → None[source]

Log operational detail separately from the sanitized event buffer.

log_telemetry(event: str, details: dict, *, level: int = 20) → None[source]

Forward redacted telemetry without retaining it in diagnostics.

machine_asleep: bool

Set by the consumer (HA coordinator) from the monitor status — jobs drop to their standby cadence so a sleeping machine is not flooded with monitor/keepalive traffic it cannot use.

mark_commands_poll() → None[source]

Record a commands.json poll from the machine.

async next_command_payload() → dict[str, Any][source]

Retrieves the next command from the queue for sending to the device. If the queue is empty, it returns an empty “heartbeat” payload.

async pop_response() → dict[str, Any] | None[source]

Returns the most recent data_response mailbox entry (LIFO).

async queue_command(command: str) → None[source]

Adds a high-level device command to the outgoing queue.

async queue_monitor() → None[source]

Adds a request for the device’s monitoring status to the queue.

async queue_properties() → None[source]

Adds a request for all device properties to the queue.

async queue_property_get(name: str) → None[source]

Adds a request for a single named datapoint to the queue.

async queue_property_set(name: str, value: Any) → None[source]

Adds a write for a single named datapoint to the outgoing queue.

Same properties envelope as queue_command, but the property name is caller-provided (e.g. device_connected for session announcements).

async rekey() → None[source]

Prepares a fresh LAN session (lodzen/cremalink#4).

The machine only performs a key exchange on POST local_reg (or on its own initiative) — a PUT on a live session is a pure keep-alive. Discarding our keys before registering therefore produces a hollow session: the machine keeps encrypting with the old keys while we can no longer decrypt anything. So this method now only resets the registration state — fresh random_2 / time_2 nonces and registered = False — while the derived keys, datapoint store, mailbox, and command queue are kept. The keys are replaced atomically by init_crypto() when the machine answers the subsequent POST with a key exchange, so in-flight old-key datapoints keep decrypting until then.

session_live() → bool[source]

True while a registered LAN session with valid keys is held.

Drives the local_reg method choice (lodzen/cremalink#4): PUT keeps a live session alive / notifies it; POST starts a new session and always triggers a key exchange.

set_machine_asleep(asleep: bool) → None[source]

Switch the jobs between normal and standby polling cadence.

async set_registered(value: bool) → None[source]
async snapshot_monitor() → dict[str, Any][source]

Returns the latest monitoring data snapshot.

async snapshot_properties() → dict[str, Any][source]

Returns the latest properties data snapshot.

async wait_for_commands_poll(timeout: float, *, since: float | None = None) → bool[source]

Wait until the machine polls commands.json after since.

PUT local_reg with notify: 1 makes the machine poll within ~0.05–1.3 s; no poll inside timeout means the session is dead on the machine side.

async wait_for_datapoint(since: int, timeout: float) → int[source]

Blocks until the datapoint seq differs from since.

Returns the current seq — callers compare it to what they passed to detect arrivals. A datapoint landing between the caller’s last harvest and this call can never be missed: the seq already differs when the wait starts. Internally a cheap seq poll at 50 ms granularity; nothing per-call is allocated or registered.

async wait_for_key_exchange(timeout: float, *, since: float | None = None) → bool[source]

Wait until a key exchange completes after since.

work_enqueued

Set by every queue_* append so the nudger’s sleep loop wakes at once — queued work (e.g. a wake frame during standby) is edge- detected within a tick instead of waiting out a 30 s interval.