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
This commit is contained in:
Felipe Martins Diel 2021-10-31 20:01:11 -03:00 committed by GitHub
parent adfebaf510
commit 3f61ff4f96
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 20 additions and 22 deletions

View file

@ -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}

View file

@ -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

View file

@ -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:

View file

@ -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

View file

@ -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)