Модели протокола¶
Pydantic-модели для валидации и конструирования JSON-структур протокола Sber Smart Home.
Pydantic models for Sber Smart Home MQTT protocol.
Provides strict typed schemas for Sber protocol payloads (device config, states, commands) and helper functions for constructing protocol values.
All models use extra="forbid" to reject unexpected fields — this catches
protocol violations like the TV allowed_values bug (extra keys caused
Sber cloud to silently reject devices).
These models serve as: - Executable specification of the Sber C2C JSON protocol - Pre-publish validation layer (invalid devices excluded from payload) - Type-safe constructors for Sber state values
Source of truth: https://developers.sber.ru/docs/ru/smarthome/c2c/
CATEGORY_REQUIRED_FEATURES
module-attribute
¶
Required features per Sber category.
Derived from :data:CATEGORY_OBLIGATORY_FEATURES with the
_CATEGORY_OBLIGATORY_OVERRIDES applied. Kept as the public name for
backward compatibility — callers should continue using this constant.
SberColourValue
¶
Bases: BaseModel
HSV colour value per Sber spec.
Ranges
h: 0-360 (hue degrees) s: 0-1000 (saturation, 0.1% steps) v: 100-1000 (value/brightness, min 100 per Sber spec VR-004)
SberValue
¶
Bases: BaseModel
A typed value in Sber state or command payload.
Sber protocol uses tagged unions: the type field selects which
*_value field carries the actual data.
Per Sber C2C spec:
- integer_value is always a string (e.g. "220", not 220)
- colour_value is an HSV object with h/s/v integer fields
SberState
¶
Bases: BaseModel
Single state key-value pair in the Sber protocol.
check_feature_type_matches
¶
Reject states where the value type doesn't match the Sber spec for the key.
Protects against silent Sber rejections like the PIR-as-BOOL bug:
Sber expects pir as {"type": "ENUM", "enum_value": "pir"}
but a buggy device class could emit {"type": "BOOL", "bool_value": true}
— Sber would accept the payload and quietly drop the device.
Uses :data:FEATURE_TYPES (auto-generated from Sber docs). Unknown
keys are allowed — not all features are in the generated catalog
(typos / undocumented features are handled by compliance tests).
Source code in custom_components/sber_mqtt_bridge/sber_models.py
SberAllowedIntegerValues
¶
Bases: BaseModel
INTEGER allowed values with min/max/step as strings.
SberAllowedFloatValues
¶
Bases: BaseModel
FLOAT allowed values with numeric min/max.
SberAllowedEnumValues
¶
Bases: BaseModel
ENUM allowed values with list of valid strings.
SberAllowedValue
¶
Bases: BaseModel
Single allowed_values entry for a feature.
Type discriminator selects which *_values field is present.
COLOUR type has no additional constraints — just {"type": "COLOUR"}.
SberDependencyCondition
¶
Bases: BaseModel
Single condition value in a dependency declaration.
SberDependency
¶
Bases: BaseModel
Feature dependency: feature X is available only when key Y has given values.
SberDeviceModel
¶
Bases: BaseModel
Device model descriptor within a Sber device config.
Per Sber spec, allowed_values should only contain keys that are
in features and need non-default ranges. Extra keys cause silent
device rejection.
must_have_online
classmethod
¶
All Sber devices must include 'online' feature (VR-010).
Source code in custom_components/sber_mqtt_bridge/sber_models.py
allowed_values_keys_must_be_known
classmethod
¶
allowed_values keys must be subset of features (TV bug prevention).
Source code in custom_components/sber_mqtt_bridge/sber_models.py
SberDevice
¶
Bases: BaseModel
Full device descriptor for Sber config publish.
Per Sber spec (VR-001), model_id and model are mutually exclusive.
This integration always uses inline model; model_id is not emitted.
partner_meta_max_size
classmethod
¶
partner_meta JSON must be under 1024 chars (VR-003).
Source code in custom_components/sber_mqtt_bridge/sber_models.py
SberDeviceState
¶
Bases: BaseModel
Current state of a single device.
SberConfigPayload
¶
Bases: BaseModel
Config publish payload (up/config topic).
SberStatusPayload
¶
Bases: BaseModel
Status publish payload (up/status topic).
SberCommandPayload
¶
Bases: BaseModel
Incoming command payload from Sber cloud (down/commands topic).
make_bool_value
¶
Create a Sber BOOL value dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
bool
|
Boolean value. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dict ready for inclusion in a Sber state payload. |
Source code in custom_components/sber_mqtt_bridge/sber_models.py
make_integer_value
¶
Create a Sber INTEGER value dict.
Per Sber C2C specification, integer_value is serialized as a string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
int
|
Integer value. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dict ready for inclusion in a Sber state payload. |
Source code in custom_components/sber_mqtt_bridge/sber_models.py
make_enum_value
¶
Create a Sber ENUM value dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
Enum string value. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dict ready for inclusion in a Sber state payload. |
Source code in custom_components/sber_mqtt_bridge/sber_models.py
make_colour_value
¶
Create a Sber COLOUR value dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
h
|
int
|
Hue component (0-360). |
required |
s
|
int
|
Saturation component (0-1000). |
required |
v
|
int
|
Value/brightness component (100-1000). |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dict ready for inclusion in a Sber state payload. |
Source code in custom_components/sber_mqtt_bridge/sber_models.py
make_state
¶
Create a Sber state entry dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
State key name. |
required |
value
|
dict[str, Any]
|
Typed value dict (from |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dict with |
Source code in custom_components/sber_mqtt_bridge/sber_models.py
validate_device
¶
Validate a single device dict against SberDevice schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device_data
|
dict[str, Any]
|
Raw device dict from |
required |
Returns:
| Type | Description |
|---|---|
tuple[bool, str]
|
Tuple |
Source code in custom_components/sber_mqtt_bridge/sber_models.py
validate_config_payload
¶
Validate a config payload dict against the SberConfigPayload schema.
This is an optional validation step — failures are logged as warnings but do not prevent publishing (the raw dict is still valid JSON).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Any]
|
Raw dict to validate. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if validation passed, False otherwise. |
Source code in custom_components/sber_mqtt_bridge/sber_models.py
validate_status_payload
¶
Validate a status payload dict against the SberStatusPayload schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Any]
|
Raw dict to validate. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if validation passed, False otherwise. |
Source code in custom_components/sber_mqtt_bridge/sber_models.py
missing_obligatory_features
¶
Return obligatory features missing from the emitted set.
Uses :data:CATEGORY_OBLIGATORY_FEATURES (auto-generated from Sber
docs ✔︎ markers). Missing obligatory features are a likely
cause of silent device rejection by Sber cloud.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
category
|
str
|
Sber category slug. |
required |
features
|
set[str]
|
Features our device would emit. |
required |
Returns:
| Type | Description |
|---|---|
set[str]
|
Set of obligatory features absent from |
set[str]
|
for unknown categories (fail-open — we don't block unknown cats). |
Source code in custom_components/sber_mqtt_bridge/sber_models.py
unknown_features_for_category
¶
Return features not present in Sber's reference model for the category.
Uses :data:CATEGORY_REFERENCE_FEATURES (auto-generated). Unknown
categories (not in our registry) return empty set — validation falls
back to general schema rules.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
category
|
str
|
Sber category slug. |
required |
features
|
set[str]
|
Features our device would emit. |
required |
Source code in custom_components/sber_mqtt_bridge/sber_models.py
validate_category_compliance
¶
Check a device descriptor for Sber category-specific violations.
Returns a list of human-readable violation messages (empty = compliant). Does NOT raise — callers decide how to handle violations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device
|
dict[str, Any]
|
Raw device dict (already passed SberDevice schema validation). |
required |