1 """The melnor integration."""
3 from __future__
import annotations
5 from melnor_bluetooth.device
import Device
14 from .const
import DOMAIN
15 from .coordinator
import MelnorDataUpdateCoordinator
17 PLATFORMS: list[Platform] = [
26 """Set up melnor from a config entry."""
28 hass.data.setdefault(DOMAIN, {}).setdefault(entry.entry_id, {})
30 ble_device = bluetooth.async_ble_device_from_address(hass, entry.data[CONF_ADDRESS])
34 f
"Couldn't find a nearby device for address: {entry.data[CONF_ADDRESS]}"
39 device =
Device(ble_device)
40 await device.connect(retry_attempts=4)
42 if not device.is_connected:
46 def _async_update_ble(
47 service_info: bluetooth.BluetoothServiceInfoBleak,
48 change: bluetooth.BluetoothChange,
50 """Update from a ble callback."""
51 device.update_ble_device(service_info.device)
53 bluetooth.async_register_callback(
57 bluetooth.BluetoothScanningMode.PASSIVE,
61 await coordinator.async_config_entry_first_refresh()
63 hass.data[DOMAIN][entry.entry_id] = coordinator
64 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
70 """Unload a config entry."""
72 device: Device = hass.data[DOMAIN][entry.entry_id].data
74 await device.disconnect()
76 if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
77 hass.data[DOMAIN].pop(entry.entry_id)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)