Move imports in flic component (#27821)

This commit is contained in:
Quentame 2019-10-18 02:13:20 +02:00 committed by Paulus Schoutsen
parent 3a608314c4
commit fe036ed094

View file

@ -2,6 +2,14 @@
import logging
import threading
from pyflic import (
FlicClient,
ButtonConnectionChannel,
ClickType,
ConnectionStatus,
ScanWizard,
ScanWizardResult,
)
import voluptuous as vol
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):
"""Set up the flic platform."""
import pyflic
# Initialize flic client responsible for
# 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)
try:
client = pyflic.FlicClient(host, port)
client = FlicClient(host, port)
except ConnectionRefusedError:
_LOGGER.error("Failed to connect to flic server")
return
@ -88,15 +95,13 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
def start_scanning(config, add_entities, client):
"""Start a new flic client for scanning and connecting to new buttons."""
import pyflic
scan_wizard = pyflic.ScanWizard()
scan_wizard = ScanWizard()
def scan_completed_callback(scan_wizard, result, address, name):
"""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)
elif result != pyflic.ScanWizardResult.WizardFailedTimeout:
elif result != ScanWizardResult.WizardFailedTimeout:
_LOGGER.warning(
"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):
"""Initialize the flic button."""
import pyflic
self._hass = hass
self._address = address
@ -131,10 +135,10 @@ class FlicButton(BinarySensorDevice):
self._is_down = False
self._ignored_click_types = ignored_click_types or []
self._hass_click_types = {
pyflic.ClickType.ButtonClick: CLICK_TYPE_SINGLE,
pyflic.ClickType.ButtonSingleClick: CLICK_TYPE_SINGLE,
pyflic.ClickType.ButtonDoubleClick: CLICK_TYPE_DOUBLE,
pyflic.ClickType.ButtonHold: CLICK_TYPE_HOLD,
ClickType.ButtonClick: CLICK_TYPE_SINGLE,
ClickType.ButtonSingleClick: CLICK_TYPE_SINGLE,
ClickType.ButtonDoubleClick: CLICK_TYPE_DOUBLE,
ClickType.ButtonHold: CLICK_TYPE_HOLD,
}
self._channel = self._create_channel()
@ -142,9 +146,7 @@ class FlicButton(BinarySensorDevice):
def _create_channel(self):
"""Create a new connection channel to the button."""
import pyflic
channel = pyflic.ButtonConnectionChannel(self._address)
channel = ButtonConnectionChannel(self._address)
channel.on_button_up_or_down = self._on_up_down
# 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):
"""Update device state, if event was not queued."""
import pyflic
if was_queued and self._queued_event_check(click_type, time_diff):
return
self._is_down = click_type == pyflic.ClickType.ButtonDown
self._is_down = click_type == ClickType.ButtonDown
self.schedule_update_ha_state()
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):
"""Remove device, if button disconnects."""
import pyflic
if connection_status == pyflic.ConnectionStatus.Disconnected:
if connection_status == ConnectionStatus.Disconnected:
_LOGGER.warning(
"Button (%s) disconnected. Reason: %s", self.address, disconnect_reason
)