mirror of
https://github.com/home-assistant/core.git
synced 2025-07-10 06:47:09 +00:00
Improve code quality of the Broadlink switch platform (#58794)
This commit is contained in:
parent
9bc0e8e8ab
commit
1e92e35bff
@ -1,6 +1,5 @@
|
|||||||
"""Support for Broadlink switches."""
|
"""Support for Broadlink switches."""
|
||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
from functools import partial
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from broadlink.exceptions import BroadlinkException
|
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):
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||||
"""Set up the Broadlink switch."""
|
"""Set up the Broadlink switch."""
|
||||||
device = hass.data[DOMAIN].devices[config_entry.entry_id]
|
device = hass.data[DOMAIN].devices[config_entry.entry_id]
|
||||||
|
switches = []
|
||||||
|
|
||||||
if device.api.type in {"RM4MINI", "RM4PRO", "RMMINI", "RMMINIB", "RMPRO"}:
|
if device.api.type in {"RM4MINI", "RM4PRO", "RMMINI", "RMMINIB", "RMPRO"}:
|
||||||
platform_data = hass.data[DOMAIN].platforms.get(SWITCH_DOMAIN, {})
|
platform_data = hass.data[DOMAIN].platforms.get(SWITCH_DOMAIN, {})
|
||||||
user_defined_switches = platform_data.get(device.api.mac, {})
|
user_defined_switches = platform_data.get(device.api.mac, {})
|
||||||
switches = [
|
switches.extend(
|
||||||
BroadlinkRMSwitch(device, config) for config in user_defined_switches
|
BroadlinkRMSwitch(device, config) for config in user_defined_switches
|
||||||
]
|
)
|
||||||
|
|
||||||
elif device.api.type == "SP1":
|
elif device.api.type == "SP1":
|
||||||
switches = [BroadlinkSP1Switch(device)]
|
switches.append(BroadlinkSP1Switch(device))
|
||||||
|
|
||||||
elif device.api.type in {"SP2", "SP2S", "SP3", "SP3S", "SP4", "SP4B"}:
|
elif device.api.type in {"SP2", "SP2S", "SP3", "SP3S", "SP4", "SP4B"}:
|
||||||
switches = [BroadlinkSP2Switch(device)]
|
switches.append(BroadlinkSP2Switch(device))
|
||||||
|
|
||||||
elif device.api.type == "BG1":
|
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":
|
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)
|
async_add_entities(switches)
|
||||||
|
|
||||||
@ -273,9 +273,9 @@ class BroadlinkBG1Slot(BroadlinkSwitch):
|
|||||||
|
|
||||||
async def _async_send_packet(self, packet):
|
async def _async_send_packet(self, packet):
|
||||||
"""Send a packet to the device."""
|
"""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:
|
try:
|
||||||
await self._device.async_request(set_state)
|
await self._device.async_request(self._device.api.set_state, **state)
|
||||||
except (BroadlinkException, OSError) as err:
|
except (BroadlinkException, OSError) as err:
|
||||||
_LOGGER.error("Failed to send packet: %s", err)
|
_LOGGER.error("Failed to send packet: %s", err)
|
||||||
return False
|
return False
|
||||||
|
Loading…
x
Reference in New Issue
Block a user