cremalink.ecam package

Transport-independent ECAM protocol layer.

Pure Python: frame builders, answer parsers, machine dialect profiles, the write-gate session helper, native 0xA2 statistics, profile-name blobs, writable settings and the recipe catalogue. No network or Home Assistant imports live here — callers hand transports in.

class cremalink.ecam.Beverage(bev_id: int, kind: str, declared: bool = False, ranges: dict = <factory>, profiles: dict = <factory>, in_priority_list: bool | None = None, priority_position: dict = <factory>, defined: bool | None = None, profiles_differ: bool = False, off_default: dict | None = None, source_properties: tuple = (), name: str | None = None, ingredients: tuple[int, ...]=(), use_for_custom: bool | None = None)[source]

Bases: object

One catalogue beverage.

bev_id: int
declared: bool = False
defined: bool | None = None
in_priority_list: bool | None = None
ingredients: tuple[int, ...] = ()
kind: str
name: str | None = None
off_default: dict | None = None
priority_position: dict
profiles: dict
profiles_differ: bool = False
ranges: dict
source_properties: tuple = ()
use_for_custom: bool | None = None
class cremalink.ecam.BrewAction(*values)[source]

Bases: int, Enum

Action/strength byte of a 0x83 brew frame.

START = 1
STOP = 2
class cremalink.ecam.MachineProfile(key: str, command_property: str, trailer: str, session: str, monitor_request_ids: frozenset[int], profile_entry_size: int, grinder_scale: float)[source]

Bases: object

Dialect declaration a device binds to via machine_profile.

Fields:

  • key: stable identifier ("non_striker" / "striker").

  • command_property: property frames are written to (data_request vs app_data_request).

  • trailer: transport suffix after the timestamp (NONE timestamp only, APP_ID 4-byte registered app id).

  • session: write-gate mechanism (plain unix timestamp write to device_connected vs 0xE8 handshake + 84 0f 03 02 registration).

  • monitor_request_ids: request ids that carry monitor frames (the device map’s monitor_profile stays authoritative per data-model.md; this is informational).

  • profile_entry_size: bytes per profile entry in 0xA4 blobs (21 non-striker / 22 striker incl. mug byte).

  • grinder_scale: multiplier applied to bean-system grinder values on striker (write *2, read /2 per APK O0/G0).

command_property: str
grinder_scale: float
key: str
monitor_request_ids: frozenset[int]
profile_entry_size: int
session: str
trailer: str
class cremalink.ecam.PowerCommand(*values)[source]

Bases: bytes, Enum

Payloads of the 0x84 0x0F power/session family (verified).

SESSION_REFRESH = b'\x03\x02'
STANDBY = b'\x01\x01'
WAKE = b'\x02\x01'
class cremalink.ecam.ProfileSlot(index: int, name: str, icon: int, mug: int | None = None)[source]

Bases: object

One occupied machine profile slot.

icon: int
index: int
mug: int | None = None
name: str
class cremalink.ecam.RecipeCatalogue(fingerprint: tuple, source: str, beverages: dict[int, ~cremalink.ecam.catalog.Beverage], priority_lists: dict[int, list[int]], names: dict[str, dict[int, str]], stats: dict, favorite_lists: dict[int, list[int]] = <factory>)[source]

Bases: object

The parsed recipe catalogue — advisory only; brew never depends on it.

beverages: dict[int, Beverage]
favorite_lists: dict[int, list[int]]

per-profile favourites (0xAC), in the machine’s favourite order

fingerprint: tuple
names: dict[str, dict[int, str]]
priority_lists: dict[int, list[int]]
source: str
stats: dict
class cremalink.ecam.SessionGate(profile: ~cremalink.ecam.machine_profiles.MachineProfile, announce: ~collections.abc.Callable[[], ~typing.Any], *, refresh_seconds: int = 240, clock: ~collections.abc.Callable[[], float] = <built-in function time>)[source]

Bases: object

Tracks session freshness and performs announces on demand.

Constructed per device/transport — per-entry state only. announce is the profile-appropriate way to (re)open the session:

  • non-striker: write_property(connected_property, unix_ts)

  • striker: the 0xE8 handshake + 84 0f 03 02 registration callback (run once), returning the registered app-id bytes.

announced_at

unix time of the last successful announce (0 = never).

