1 """Support for Xiaomi event entities."""
3 from __future__
import annotations
5 from dataclasses
import replace
10 EventEntityDescription,
17 from .
import format_discovered_event_class, format_event_dispatcher_name
24 EVENT_CLASS_FINGERPRINT,
31 from .types
import XiaomiBLEConfigEntry
33 DESCRIPTIONS_BY_EVENT_CLASS = {
35 key=EVENT_CLASS_BUTTON,
36 translation_key=
"button",
42 device_class=EventDeviceClass.BUTTON,
46 translation_key=
"cube",
53 key=EVENT_CLASS_DIMMER,
54 translation_key=
"dimmer",
60 "rotate_left_pressed",
61 "rotate_right_pressed",
65 key=EVENT_CLASS_ERROR,
66 translation_key=
"error",
68 "frequent_unlocking_with_incorrect_password",
69 "frequent_unlocking_with_wrong_fingerprints",
70 "operation_timeout_password_input_timeout",
72 "reset_button_is_pressed",
73 "the_wrong_key_is_frequently_unlocked",
74 "foreign_body_in_the_keyhole",
75 "the_key_has_not_been_taken_out",
76 "error_nfc_frequently_unlocks",
77 "timeout_is_not_locked_as_required",
78 "failure_to_unlock_frequently_in_multiple_ways",
79 "unlocking_the_face_frequently_fails",
80 "failure_to_unlock_the_vein_frequently",
82 "unlock_inside_the_door_after_arming",
83 "palmprints_frequently_fail_to_unlock",
85 "the_battery_level_is_less_than_10_percent",
86 "the_battery_is_less_than_5_percent",
87 "the_fingerprint_sensor_is_abnormal",
88 "the_accessory_battery_is_low",
90 "the_lock_sensor_is_faulty",
94 key=EVENT_CLASS_FINGERPRINT,
95 translation_key=
"fingerprint",
99 "low_quality_too_light_fuzzy",
106 key=EVENT_CLASS_LOCK,
107 translation_key=
"lock",
109 "lock_outside_the_door",
110 "unlock_outside_the_door",
111 "lock_inside_the_door",
112 "unlock_inside_the_door",
115 "release_the_antilock",
116 "turn_on_child_lock",
117 "turn_off_child_lock",
122 key=EVENT_CLASS_MOTION,
123 translation_key=
"motion",
124 event_types=[
"motion_detected"],
125 device_class=EventDeviceClass.MOTION,
131 """Representation of a Xiaomi event entity."""
133 _attr_should_poll =
False
134 _attr_has_entity_name =
True
140 event: XiaomiBleEvent |
None,
142 """Initialise a Xiaomi event entity."""
147 base_event_class, _, postfix = event_class.partition(
"_")
148 base_description = DESCRIPTIONS_BY_EVENT_CLASS[base_event_class]
150 postfix_name = f
" {postfix}" if postfix
else ""
151 self.
_attr_name_attr_name = f
"{base_event_class.title()}{postfix_name}"
154 identifiers={(DOMAIN, address)},
155 connections={(dr.CONNECTION_BLUETOOTH, address)},
164 self.
_trigger_event_trigger_event(event[EVENT_TYPE], event[EVENT_PROPERTIES])
167 """Entity added to hass."""
179 self.
_trigger_event_trigger_event(event[EVENT_TYPE], event[EVENT_PROPERTIES])
185 entry: XiaomiBLEConfigEntry,
186 async_add_entities: AddEntitiesCallback,
188 """Set up Xiaomi event."""
189 coordinator = entry.runtime_data
190 address = coordinator.address
191 ent_reg = er.async_get(hass)
195 for ent_reg_entry
in er.async_entries_for_config_entry(ent_reg, entry.entry_id)
196 if ent_reg_entry.domain ==
"event"
197 and (address_event_class := ent_reg_entry.unique_id.partition(
"-"))
201 def _async_discovered_event_class(event_class: str, event: XiaomiBleEvent) ->
None:
202 """Handle a newly discovered event class with or without a postfix."""
205 entry.async_on_unload(
209 _async_discovered_event_class,
None _trigger_event(self, str event_type, dict[str, Any]|None event_attributes=None)
None async_added_to_hass(self)
None _async_handle_event(self, XiaomiBleEvent event)
None __init__(self, str address, str event_class, XiaomiBleEvent|None event)
None async_write_ha_state(self)
None async_on_remove(self, CALLBACK_TYPE func)
None async_setup_entry(HomeAssistant hass, XiaomiBLEConfigEntry entry, AddEntitiesCallback async_add_entities)
str format_discovered_event_class(str address)
str format_event_dispatcher_name(str address, str event_class)
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)