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 import config_entries, data_entry_flow
from homeassistant.components.dhcp import IP_ADDRESS, MAC_ADDRESS 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.const import CONF_HOST, CONF_MAC, CONF_NAME, CONF_TIMEOUT, CONF_TYPE
from homeassistant.helpers import config_validation as cv 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 from .helpers import format_mac
_LOGGER = logging.getLogger(__name__) _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): async def async_set_device(self, device, raise_on_progress=True):
"""Define a device for the config flow.""" """Define a device for the config flow."""
supported_types = set.union(*DOMAINS_AND_TYPES.values()) if device.type not in DEVICE_TYPES:
if device.type not in supported_types:
_LOGGER.error( _LOGGER.error(
"Unsupported device: %s. If it worked before, please open " "Unsupported device: %s. If it worked before, please open "
"an issue at https://github.com/home-assistant/core/issues", "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="cannot_connect")
return self.async_abort(reason="unknown") return self.async_abort(reason="unknown")
supported_types = set.union(*DOMAINS_AND_TYPES.values()) if device.type not in DEVICE_TYPES:
if device.type not in supported_types:
return self.async_abort(reason="not_supported") return self.async_abort(reason="not_supported")
await self.async_set_device(device) await self.async_set_device(device)
@ -110,7 +107,7 @@ class BroadlinkFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
else: else:
device.timeout = timeout device.timeout = timeout
if self.source != SOURCE_REAUTH: if self.source != config_entries.SOURCE_REAUTH:
await self.async_set_device(device) await self.async_set_device(device)
self._abort_if_unique_id_configured( self._abort_if_unique_id_configured(
updates={CONF_HOST: device.host[0], CONF_TIMEOUT: timeout} 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.light import DOMAIN as LIGHT_DOMAIN
from homeassistant.components.remote import DOMAIN as REMOTE_DOMAIN from homeassistant.components.remote import DOMAIN as REMOTE_DOMAIN
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
@ -36,6 +36,7 @@ DOMAINS_AND_TYPES = {
}, },
LIGHT_DOMAIN: {"LB1"}, LIGHT_DOMAIN: {"LB1"},
} }
DEVICE_TYPES = set.union(*DOMAINS_AND_TYPES.values())
DEFAULT_PORT = 80 DEFAULT_PORT = 80
DEFAULT_TIMEOUT = 5 DEFAULT_TIMEOUT = 5

View File

@ -23,9 +23,9 @@ from .updater import get_update_manager
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
def get_domains(dev_type): def get_domains(device_type):
"""Return the domains available for a 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: class BroadlinkDevice:
@ -67,8 +67,8 @@ class BroadlinkDevice:
device_registry.async_update_device(device_entry.id, name=entry.title) device_registry.async_update_device(device_entry.id, name=entry.title)
await hass.config_entries.async_reload(entry.entry_id) await hass.config_entries.async_reload(entry.entry_id)
def _auth_fetch_firmware(self): def _get_firmware_version(self):
"""Auth and fetch firmware.""" """Get firmware version."""
self.api.auth() self.api.auth()
with suppress(BroadlinkException, OSError): with suppress(BroadlinkException, OSError):
return self.api.get_fwversion() return self.api.get_fwversion()
@ -89,7 +89,7 @@ class BroadlinkDevice:
try: try:
self.fw_version = await self.hass.async_add_executor_job( self.fw_version = await self.hass.async_add_executor_job(
self._auth_fetch_firmware self._get_firmware_version
) )
except AuthenticationError: except AuthenticationError:

View File

@ -3,7 +3,7 @@ from base64 import b64decode
from homeassistant import config_entries from homeassistant import config_entries
from homeassistant.const import CONF_HOST 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 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.const import CONF_HOST, STATE_OFF
from homeassistant.core import callback 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.restore_state import RestoreEntity
from homeassistant.helpers.storage import Store from homeassistant.helpers.storage import Store
from homeassistant.util.dt import utcnow from homeassistant.util import dt
from .const import DOMAIN from .const import DOMAIN
from .entity import BroadlinkEntity from .entity import BroadlinkEntity
@ -332,8 +332,8 @@ class BroadlinkRemote(BroadlinkEntity, RemoteEntity, RestoreEntity):
) )
try: try:
start_time = utcnow() start_time = dt.utcnow()
while (utcnow() - start_time) < LEARNING_TIMEOUT: while (dt.utcnow() - start_time) < LEARNING_TIMEOUT:
await asyncio.sleep(1) await asyncio.sleep(1)
try: try:
code = await self._device.async_request(self._device.api.check_data) code = await self._device.async_request(self._device.api.check_data)
@ -367,8 +367,8 @@ class BroadlinkRemote(BroadlinkEntity, RemoteEntity, RestoreEntity):
) )
try: try:
start_time = utcnow() start_time = dt.utcnow()
while (utcnow() - start_time) < LEARNING_TIMEOUT: while (dt.utcnow() - start_time) < LEARNING_TIMEOUT:
await asyncio.sleep(1) await asyncio.sleep(1)
found = await self._device.async_request( found = await self._device.async_request(
self._device.api.check_frequency self._device.api.check_frequency
@ -405,8 +405,8 @@ class BroadlinkRemote(BroadlinkEntity, RemoteEntity, RestoreEntity):
) )
try: try:
start_time = utcnow() start_time = dt.utcnow()
while (utcnow() - start_time) < LEARNING_TIMEOUT: while (dt.utcnow() - start_time) < LEARNING_TIMEOUT:
await asyncio.sleep(1) await asyncio.sleep(1)
try: try:
code = await self._device.async_request(self._device.api.check_data) code = await self._device.async_request(self._device.api.check_data)