ensure_fresh(now: float | None = None) → bool[source]

Announce the session when stale. Returns True iff an announce ran.

invalidate() → None[source]

Force the next gated write to re-announce (e.g. after an ack indicated the write was ignored).

is_fresh(now: float | None = None) → bool[source]

Whether the current session is still inside the refresh window.

property uses_timestamp_announce: bool

Non-striker machines announce via the device_connected ts.

class cremalink.ecam.StatisticEntry(a2_id: int, raw: int, label: str | None, value: int | float, unit: str | None)[source]

Bases: object

One interpreted A2 statistic.

a2_id: int
label: str | None
raw: int
unit: str | None
value: int | float
class cremalink.ecam.StatisticsReport(source: str, entries: dict[int, ~cremalink.ecam.statistics.StatisticEntry]=<factory>, complete: bool = False, fetched_at: float = <factory>, data_updated_at: float | None = None, cloud_counters: dict[str, int | float | None]=<factory>, breakdowns: dict[str, dict]=<factory>)[source]

Bases: object

Result of one statistics fetch.

breakdowns: dict[str, dict]

per-counter JSON breakdowns (Eletta aggregated counters)

cloud_counters: dict[str, int | float | None]

logical cloud counters when source == “cloud_counters”

complete: bool = False
data_updated_at: float | None = None

when the cloud last received a statistics datapoint from the machine (epoch); None where the transport has no per-datapoint times

entries: dict[int, StatisticEntry]
fetched_at: float
source: str
class cremalink.ecam.WritableSetting(key: str, param_id: int, options: dict[int, str], capability: str, datapoint_hint: str = '', default_levels: int | None = None)[source]

Bases: object

A capability-gated machine setting writable via 0x90.

capability: str
datapoint_hint: str = ''
default_levels: int | None = None

Levels a model offers unless its map’s setting_levels says otherwise (None: every option).

key: str
options: dict[int, str]
param_id: int
cremalink.ecam.answer_flag(frame: bytes | bytearray) → int[source]

The echoed request flag — byte[3] of the 0xD0 blob.

cremalink.ecam.answer_id(frame: bytes | bytearray) → int[source]

The echoed request id — byte[2] of the 0xD0 blob.

cremalink.ecam.blob_family(value: Any) → bytes | None[source]

The family bytes of a machine blob, read without decoding all of it.

cremalink.ecam.brew_param_included(param_id: int) → bool[source]

The app-side E0 predicate: which recipe params go on the wire.

cremalink.ecam.build_bean_system(index: int, profile: MachineProfile) → bytes[source]

0D 06 BA F0 <index> — bean-system slot read (APK U).

cremalink.ecam.build_brew(beverage_id: int, action: int | BrewAction, recipe: bytes | bytearray | Iterable[int], profile: MachineProfile, *, profile_slot: int = 0, accessory: int = 0) → bytes[source]

Build a 0x83 brew frame.

0D LEN 83 F0 <bev> <action> <recipe…> <(profile_slot<<2)|accessory> — the frame names its profile in the trailer byte; the recipe bytes must be that same profile’s values (0xA6 reads are profile-scoped).

cremalink.ecam.build_catalog(properties: dict) → RecipeCatalogue[source]

Build the machine’s beverage catalogue from property values.

cremalink.ecam.build_factory_descriptor(beverage_id: int, profile: MachineProfile) → bytes[source]

0D 06 B0 F0 <bev> — min/default/max descriptor read (APK Y0).

cremalink.ecam.build_monitor_request(profile: MachineProfile) → bytes[source]

0D 05 <monitor_id> 0F — monitor snapshot request (APK V).

cremalink.ecam.build_power(kind: PowerCommand, profile: MachineProfile) → bytes[source]

Build an 0x84 0x0F power/session frame.

cremalink.ecam.build_priority_list(profile_slot: int, profile: MachineProfile) → bytes[source]

0D 06 A8 F0 <prof> — per-profile ordered beverage list.

cremalink.ecam.build_profile_names(first: int, count: int, profile: MachineProfile) → bytes[source]

0D 07 A4 F0 <first> <count> — profile-name read (APK t0).

cremalink.ecam.build_profile_select(profile_slot: int, profile: MachineProfile) → bytes[source]

