1 """The Gardena Bluetooth integration."""
3 from __future__
import annotations
7 from bleak.backends.device
import BLEDevice
8 from gardena_bluetooth.client
import CachedConnection, Client
9 from gardena_bluetooth.const
import DeviceConfiguration, DeviceInformation
10 from gardena_bluetooth.exceptions
import CommunicationFailure
20 from .const
import DOMAIN
21 from .coordinator
import DeviceUnavailable, GardenaBluetoothCoordinator
23 PLATFORMS: list[Platform] = [
24 Platform.BINARY_SENSOR,
31 LOGGER = logging.getLogger(__name__)
35 type GardenaBluetoothConfigEntry = ConfigEntry[GardenaBluetoothCoordinator]
39 """Set up a cached client that keeps connection after last use."""
41 def _device_lookup() -> BLEDevice:
42 device = bluetooth.async_ble_device_from_address(
43 hass, address, connectable=
True
49 return CachedConnection(DISCONNECT_DELAY, _device_lookup)
53 hass: HomeAssistant, entry: GardenaBluetoothConfigEntry
55 """Set up Gardena Bluetooth from a config entry."""
57 address = entry.data[CONF_ADDRESS]
60 sw_version = await client.read_char(DeviceInformation.firmware_version,
None)
61 manufacturer = await client.read_char(DeviceInformation.manufacturer_name,
None)
62 model = await client.read_char(DeviceInformation.model_number,
None)
63 name = await client.read_char(
64 DeviceConfiguration.custom_device_name, entry.title
66 uuids = await client.get_all_characteristics_uuid()
67 await client.update_timestamp(dt_util.now())
68 except (TimeoutError, CommunicationFailure, DeviceUnavailable)
as exception:
69 await client.disconnect()
71 f
"Unable to connect to device {address} due to {exception}"
75 identifiers={(DOMAIN, address)},
77 sw_version=sw_version,
78 manufacturer=manufacturer,
83 hass, LOGGER, client, uuids, device, address
86 entry.runtime_data = coordinator
87 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
88 await coordinator.async_refresh()
94 hass: HomeAssistant, entry: GardenaBluetoothConfigEntry
96 """Unload a config entry."""
97 if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
98 await entry.runtime_data.async_shutdown()
bool async_setup_entry(HomeAssistant hass, GardenaBluetoothConfigEntry entry)
bool async_unload_entry(HomeAssistant hass, GardenaBluetoothConfigEntry entry)
CachedConnection get_connection(HomeAssistant hass, str address)