From fe036ed0940c5be3ee901ba060ca8b40a01fd324 Mon Sep 17 00:00:00 2001 From: Quentame Date: Fri, 18 Oct 2019 02:13:20 +0200 Subject: [PATCH] Move imports in flic component (#27821) --- .../components/flic/binary_sensor.py | 40 +++++++++---------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/homeassistant/components/flic/binary_sensor.py b/homeassistant/components/flic/binary_sensor.py index 4fa97334889..416d39e5332 100644 --- a/homeassistant/components/flic/binary_sensor.py +++ b/homeassistant/components/flic/binary_sensor.py @@ -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 )