Базовые классы¶
BaseEntity¶
Абстрактный базовый класс для всех устройств Sber Smart Home.
Base entity class for Sber Smart Home device representations.
All device types (light, relay, climate, etc.) inherit from BaseEntity. It defines the contract for converting between HA states and Sber JSON protocol.
ROLE_BATTERY
module-attribute
¶
ROLE_BATTERY = LinkableRole('battery', frozenset({'sensor'}), frozenset({'battery'}))
Battery percentage sensor (sensor domain, battery device_class).
ROLE_BATTERY_LOW
module-attribute
¶
ROLE_BATTERY_LOW = LinkableRole('battery_low', frozenset({'binary_sensor'}), frozenset({'battery'}))
Low-battery binary sensor (binary_sensor domain, battery device_class).
ROLE_SIGNAL
module-attribute
¶
ROLE_SIGNAL = LinkableRole('signal_strength', frozenset({'sensor'}), frozenset({'signal_strength'}))
Signal strength sensor (sensor domain, signal_strength device_class).
ROLE_TEMPERATURE
module-attribute
¶
ROLE_TEMPERATURE = LinkableRole('temperature', frozenset({'sensor'}), frozenset({'temperature'}))
Temperature sensor (sensor domain, temperature device_class).
ROLE_HUMIDITY
module-attribute
¶
ROLE_HUMIDITY = LinkableRole('humidity', frozenset({'sensor'}), frozenset({'humidity'}))
Humidity sensor (sensor domain, humidity device_class).
SENSOR_LINK_ROLES
module-attribute
¶
SENSOR_LINK_ROLES = (ROLE_BATTERY, ROLE_BATTERY_LOW, ROLE_SIGNAL)
Common linkable roles for battery-powered devices (sensors, covers, valves).
ALL_LINKABLE_ROLES
module-attribute
¶
ALL_LINKABLE_ROLES = (ROLE_BATTERY, ROLE_BATTERY_LOW, ROLE_SIGNAL, ROLE_TEMPERATURE, ROLE_HUMIDITY)
Global registry of all known linkable roles for display in UI.
LinkableRole
dataclass
¶
Describes a linkable sensor role that a device class accepts.
Each role declares which HA domain + device_class combinations it matches.
Device classes declare which roles they accept via LINKABLE_ROLES.
This eliminates the need for separate mapping dicts and domain overrides.
Attributes:
| Name | Type | Description |
|---|---|---|
role |
str
|
Link role name (e.g. |
domains |
frozenset[str]
|
Accepted HA entity domains (e.g. |
device_classes |
frozenset[str]
|
Accepted HA device_class values (e.g. |
matches
¶
Check if an HA entity matches this role.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
domain
|
str
|
HA entity domain (e.g. |
required |
device_class
|
str
|
HA original_device_class (e.g. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if both domain and device_class match. |
Source code in custom_components/sber_mqtt_bridge/devices/base_entity.py
BaseEntity
¶
Bases: ABC
Abstract base class for all Sber device entities.
Defines the interface that all device types must implement: - fill_by_ha_state: Parse HA state into internal representation - create_features_list: Return Sber feature names - to_sber_state: Build Sber device config JSON - to_sber_current_state: Build Sber current state JSON - process_cmd: Handle Sber commands, return HA service calls - process_state_change: Handle HA state change events
Initialize base entity from HA entity registry data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
category
|
str
|
Sber device category (e.g., 'light', 'relay', 'sensor_temp'). |
required |
entity_data
|
dict
|
Dict with HA entity registry fields. |
required |
Source code in custom_components/sber_mqtt_bridge/devices/base_entity.py
LINKABLE_ROLES
class-attribute
¶
Linkable roles this device class accepts. Override in subclasses.
effective_room
property
¶
Return the best available room name.
Priority: entity area_id → device area_id → empty string.
is_online
property
¶
Public accessor for entity online status.
Returns:
| Type | Description |
|---|---|
bool
|
True if the entity state indicates it is reachable. |
fill_by_ha_state
¶
Parse HA state dict and update internal state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ha_entity_state
|
dict
|
Dict with 'state' and 'attributes' keys from HA. |
required |
Source code in custom_components/sber_mqtt_bridge/devices/base_entity.py
is_group_state
¶
Check if this entity represents a group of other entities.
create_features_list
¶
Return list of Sber feature names supported by this entity.
Base implementation returns ['online']. Child classes extend this.
create_allowed_values_list
¶
Return allowed values map for Sber model descriptor.
Override in subclasses to provide allowed_values for features that require INTEGER ranges or ENUM value lists.
Returns:
| Type | Description |
|---|---|
dict[str, dict]
|
Dict mapping feature key to its allowed values descriptor, |
dict[str, dict]
|
or empty dict if no allowed values needed. |
Source code in custom_components/sber_mqtt_bridge/devices/base_entity.py
create_dependencies
¶
Return feature dependencies map for Sber model descriptor.
Override in subclasses to declare feature dependencies (e.g., light_colour depends on light_mode == 'colour').
Returns:
| Type | Description |
|---|---|
dict[str, dict]
|
Dict mapping feature key to its dependency descriptor, |
dict[str, dict]
|
or empty dict if no dependencies needed. |
Source code in custom_components/sber_mqtt_bridge/devices/base_entity.py
get_final_features_list
¶
Return features list with user overrides applied.
Removes features from removed_features and appends features
from extra_features. Duplicate-safe.
Returns:
| Type | Description |
|---|---|
list[str]
|
Final list of Sber feature names. |
Source code in custom_components/sber_mqtt_bridge/devices/base_entity.py
link_device
¶
Link this entity to a HA device registry entry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device_data
|
DeviceData
|
Device registry data dict. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If device_id does not match. |
Source code in custom_components/sber_mqtt_bridge/devices/base_entity.py
to_sber_state
¶
Build Sber device config JSON for MQTT publish.
Returns:
| Type | Description |
|---|---|
dict
|
Dict with device descriptor for Sber (id, name, room, model, features). |
dict
|
Optionally includes nicknames, groups, parent_id, and partner_meta |
dict
|
when configured. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If fill_by_ha_state was not called first. |
RuntimeError
|
If device has device_id but linked_device is not set. |
Source code in custom_components/sber_mqtt_bridge/devices/base_entity.py
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 | |
to_sber_current_state
abstractmethod
¶
Build Sber current state JSON for MQTT publish.
Returns:
| Type | Description |
|---|---|
dict
|
Dict with entity_id key mapping to {'states': [...]}. |
get_entity_domain
¶
Extract HA domain from entity_id.
Returns:
| Type | Description |
|---|---|
str
|
Domain string (e.g., 'climate' from 'climate.living_room'). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If entity_id has invalid format. |
Source code in custom_components/sber_mqtt_bridge/devices/base_entity.py
process_cmd
abstractmethod
¶
Process a command from Sber cloud.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cmd_data
|
dict
|
Command payload with 'states' list, or None. |
required |
Returns:
| Type | Description |
|---|---|
list[dict]
|
List of dicts with 'url' key containing HA service call descriptors, |
list[dict]
|
or empty list if no action needed or cmd_data is None. |
Source code in custom_components/sber_mqtt_bridge/devices/base_entity.py
process_state_change
¶
Handle a state change event from Home Assistant.
Default implementation refreshes internal state via fill_by_ha_state. Override in subclasses if additional processing is needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
old_state
|
dict | None
|
Previous HA state dict (may be None). |
required |
new_state
|
dict
|
New HA state dict. |
required |
Source code in custom_components/sber_mqtt_bridge/devices/base_entity.py
has_significant_change
¶
Check if current Sber state differs from last published state.
Used to avoid unnecessary MQTT publishes when only non-relevant HA attributes changed (e.g., last_updated, icon, etc.).
Returns:
| Type | Description |
|---|---|
bool
|
True if the state has changed and should be published. |
Source code in custom_components/sber_mqtt_bridge/devices/base_entity.py
mark_state_published
¶
Snapshot current Sber state as the last published state.
Called after successful MQTT publish to enable value diffing.
Source code in custom_components/sber_mqtt_bridge/devices/base_entity.py
resolve_link_role
¶
Determine the link role for an HA entity based on domain and device_class.
Iterates ALL_LINKABLE_ROLES and returns the role name of the first match.
Domain-aware disambiguation is built into the role definitions:
e.g. sensor + battery → battery, binary_sensor + battery
→ battery_low.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
domain
|
str
|
HA entity domain. |
required |
device_class
|
str
|
HA original_device_class. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Role name string, or empty string if no match. |
Source code in custom_components/sber_mqtt_bridge/devices/base_entity.py
OnOffEntity¶
Базовый класс для устройств с on/off состоянием (реле, розетки, клапаны).
Base class for Sber on/off entities (relay, socket).
Provides shared implementations of fill_by_ha_state, create_features_list,
and to_sber_current_state for devices that expose a simple on/off state
via the Sber on_off feature.
Supports optional power, voltage, and current features when the
HA entity reports those values via attributes.
OnOffEntity
¶
Bases: BaseEntity
Base class for on/off entities that expose the Sber 'on_off' feature.
Subclasses must implement process_cmd to map Sber on/off commands
to the appropriate HA service calls (e.g., turn_on/turn_off
for relays).
Subclasses may override _ha_on_state if the HA 'on' state string
differs from the default "on".
Optionally reports power, voltage, and current when
the HA entity has those attributes.
Initialize on/off entity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
category
|
str
|
Sber device category string. |
required |
entity_data
|
dict
|
HA entity registry dict containing entity metadata. |
required |
Source code in custom_components/sber_mqtt_bridge/devices/on_off_entity.py
fill_by_ha_state
¶
Parse HA state and update on/off status, energy, and child_lock attributes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ha_state
|
dict
|
HA state dict with 'state' and 'attributes' keys. |
required |
Source code in custom_components/sber_mqtt_bridge/devices/on_off_entity.py
create_features_list
¶
Return Sber feature list including 'on_off' and optional features.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of Sber feature strings supported by this entity. |
Source code in custom_components/sber_mqtt_bridge/devices/on_off_entity.py
to_sber_current_state
¶
Build Sber current state payload with online, on_off, energy, and child_lock.
Returns:
| Type | Description |
|---|---|
dict[str, dict]
|
Dict mapping entity_id to its Sber state representation. |
Source code in custom_components/sber_mqtt_bridge/devices/on_off_entity.py
SimpleSensor¶
Базовый класс для read-only сенсоров.
Base class for read-only Sber sensors with a single value feature.
Provides shared implementations of process_cmd,
create_features_list, and to_sber_current_state so that concrete
sensor subclasses only need to define how their value is extracted and
formatted for the Sber protocol.
Supports optional battery_percentage feature when the HA entity
reports battery level via attributes.
SimpleReadOnlySensor
¶
Bases: BaseEntity
Base class for read-only sensors that expose a single Sber feature.
Subclasses must define the class-level attributes _sber_value_key
and _sber_value_type, and implement _get_sber_value to return
the current sensor value in the appropriate Sber format.
Optionally reports battery_percentage when the HA entity has
a battery or battery_level attribute.
Initialize simple read-only sensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
category
|
str
|
Sber device category string. |
required |
entity_data
|
dict
|
HA entity registry dict containing entity metadata. |
required |
Source code in custom_components/sber_mqtt_bridge/devices/simple_sensor.py
update_linked_data
¶
Inject data from a linked entity into this sensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
role
|
str
|
Link role name (battery, battery_low, signal_strength, humidity, temperature). |
required |
ha_state
|
dict
|
HA state dict with 'state' and 'attributes'. |
required |
Source code in custom_components/sber_mqtt_bridge/devices/simple_sensor.py
fill_by_ha_state
¶
Parse HA state and update internal state including battery and signal.
Reads battery level from battery or battery_level attribute.
Reads signal strength from signal_strength, rssi, or
linkquality attribute.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ha_state
|
dict
|
HA state dict with 'state' and 'attributes' keys. |
required |
Source code in custom_components/sber_mqtt_bridge/devices/simple_sensor.py
create_features_list
¶
Return Sber feature list including the sensor's value key.
Adds battery_percentage and battery_low_power if battery
level is available. Adds signal_strength if signal data is present.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of Sber feature strings supported by this entity. |
Source code in custom_components/sber_mqtt_bridge/devices/simple_sensor.py
to_sber_current_state
¶
Build Sber current state payload with online, value, battery, and signal keys.
Returns:
| Type | Description |
|---|---|
dict[str, dict]
|
Dict mapping entity_id to its Sber state representation. |
Source code in custom_components/sber_mqtt_bridge/devices/simple_sensor.py
process_cmd
¶
Process Sber command (no-op for read-only sensor).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cmd_data
|
dict
|
Sber command dict (ignored). |
required |
Returns:
| Type | Description |
|---|---|
list[dict]
|
Empty list -- sensors do not accept commands. |