Skip to content

Commit

Permalink
Migrated to new Home Assistant Bluetooth API
Browse files Browse the repository at this point in the history
  • Loading branch information
ClusterM committed Aug 13, 2022
1 parent b296437 commit d49ee0a
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 180 deletions.
2 changes: 1 addition & 1 deletion custom_components/skykettle/__init__.py
Expand Up @@ -37,7 +37,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
config[CONF_FRIENDLY_NAME] = config[CONF_FRIENDLY_NAME] + 'S'
hass.config_entries.async_update_entry(entry, data=config)
_LOGGER.info(f"Fixed invalid model name: {config[CONF_FRIENDLY_NAME][:-1]} -> {config[CONF_FRIENDLY_NAME]}")

kettle = KettleConnection(
mac=entry.data[CONF_MAC],
key=entry.data[CONF_PASSWORD],
Expand Down
68 changes: 13 additions & 55 deletions custom_components/skykettle/config_flow.py
Expand Up @@ -4,7 +4,9 @@
import secrets
import traceback
import sys
import asyncio
import subprocess
from homeassistant.components import bluetooth
import voluptuous as vol
from homeassistant.const import *
import homeassistant.helpers.config_validation as cv
Expand Down Expand Up @@ -50,56 +52,8 @@ async def init_mac(self, mac):

async def async_step_user(self, user_input=None):
"""Handle the user step."""
# Check OS
if sys.platform != "linux":
return self.async_abort(reason='linux_not_found')
# Test binaries
try:
subprocess.Popen(["timeout"], shell=False).kill()
except FileNotFoundError:
_LOGGER.error(traceback.format_exc())
return self.async_abort(reason='timeout_not_found')
try:
subprocess.Popen(["gatttool"], shell=False).kill()
except FileNotFoundError:
_LOGGER.error(traceback.format_exc())
return self.async_abort(reason='gatttool_not_found')
try:
subprocess.Popen(["hcitool"], shell=False).kill()
except FileNotFoundError:
_LOGGER.error(traceback.format_exc())
return self.async_abort(reason='hcitool_not_found')
return await self.async_step_select_adapter()

async def async_step_select_adapter(self, user_input=None):
"""Handle the select_adapter step."""
errors = {}
if user_input is not None:
spl = user_input[CONF_DEVICE].split(' ', maxsplit=1)
name = None
if spl[0] != "auto": name = spl[0]
self.config[CONF_DEVICE] = name
# Continue to scan
return await self.async_step_scan_message()

try:
adapters = await ble_get_adapters()
_LOGGER.debug(f"Adapters: {adapters}")
adapters_list = [f"{r.name} ({r.mac})" for r in adapters]
adapters_list = ["auto"] + adapters_list # Auto
schema = vol.Schema(
{
vol.Required(CONF_DEVICE): vol.In(adapters_list)
})
except Exception:
_LOGGER.error(traceback.format_exc())
return self.async_abort(reason='unknown')
return self.async_show_form(
step_id="select_adapter",
errors=errors,
data_schema=schema
)

return await self.async_step_scan_message()

async def async_step_scan_message(self, user_input=None):
"""Handle the scan_message step."""
if user_input is not None:
Expand Down Expand Up @@ -127,12 +81,16 @@ async def async_step_scan(self, user_input=None):
return await self.async_step_connect()

try:
macs = await ble_scan(self.config.get(CONF_DEVICE, None), scan_time=BLE_SCAN_TIME)
_LOGGER.debug(f"Scan result: {macs}")
macs_filtered = [mac for mac in macs if mac.name and (mac.name.startswith("RK-") or mac.name.startswith("RFS-"))]
if len(macs_filtered) == 0:
scanner = bluetooth.async_get_scanner(self.hass)
await scanner.start()
await asyncio.sleep(5)
await scanner.stop()
for device in scanner.discovered_devices:
_LOGGER.debug(f"Device found: {device.address} - {device.name}")
devices_filtered = [device for device in scanner.discovered_devices if device.name and (device.name.startswith("RK-") or device.name.startswith("RFS-"))]
if len(devices_filtered) == 0:
return self.async_abort(reason='kettle_not_found')
mac_list = [f"{r.mac} ({r.name})" for r in macs_filtered]
mac_list = [f"{r.address} ({r.name})" for r in devices_filtered]
schema = vol.Schema(
{
vol.Required(CONF_MAC): vol.In(mac_list)
Expand Down
2 changes: 0 additions & 2 deletions custom_components/skykettle/const.py
Expand Up @@ -5,8 +5,6 @@
MANUFACTORER = "Redmond"
SUGGESTED_AREA = "kitchen"

REGEX_MAC = r"^(([0-9a-fA-F]){2}[:-]?){5}[0-9a-fA-F]{2}$"

CONF_PERSISTENT_CONNECTION = "persistent_connection"

DEFAULT_SCAN_INTERVAL = 5
Expand Down

0 comments on commit d49ee0a

Please sign in to comment.