0D 06 A9 F0 <prof> — profile select (verified live).

This is a gated write: the session must be announced first.

cremalink.ecam.build_read(request_id: int, args: bytes | bytearray | Iterable[int] = b'', profile: MachineProfile | None = None, *, flag: int | None = None) → bytes[source]

Build a read-style request frame for the given request id.

args is the raw payload after the flag byte. Flags: 0x0F for the statistics family, 0xF0 (answer wanted) elsewhere — both verified on live LAN. Use the build_read_* helpers below for the structured payloads.

cremalink.ecam.build_read_block(param_id: int, count: int, profile: MachineProfile) → bytes[source]

0D 08 <0x95|0xA1> <flag> <param:2B> <count> — APK r0 block read.

count > 4 goes out as the 0xA1 extended read; the machine normalises the answer back to a 0x95 param list.

cremalink.ecam.build_read_param(param_id: int, profile: MachineProfile) → bytes[source]

0D 08 95 <flag> <param:2B> <qty=1> — single parameter read.

The app’s buildParamRead (protocol.md §8) always carries the qty byte; the legacy qty-less len-7 form is only tolerated by the machine (answered sporadically — see §14.8’s 2026-10-14 captures where len-7 reads were dropped while 0D 08 app reads answered). Delegates to build_read_block() with count=1, which emits the identical wire frame.

cremalink.ecam.build_read_params(first_param: int, last_param: int, profile: MachineProfile) → bytes[source]

0D 09 95 <flag> <first:2B> <last:2B> — param range read (captured).

cremalink.ecam.build_recipe_names(first: int, count: int, profile: MachineProfile) → bytes[source]

0D 07 AA F0 <first> <count> — custom recipe-name read (APK v0).

cremalink.ecam.build_recipe_values(profile_slot: int, beverage_id: int, profile: MachineProfile) → bytes[source]

0D 07 A6 F0 <prof> <bev> — profile-scoped recipe read (APK M0).

cremalink.ecam.build_statistics_page(start_id: int, count: int, profile: MachineProfile) → bytes[source]

0D 08 A2 0F <start_id:2B> <count:1B> — statistics page read.

cremalink.ecam.build_striker_handshake(profile: MachineProfile) → bytes[source]

0D 06 E8 F0 00 — striker session handshake (APK z0).

cremalink.ecam.build_write_param(param_id: int, value: int, profile: MachineProfile, *, flag: int | None = None) → bytes[source]

Build a 0x90 parameter-write frame.

0D 0B 90 <flag> <param:2B> <u32:4B> — byte-identical to the app’s n0() builder: flag 0xF0 for param >= 1000, else 0x0F (protocol.md §14.5 verified 90 0F 00 3E … committing under the device_connected session announce; a 90 F0 write without the announce only staged transiently). flag stays overridable.

cremalink.ecam.catalog_beverage_ids(catalog: RecipeCatalogue | None) → set[int][source]

Every beverage id the machine declared, whatever its kind.

cremalink.ecam.counter_breakdown(val: Any) → dict | None[source]

Return the per-recipe JSON breakdown of a counter, else None.

cremalink.ecam.decode_a2_page(frame: bytes | bytearray) → list[tuple[int, int]][source]

0xA2/0xA1 statistics page → [(id, raw_u32), …].

Records are 6 bytes <id:2B> <u32:4B> starting at payload offset 2 (APK h.u parser); trailing partial records are ignored.

cremalink.ecam.decode_blob(value: Any) → dict | None[source]

Classify one datapoint value as a machine blob, or None.

Three gates: 0xd0 prefix, declared length, CRC. The length gate is declared <= len(raw): the machine appends a 4-byte timestamp to some datapoints, and strict equality misgrades those live channels as truncated.

cremalink.ecam.encode_for_transport(frame: bytes | bytearray, profile: MachineProfile, *, timestamp: int | None = None, app_id: bytes | bytearray | None = None, raw: bool = False) → str[source]

Base64-encode frame + <ts:4B BE> [+ <app_id:4B>] for the wire.

The timestamp is never CRC-covered; the striker app-id trailer follows it (FR-004/005/007). raw=True skips frame validation — the non-striker s0() handshake blob is a valid data_request payload that carries no 0x0D header (§14.8).

cremalink.ecam.fingerprint(properties: dict) → tuple[source]

