From 3f61ff4f961ac71e528ded8345848bab3eed2c40 Mon Sep 17 00:00:00 2001 From: Felipe Martins Diel <41558831+felipediel@users.noreply.github.com> Date: Sun, 31 Oct 2021 20:01:11 -0300 Subject: [PATCH] Make general code quality improvements in the Broadlink integration (#58848) * Create DEVICE_TYPES constant * Rename _auth_fetch_firmware() to _get_firmware_version() * Rename dev_type to device_type * Use SOURCE_REAUTH from config_entries namespace * Fix unidiomatic imports --- .../components/broadlink/config_flow.py | 11 ++++------- homeassistant/components/broadlink/const.py | 3 ++- homeassistant/components/broadlink/device.py | 10 +++++----- homeassistant/components/broadlink/helpers.py | 2 +- homeassistant/components/broadlink/remote.py | 16 ++++++++-------- 5 files changed, 20 insertions(+), 22 deletions(-) diff --git a/homeassistant/components/broadlink/config_flow.py b/homeassistant/components/broadlink/config_flow.py index 884a6a9d102..2ab21372fd9 100644 --- a/homeassistant/components/broadlink/config_flow.py +++ b/homeassistant/components/broadlink/config_flow.py @@ -14,11 +14,10 @@ import voluptuous as vol from homeassistant import config_entries, data_entry_flow from homeassistant.components.dhcp import IP_ADDRESS, MAC_ADDRESS -from homeassistant.config_entries import SOURCE_REAUTH from homeassistant.const import CONF_HOST, CONF_MAC, CONF_NAME, CONF_TIMEOUT, CONF_TYPE from homeassistant.helpers import config_validation as cv -from .const import DEFAULT_PORT, DEFAULT_TIMEOUT, DOMAIN, DOMAINS_AND_TYPES +from .const import DEFAULT_PORT, DEFAULT_TIMEOUT, DEVICE_TYPES, DOMAIN from .helpers import format_mac _LOGGER = logging.getLogger(__name__) @@ -35,8 +34,7 @@ class BroadlinkFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): async def async_set_device(self, device, raise_on_progress=True): """Define a device for the config flow.""" - supported_types = set.union(*DOMAINS_AND_TYPES.values()) - if device.type not in supported_types: + if device.type not in DEVICE_TYPES: _LOGGER.error( "Unsupported device: %s. If it worked before, please open " "an issue at https://github.com/home-assistant/core/issues", @@ -73,8 +71,7 @@ class BroadlinkFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): return self.async_abort(reason="cannot_connect") return self.async_abort(reason="unknown") - supported_types = set.union(*DOMAINS_AND_TYPES.values()) - if device.type not in supported_types: + if device.type not in DEVICE_TYPES: return self.async_abort(reason="not_supported") await self.async_set_device(device) @@ -110,7 +107,7 @@ class BroadlinkFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): else: device.timeout = timeout - if self.source != SOURCE_REAUTH: + if self.source != config_entries.SOURCE_REAUTH: await self.async_set_device(device) self._abort_if_unique_id_configured( updates={CONF_HOST: device.host[0], CONF_TIMEOUT: timeout} diff --git a/homeassistant/components/broadlink/const.py b/homeassistant/components/broadlink/const.py index f40fd7785a1..174c7edde3a 100644 --- a/homeassistant/components/broadlink/const.py +++ b/homeassistant/components/broadlink/const.py @@ -1,4 +1,4 @@ -"""Constants for the Broadlink integration.""" +"""Constants.""" from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN from homeassistant.components.remote import DOMAIN as REMOTE_DOMAIN from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN @@ -36,6 +36,7 @@ DOMAINS_AND_TYPES = { }, LIGHT_DOMAIN: {"LB1"}, } +DEVICE_TYPES = set.union(*DOMAINS_AND_TYPES.values()) DEFAULT_PORT = 80 DEFAULT_TIMEOUT = 5 diff --git a/homeassistant/components/broadlink/device.py b/homeassistant/components/broadlink/device.py index aada9ace84a..1d1fe273252 100644 --- a/homeassistant/components/broadlink/device.py +++ b/homeassistant/components/broadlink/device.py @@ -23,9 +23,9 @@ from .updater import get_update_manager _LOGGER = logging.getLogger(__name__) -def get_domains(dev_type): +def get_domains(device_type): """Return the domains available for a device type.""" - return {d for d, t in DOMAINS_AND_TYPES.items() if dev_type in t} + return {d for d, t in DOMAINS_AND_TYPES.items() if device_type in t} class BroadlinkDevice: @@ -67,8 +67,8 @@ class BroadlinkDevice: device_registry.async_update_device(device_entry.id, name=entry.title) await hass.config_entries.async_reload(entry.entry_id) - def _auth_fetch_firmware(self): - """Auth and fetch firmware.""" + def _get_firmware_version(self): + """Get firmware version.""" self.api.auth() with suppress(BroadlinkException, OSError): return self.api.get_fwversion() @@ -89,7 +89,7 @@ class BroadlinkDevice: try: self.fw_version = await self.hass.async_add_executor_job( - self._auth_fetch_firmware + self._get_firmware_version ) except AuthenticationError: diff --git a/homeassistant/components/broadlink/helpers.py b/homeassistant/components/broadlink/helpers.py index 6d81b98d5d1..bec61ba5bbd 100644 --- a/homeassistant/components/broadlink/helpers.py +++ b/homeassistant/components/broadlink/helpers.py @@ -3,7 +3,7 @@ from base64 import b64decode from homeassistant import config_entries from homeassistant.const import CONF_HOST -import homeassistant.helpers.config_validation as cv +from homeassistant.helpers import config_validation as cv from .const import DOMAIN diff --git a/homeassistant/components/broadlink/remote.py b/homeassistant/components/broadlink/remote.py index a0c5c4130e5..cc2d85204b8 100644 --- a/homeassistant/components/broadlink/remote.py +++ b/homeassistant/components/broadlink/remote.py @@ -34,10 +34,10 @@ from homeassistant.components.remote import ( ) from homeassistant.const import CONF_HOST, STATE_OFF from homeassistant.core import callback -import homeassistant.helpers.config_validation as cv +from homeassistant.helpers import config_validation as cv from homeassistant.helpers.restore_state import RestoreEntity from homeassistant.helpers.storage import Store -from homeassistant.util.dt import utcnow +from homeassistant.util import dt from .const import DOMAIN from .entity import BroadlinkEntity @@ -332,8 +332,8 @@ class BroadlinkRemote(BroadlinkEntity, RemoteEntity, RestoreEntity): ) try: - start_time = utcnow() - while (utcnow() - start_time) < LEARNING_TIMEOUT: + start_time = dt.utcnow() + while (dt.utcnow() - start_time) < LEARNING_TIMEOUT: await asyncio.sleep(1) try: code = await self._device.async_request(self._device.api.check_data) @@ -367,8 +367,8 @@ class BroadlinkRemote(BroadlinkEntity, RemoteEntity, RestoreEntity): ) try: - start_time = utcnow() - while (utcnow() - start_time) < LEARNING_TIMEOUT: + start_time = dt.utcnow() + while (dt.utcnow() - start_time) < LEARNING_TIMEOUT: await asyncio.sleep(1) found = await self._device.async_request( self._device.api.check_frequency @@ -405,8 +405,8 @@ class BroadlinkRemote(BroadlinkEntity, RemoteEntity, RestoreEntity): ) try: - start_time = utcnow() - while (utcnow() - start_time) < LEARNING_TIMEOUT: + start_time = dt.utcnow() + while (dt.utcnow() - start_time) < LEARNING_TIMEOUT: await asyncio.sleep(1) try: code = await self._device.async_request(self._device.api.check_data)