Skip to content

Commit

Permalink
fix error in config_entry data
Browse files Browse the repository at this point in the history
fix error in config_entry data
  • Loading branch information
dave-code-ruiz authored Aug 22, 2023
1 parent 9f8d7d3 commit f349b6d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 34 deletions.
3 changes: 1 addition & 2 deletions custom_components/elkbledom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 4 additions & 5 deletions custom_components/elkbledom/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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()
Expand Down Expand Up @@ -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)

Expand All @@ -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"]:
Expand Down Expand Up @@ -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:
Expand Down
56 changes: 29 additions & 27 deletions custom_components/elkbledom/elkbledom.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit f349b6d

Please sign in to comment.