diff --git a/custom_components/elkbledom/__init__.py b/custom_components/elkbledom/__init__.py index 000dd03..61b29a3 100644 --- a/custom_components/elkbledom/__init__.py +++ b/custom_components/elkbledom/__init__.py @@ -15,10 +15,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up ElkBLEDOM from a config entry.""" reset = entry.options.get(CONF_RESET, None) or entry.data.get(CONF_RESET, None) delay = entry.options.get(CONF_DELAY, None) or entry.data.get(CONF_DELAY, None) - devices = entry.data.get("devices", None) LOGGER.debug("Config Reset data: %s and config delay data: %s", reset, delay) - instance = BLEDOMInstance(entry.data[CONF_MAC], reset, delay, devices, hass) + instance = BLEDOMInstance(entry.data[CONF_MAC], reset, delay, hass) hass.data.setdefault(DOMAIN, {})[entry.entry_id] = instance await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) diff --git a/custom_components/elkbledom/config_flow.py b/custom_components/elkbledom/config_flow.py index 19db088..79a95bf 100644 --- a/custom_components/elkbledom/config_flow.py +++ b/custom_components/elkbledom/config_flow.py @@ -32,7 +32,6 @@ def __init__(self) -> None: self._instance = None self.name = None self._discovery_info: BluetoothServiceInfoBleak | None = None - self._discovered_device: DeviceData | None = None self._discovered_devices = [] async def async_step_bluetooth( @@ -42,7 +41,7 @@ async def async_step_bluetooth( LOGGER.debug("Discovered bluetooth devices, step bluetooth, : %s , %s", discovery_info.address, discovery_info.name) await self.async_set_unique_id(discovery_info.address) self._abort_if_unique_id_configured() - device = DeviceData(discovery_info) + device = DeviceData(self.hass, discovery_info) if device.is_supported: self._discovered_devices.append(device) return await self.async_step_bluetooth_confirm() @@ -79,7 +78,7 @@ async def async_step_user( if (device for device in self._discovered_devices if device.address == self.mac) != ([]): LOGGER.debug("Device with address %s in discovered_devices", self.mac) continue - device = DeviceData(discovery_info) + device = DeviceData(self.hass, discovery_info) if device.is_supported: self._discovered_devices.append(device) @@ -104,7 +103,7 @@ async def async_step_validate(self, user_input: "dict[str, Any] | None" = None): if user_input is not None: if "flicker" in user_input: if user_input["flicker"]: - return self.async_create_entry(title=self.name, data={CONF_MAC: self.mac, "name": self.name, "devices": self._discovered_devices}) + return self.async_create_entry(title=self.name, data={CONF_MAC: self.mac, "name": self.name}) return self.async_abort(reason="cannot_validate") if "retry" in user_input and not user_input["retry"]: @@ -144,7 +143,7 @@ async def async_step_manual(self, user_input: "dict[str, Any] | None" = None): async def toggle_light(self): if not self._instance: - self._instance = BLEDOMInstance(self.mac, False, 120, self._discovered_devices, self.hass) + self._instance = BLEDOMInstance(self.mac, False, 120, self.hass) try: await self._instance.update() if self._instance.is_on: diff --git a/custom_components/elkbledom/elkbledom.py b/custom_components/elkbledom/elkbledom.py index 03e6a0b..a4fe636 100644 --- a/custom_components/elkbledom/elkbledom.py +++ b/custom_components/elkbledom/elkbledom.py @@ -12,7 +12,7 @@ BleakNotFoundError, establish_connection, ) -from homeassistant.components.bluetooth import BluetoothServiceInfoBleak +from homeassistant.components.bluetooth import async_discovered_service_info, async_ble_device_from_address from home_assistant_bluetooth import BluetoothServiceInfo from bluetooth_sensor_state_data import BluetoothData from typing import Any, TypeVar, cast, Tuple @@ -126,12 +126,26 @@ async def _async_wrap_retry_bluetooth_connection_error( return cast(WrapFuncType, _async_wrap_retry_bluetooth_connection_error) class DeviceData(BluetoothData): - def __init__(self, discovery_info): + def __init__(self, hass, discovery_info): self._discovery = discovery_info self._supported = self._discovery.name.lower().startswith("elk-ble") or self._discovery.name.lower().startswith("elk-bulb") or self._discovery.name.lower().startswith("ledble") or self._discovery.name.lower().startswith("melk") self._address = self._discovery.address self._name = self._discovery.name self._rssi = self._discovery.rssi + self._hass = hass + self._bledevice = async_ble_device_from_address(hass, self._address) + # try: + # discovered_devices_and_advertisement_data = await BleakScanner.discover(return_adv=True) + # for device, adv_data in discovered_devices_and_advertisement_data.values(): + # if device.address == address: + # self._bledevice = device + # self._adv_data = adv_data + # except (Exception) as error: + # LOGGER.warning("Warning getting device: %s", error) + # self._bledevice = bluetooth.async_ble_device_from_address(self._hass, address) + # if not self._bledevice: + # raise ConfigEntryNotReady(f"You need to add bluetooth integration (https://www.home-assistant.io/integrations/bluetooth) or couldn't find a nearby device with address: {address}") + # def __init__(self, *args): # if isinstance(args[0], BluetoothServiceInfoBleak): @@ -165,30 +179,32 @@ def name(self): @property def rssi(self): return self._rssi + + def bledevice(self) -> BLEDevice: + return self._bledevice def _start_update(self, service_info: BluetoothServiceInfo) -> None: """Update from BLE advertisement data.""" LOGGER.debug("Parsing Govee BLE advertisement data: %s", service_info) class BLEDOMInstance: - def __init__(self, address, reset: bool, delay: int, devices, hass) -> None: + def __init__(self, address, reset: bool, delay: int, hass) -> None: self.loop = asyncio.get_running_loop() self._address = address self._reset = reset self._delay = delay self._hass = hass self._device: BLEDevice | None = None - self._device_data = None - if devices is not None: - for device in devices: - # device = DeviceData(device) + self._device_data: DeviceData | None = None + + for discovery_info in async_discovered_service_info(hass): + if discovery_info.address == address: + device = DeviceData(hass, discovery_info) LOGGER.debug("device %s: %s %s",device.name, device.address, device.rssi) - if device.address == address: + if device.is_supported: self._device_data = device + # self._adv_data: AdvertisementData | None = None - #self._device = bluetooth.async_ble_device_from_address(self._hass, address) - #if not self._device: - # raise ConfigEntryNotReady(f"You need to add bluetooth integration (https://www.home-assistant.io/integrations/bluetooth) or couldn't find a nearby device with address: {address}") self._connect_lock: asyncio.Lock = asyncio.Lock() self._client: BleakClientWithServiceCache | None = None self._disconnect_timer: asyncio.TimerHandle | None = None @@ -359,23 +375,9 @@ async def update(self): LOGGER.debug(track) async def _getdevice(self, address) -> None: - """Get device and advertisement data from discovered device""" - - # try: - # discovered_devices_and_advertisement_data = await BleakScanner.discover(return_adv=True) - # for device, adv_data in discovered_devices_and_advertisement_data.values(): - # if device.address == address: - # self._device = device - # self._adv_data = adv_data - # except (Exception) as error: - # LOGGER.warning("Warning getting device: %s", error) - # self._device = bluetooth.async_ble_device_from_address(self._hass, address) - # if not self._device: - # raise ConfigEntryNotReady(f"You need to add bluetooth integration (https://www.home-assistant.io/integrations/bluetooth) or couldn't find a nearby device with address: {address}") - try: - - self._device = bluetooth.async_ble_device_from_address(self._hass, address) + self._device = self._device_data.bledevice() + #self._device = bluetooth.async_ble_device_from_address(self._hass, address) except (Exception) as error: LOGGER.warning("Warning getting device: %s", error) if not self._device: