Improve code quality of the Broadlink switch platform (#58794)

This commit is contained in:
Felipe Martins Diel 2021-10-31 07:48:52 -03:00 committed by GitHub
parent 9bc0e8e8ab
commit 1e92e35bff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,5 @@
"""Support for Broadlink switches."""
from abc import ABC, abstractmethod
from functools import partial
import logging
from broadlink.exceptions import BroadlinkException
@ -108,25 +107,26 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up the Broadlink switch."""
device = hass.data[DOMAIN].devices[config_entry.entry_id]
switches = []
if device.api.type in {"RM4MINI", "RM4PRO", "RMMINI", "RMMINIB", "RMPRO"}:
platform_data = hass.data[DOMAIN].platforms.get(SWITCH_DOMAIN, {})
user_defined_switches = platform_data.get(device.api.mac, {})
switches = [
switches.extend(
BroadlinkRMSwitch(device, config) for config in user_defined_switches
]
)
elif device.api.type == "SP1":
switches = [BroadlinkSP1Switch(device)]
switches.append(BroadlinkSP1Switch(device))
elif device.api.type in {"SP2", "SP2S", "SP3", "SP3S", "SP4", "SP4B"}:
switches = [BroadlinkSP2Switch(device)]
switches.append(BroadlinkSP2Switch(device))
elif device.api.type == "BG1":
switches = [BroadlinkBG1Slot(device, slot) for slot in range(1, 3)]
switches.extend(BroadlinkBG1Slot(device, slot) for slot in range(1, 3))
elif device.api.type == "MP1":
switches = [BroadlinkMP1Slot(device, slot) for slot in range(1, 5)]
switches.extend(BroadlinkMP1Slot(device, slot) for slot in range(1, 5))
async_add_entities(switches)
@ -273,9 +273,9 @@ class BroadlinkBG1Slot(BroadlinkSwitch):
async def _async_send_packet(self, packet):
"""Send a packet to the device."""
set_state = partial(self._device.api.set_state, **{f"pwr{self._slot}": packet})
state = {f"pwr{self._slot}": packet}
try:
await self._device.async_request(set_state)
await self._device.async_request(self._device.api.set_state, **state)
except (BroadlinkException, OSError) as err:
_LOGGER.error("Failed to send packet: %s", err)
return False