A change-detection key over catalogue datapoints, and only those.

cremalink.ecam.interpret(entries: Iterable[tuple[int, int]], *, source: str = 'native', complete: bool = False, fetched_at: float | None = None) → StatisticsReport[source]

Fold (a2_id, raw) pairs into a labelled StatisticsReport.

Unlabeled ids pass through with label=None — they are real machine data, just not display-named.

cremalink.ecam.is_sync_timeout(frame: bytes | bytearray) → bool[source]

0xE1 — machine reports busy/sync-timeout; abort the pending read.

Tolerant by design: a blob that does not parse cannot be an abort.

cremalink.ecam.parse_a9_ack(frame: bytes | bytearray) → tuple[int, bool][source]

0xA9 profile-select ack → (profile, ok) — <prof> <ok=00>.

cremalink.ecam.parse_counter_value(val: Any) → int | None[source]

Return the integer state for a counter value, or None.

Plain integers pass through; a JSON object is summed over its integer sub-values; anything else yields None (unknown).

cremalink.ecam.parse_profile_names(blob: bytes | bytearray, profile: MachineProfile) → list[ProfileSlot][source]

Parse one a4f0 envelope into occupied ProfileSlots.

Never raises on malformed input — unparseable content yields [] (callers treat a missing page as “no slots”, never a crash).

cremalink.ecam.parse_settings(blob: bytes | bytearray) → dict[int, int][source]

Parse a 0x95 answer/stored blob into {param_id: raw_u32}.

Shape-tolerant: short stored values, full republish blobs (descriptor tail ignored), ack echoes (no values) and all-zero unknown-parameter blobs are all handled without raising.

cremalink.ecam.parse_simple_ack(frame: bytes | bytearray) → tuple[int, bool][source]

Generic <ok:1B> ack — used by 0x83/0xA5/0xAB/0xB9/ 0xBB/0xE2 answers. Returns (answer_id, ok).

cremalink.ecam.parse_tlv(payload: bytes) → dict[int, int] | None[source]

Parse a TLV parameter payload; None if it does not consume exactly.

cremalink.ecam.parse_triples(payload: bytes) → dict[int, tuple[int, int, int]] | None[source]

Parse a factory descriptor into tag -> (min, default, max).

cremalink.ecam.parse_water_volume_liters(val: Any) → float | None[source]

Convert a water counter (half-millilitres, raw / 2000) to litres.

Same unit as the app’s LAN statistics (STAT_LABELS unit l); checked on an Eletta Explore: raw 1782545 = 891 l on its display.

cremalink.ecam.parse_write_ack(frame: bytes | bytearray) → tuple[int, bool][source]

0x90 ack → (param_id, ok) — payload <param:2B> <ok:1B>.

Ack byte 0x00 means the write committed (b==0 ⇒ success per the APK dispatcher).

cremalink.ecam.profile_from_map(map_data: dict[str, Any], *, logger_: Logger | None = None) → MachineProfile[source]

Bind the device map’s machine_profile field to a preset.

A missing field binds NON_STRIKER with a logged warning (the verified Soul behaviour). An unrecognized value raises ValueError — failing closed is required because command construction on the wrong dialect produces frames the machine ignores (FR-006).

cremalink.ecam.resolve_cloud_breakdowns(properties: dict[str, Any]) → dict[str, dict][source]

Per-counter JSON breakdowns, keyed by logical counter.

cremalink.ecam.resolve_cloud_counters(properties: dict[str, Any]) → dict[str, int | float | None][source]

Resolve logical counters against the property/dp cache.

properties maps datapoint name → value (or a {"value": …} wrapper). First present candidate wins; absent candidates produce no key — an unresolved counter is “absent”, not None (FR-016d).

cremalink.ecam.trim_frame(blob: bytes | bytearray) → bytes[source]

Cut a received blob to its declared frame, dropping any trailer.

data_response/republished datapoints append a 4-byte timestamp after the CRC — the trailing bytes are transport metadata, not frame.

cremalink.ecam.verify_frame(frame: bytes | bytearray) → bool[source]

Validate a wire frame’s length byte and CRC.

Bytes beyond the declared length (e.g. the datapoint timestamp trailer) are ignored. Returns False on any structural violation — never raises.

Submodules