1 """The Dormakaba dKey integration."""
3 from __future__
import annotations
5 from datetime
import timedelta
8 from py_dormakaba_dkey
import DKEYLock
9 from py_dormakaba_dkey.errors
import DKEY_EXCEPTIONS, NotAssociated
10 from py_dormakaba_dkey.models
import AssociationData
20 from .const
import CONF_ASSOCIATION_DATA, DOMAIN, UPDATE_SECONDS
21 from .models
import DormakabaDkeyData
23 PLATFORMS: list[Platform] = [Platform.BINARY_SENSOR, Platform.LOCK, Platform.SENSOR]
25 _LOGGER = logging.getLogger(__name__)
29 """Set up Dormakaba dKey from a config entry."""
30 address: str = entry.data[CONF_ADDRESS]
31 ble_device = bluetooth.async_ble_device_from_address(hass, address.upper(),
True)
35 lock = DKEYLock(ble_device)
36 lock.set_association_data(
37 AssociationData.from_json(entry.data[CONF_ASSOCIATION_DATA])
41 def _async_update_ble(
42 service_info: bluetooth.BluetoothServiceInfoBleak,
43 change: bluetooth.BluetoothChange,
45 """Update from a ble callback."""
46 lock.set_ble_device_and_advertisement_data(
47 service_info.device, service_info.advertisement
50 entry.async_on_unload(
51 bluetooth.async_register_callback(
55 bluetooth.BluetoothScanningMode.PASSIVE,
59 async
def _async_update() -> None:
60 """Update the device state."""
63 await lock.disconnect()
64 except NotAssociated
as ex:
66 except DKEY_EXCEPTIONS
as ex:
74 update_method=_async_update,
75 update_interval=
timedelta(seconds=UPDATE_SECONDS),
77 await coordinator.async_config_entry_first_refresh()
83 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
86 """Close the connection."""
87 await lock.disconnect()
89 entry.async_on_unload(
90 hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _async_stop)
96 """Unload a config entry."""
97 if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
98 data: DormakabaDkeyData = hass.data[DOMAIN].pop(entry.entry_id)
99 await data.lock.disconnect()
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
None _async_stop(HomeAssistant hass, bool restart)