Move imports in flic component (#27821)
This commit is contained in:
parent
3a608314c4
commit
fe036ed094
1 changed files with 19 additions and 21 deletions
|
@ -2,6 +2,14 @@
|
||||||
import logging
|
import logging
|
||||||
import threading
|
import threading
|
||||||
|
|
||||||
|
from pyflic import (
|
||||||
|
FlicClient,
|
||||||
|
ButtonConnectionChannel,
|
||||||
|
ClickType,
|
||||||
|
ConnectionStatus,
|
||||||
|
ScanWizard,
|
||||||
|
ScanWizardResult,
|
||||||
|
)
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
@ -49,7 +57,6 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||||
|
|
||||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||||
"""Set up the flic platform."""
|
"""Set up the flic platform."""
|
||||||
import pyflic
|
|
||||||
|
|
||||||
# Initialize flic client responsible for
|
# Initialize flic client responsible for
|
||||||
# connecting to buttons and retrieving events
|
# connecting to buttons and retrieving events
|
||||||
|
@ -58,7 +65,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||||
discovery = config.get(CONF_DISCOVERY)
|
discovery = config.get(CONF_DISCOVERY)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
client = pyflic.FlicClient(host, port)
|
client = FlicClient(host, port)
|
||||||
except ConnectionRefusedError:
|
except ConnectionRefusedError:
|
||||||
_LOGGER.error("Failed to connect to flic server")
|
_LOGGER.error("Failed to connect to flic server")
|
||||||
return
|
return
|
||||||
|
@ -88,15 +95,13 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||||
|
|
||||||
def start_scanning(config, add_entities, client):
|
def start_scanning(config, add_entities, client):
|
||||||
"""Start a new flic client for scanning and connecting to new buttons."""
|
"""Start a new flic client for scanning and connecting to new buttons."""
|
||||||
import pyflic
|
scan_wizard = ScanWizard()
|
||||||
|
|
||||||
scan_wizard = pyflic.ScanWizard()
|
|
||||||
|
|
||||||
def scan_completed_callback(scan_wizard, result, address, name):
|
def scan_completed_callback(scan_wizard, result, address, name):
|
||||||
"""Restart scan wizard to constantly check for new buttons."""
|
"""Restart scan wizard to constantly check for new buttons."""
|
||||||
if result == pyflic.ScanWizardResult.WizardSuccess:
|
if result == ScanWizardResult.WizardSuccess:
|
||||||
_LOGGER.info("Found new button %s", address)
|
_LOGGER.info("Found new button %s", address)
|
||||||
elif result != pyflic.ScanWizardResult.WizardFailedTimeout:
|
elif result != ScanWizardResult.WizardFailedTimeout:
|
||||||
_LOGGER.warning(
|
_LOGGER.warning(
|
||||||
"Failed to connect to button %s. Reason: %s", address, result
|
"Failed to connect to button %s. Reason: %s", address, result
|
||||||
)
|
)
|
||||||
|
@ -123,7 +128,6 @@ class FlicButton(BinarySensorDevice):
|
||||||
|
|
||||||
def __init__(self, hass, client, address, timeout, ignored_click_types):
|
def __init__(self, hass, client, address, timeout, ignored_click_types):
|
||||||
"""Initialize the flic button."""
|
"""Initialize the flic button."""
|
||||||
import pyflic
|
|
||||||
|
|
||||||
self._hass = hass
|
self._hass = hass
|
||||||
self._address = address
|
self._address = address
|
||||||
|
@ -131,10 +135,10 @@ class FlicButton(BinarySensorDevice):
|
||||||
self._is_down = False
|
self._is_down = False
|
||||||
self._ignored_click_types = ignored_click_types or []
|
self._ignored_click_types = ignored_click_types or []
|
||||||
self._hass_click_types = {
|
self._hass_click_types = {
|
||||||
pyflic.ClickType.ButtonClick: CLICK_TYPE_SINGLE,
|
ClickType.ButtonClick: CLICK_TYPE_SINGLE,
|
||||||
pyflic.ClickType.ButtonSingleClick: CLICK_TYPE_SINGLE,
|
ClickType.ButtonSingleClick: CLICK_TYPE_SINGLE,
|
||||||
pyflic.ClickType.ButtonDoubleClick: CLICK_TYPE_DOUBLE,
|
ClickType.ButtonDoubleClick: CLICK_TYPE_DOUBLE,
|
||||||
pyflic.ClickType.ButtonHold: CLICK_TYPE_HOLD,
|
ClickType.ButtonHold: CLICK_TYPE_HOLD,
|
||||||
}
|
}
|
||||||
|
|
||||||
self._channel = self._create_channel()
|
self._channel = self._create_channel()
|
||||||
|
@ -142,9 +146,7 @@ class FlicButton(BinarySensorDevice):
|
||||||
|
|
||||||
def _create_channel(self):
|
def _create_channel(self):
|
||||||
"""Create a new connection channel to the button."""
|
"""Create a new connection channel to the button."""
|
||||||
import pyflic
|
channel = ButtonConnectionChannel(self._address)
|
||||||
|
|
||||||
channel = pyflic.ButtonConnectionChannel(self._address)
|
|
||||||
channel.on_button_up_or_down = self._on_up_down
|
channel.on_button_up_or_down = self._on_up_down
|
||||||
|
|
||||||
# If all types of clicks should be ignored, skip registering callbacks
|
# If all types of clicks should be ignored, skip registering callbacks
|
||||||
|
@ -212,12 +214,10 @@ class FlicButton(BinarySensorDevice):
|
||||||
|
|
||||||
def _on_up_down(self, channel, click_type, was_queued, time_diff):
|
def _on_up_down(self, channel, click_type, was_queued, time_diff):
|
||||||
"""Update device state, if event was not queued."""
|
"""Update device state, if event was not queued."""
|
||||||
import pyflic
|
|
||||||
|
|
||||||
if was_queued and self._queued_event_check(click_type, time_diff):
|
if was_queued and self._queued_event_check(click_type, time_diff):
|
||||||
return
|
return
|
||||||
|
|
||||||
self._is_down = click_type == pyflic.ClickType.ButtonDown
|
self._is_down = click_type == ClickType.ButtonDown
|
||||||
self.schedule_update_ha_state()
|
self.schedule_update_ha_state()
|
||||||
|
|
||||||
def _on_click(self, channel, click_type, was_queued, time_diff):
|
def _on_click(self, channel, click_type, was_queued, time_diff):
|
||||||
|
@ -243,9 +243,7 @@ class FlicButton(BinarySensorDevice):
|
||||||
|
|
||||||
def _connection_status_changed(self, channel, connection_status, disconnect_reason):
|
def _connection_status_changed(self, channel, connection_status, disconnect_reason):
|
||||||
"""Remove device, if button disconnects."""
|
"""Remove device, if button disconnects."""
|
||||||
import pyflic
|
if connection_status == ConnectionStatus.Disconnected:
|
||||||
|
|
||||||
if connection_status == pyflic.ConnectionStatus.Disconnected:
|
|
||||||
_LOGGER.warning(
|
_LOGGER.warning(
|
||||||
"Button (%s) disconnected. Reason: %s", self.address, disconnect_reason
|
"Button (%s) disconnected. Reason: %s", self.address, disconnect_reason
|
||||||
)
|
)
|
||||||
|
|
Loading…
Add table
Reference in a new issue