1 """Support for Broadlink switches."""
3 from __future__
import annotations
5 from abc
import ABC, abstractmethod
9 from broadlink.exceptions
import BroadlinkException
10 import voluptuous
as vol
13 PLATFORM_SCHEMA
as SWITCH_PLATFORM_SCHEMA,
37 from .
import BroadlinkDevice
38 from .const
import DOMAIN
39 from .entity
import BroadlinkEntity
40 from .helpers
import data_packet, import_device, mac_address
42 _LOGGER = logging.getLogger(__name__)
46 SWITCH_SCHEMA = vol.Schema(
48 vol.Required(CONF_NAME): cv.string,
49 vol.Optional(CONF_COMMAND_OFF): data_packet,
50 vol.Optional(CONF_COMMAND_ON): data_packet,
54 PLATFORM_SCHEMA = vol.All(
55 cv.deprecated(CONF_HOST),
56 cv.deprecated(CONF_SLOTS),
57 cv.deprecated(CONF_TIMEOUT),
58 cv.deprecated(CONF_TYPE),
59 SWITCH_PLATFORM_SCHEMA.extend(
61 vol.Required(CONF_MAC): mac_address,
62 vol.Optional(CONF_HOST): cv.string,
63 vol.Optional(CONF_SWITCHES, default=[]): vol.All(
75 async_add_entities: AddEntitiesCallback,
76 discovery_info: DiscoveryInfoType |
None =
None,
78 """Import the device and set up custom switches.
80 This is for backward compatibility.
81 Do not use this method.
83 mac_addr = config[CONF_MAC]
84 host = config.get(CONF_HOST)
86 if switches := config.get(CONF_SWITCHES):
87 platform_data = hass.data[DOMAIN].platforms.get(Platform.SWITCH, {})
88 async_add_entities_config_entry: AddEntitiesCallback
89 device: BroadlinkDevice
90 async_add_entities_config_entry, device = platform_data.get(
91 mac_addr, (
None,
None)
93 if not async_add_entities_config_entry:
94 raise PlatformNotReady
96 async_add_entities_config_entry(
102 "The switch platform is deprecated, except for custom IR/RF "
103 "switches. Please refer to the Broadlink documentation to "
113 config_entry: ConfigEntry,
114 async_add_entities: AddEntitiesCallback,
116 """Set up the Broadlink switch."""
117 device = hass.data[DOMAIN].devices[config_entry.entry_id]
118 switches: list[BroadlinkSwitch] = []
120 if device.api.type
in {
"RM4MINI",
"RM4PRO",
"RMMINI",
"RMMINIB",
"RMPRO"}:
121 platform_data = hass.data[DOMAIN].platforms.setdefault(Platform.SWITCH, {})
122 platform_data[device.api.mac] = async_add_entities, device
123 elif device.api.type ==
"SP1":
126 elif device.api.type
in {
"SP2",
"SP2S",
"SP3",
"SP3S",
"SP4",
"SP4B"}:
129 elif device.api.type ==
"BG1":
132 elif device.api.type
in {
"MP1",
"MP1S"}:
139 """Representation of a Broadlink switch."""
141 _attr_assumed_state =
True
142 _attr_device_class = SwitchDeviceClass.SWITCH
144 def __init__(self, device, command_on, command_off):
145 """Initialize the switch."""
151 """Call when the switch is added to hass."""
153 self.
_attr_is_on_attr_is_on = state
is not None and state.state == STATE_ON
157 """Turn on the switch."""
163 """Turn off the switch."""
170 """Send a packet to the device."""
173 class BroadlinkRMSwitch(BroadlinkSwitch):
174 """Representation of a Broadlink RM switch."""
177 """Initialize the switch."""
179 device, config.get(CONF_COMMAND_ON), config.get(CONF_COMMAND_OFF)
184 """Send a packet to the device."""
191 await device.async_request(device.api.send_data, packet)
192 except (BroadlinkException, OSError)
as err:
193 _LOGGER.error(
"Failed to send packet: %s", err)
199 """Representation of a Broadlink SP1 switch."""
201 _attr_has_entity_name =
True
204 """Initialize the switch."""
209 """Send a packet to the device."""
213 await device.async_request(device.api.set_power, packet)
214 except (BroadlinkException, OSError)
as err:
215 _LOGGER.error(
"Failed to send packet: %s", err)
221 """Representation of a Broadlink SP2 switch."""
223 _attr_assumed_state =
False
224 _attr_has_entity_name =
True
228 """Initialize the switch."""
229 super().
__init__(device, *args, **kwargs)
233 """Update the state of the entity."""
238 """Representation of a Broadlink MP1 slot."""
240 _attr_assumed_state =
False
241 _attr_has_entity_name =
True
244 """Initialize the switch."""
252 """Update the state of the entity."""
256 """Send a packet to the device."""
260 await device.async_request(device.api.set_power, self.
_slot_slot, packet)
261 except (BroadlinkException, OSError)
as err:
262 _LOGGER.error(
"Failed to send packet: %s", err)
268 """Representation of a Broadlink BG1 slot."""
270 _attr_assumed_state =
False
271 _attr_has_entity_name =
True
274 """Initialize the switch."""
284 """Update the state of the entity."""
288 """Send a packet to the device."""
290 state = {f
"pwr{self._slot}": packet}
293 await device.async_request(device.api.set_state, **state)
294 except (BroadlinkException, OSError)
as err:
295 _LOGGER.error(
"Failed to send packet: %s", err)
def _async_send_packet(self, packet)
def _update_state(self, data)
def __init__(self, device, slot)
def _async_send_packet(self, packet)
def __init__(self, device, slot)
def _update_state(self, data)
def _async_send_packet(self, packet)
def __init__(self, device, config)
def _async_send_packet(self, packet)
def __init__(self, device)
def __init__(self, device, *args, **kwargs)
def _update_state(self, data)
None async_turn_on(self, **Any kwargs)
def __init__(self, device, command_on, command_off)
None async_turn_off(self, **Any kwargs)
def _async_send_packet(self, packet)
None async_added_to_hass(self)
None async_write_ha_state(self)
State|None async_get_last_state(self)
def import_device(hass, host)
None async_setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback async_add_entities, DiscoveryInfoType|None discovery_info=None)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)