From e5fe0c40ecf2cb243e30ddd162faa7489be54758 Mon Sep 17 00:00:00 2001
From: Felipe Martins Diel <41558831+felipediel@users.noreply.github.com>
Date: Sun, 8 Nov 2020 14:19:28 -0300
Subject: [PATCH] Add support for Broadlink SP4 series and the latest SP mini 3
 devices (#41522)

* Add support for Broadlink SP4 series

* Bump python-broadlink to 0.16

* Fix stale docstring
---
 homeassistant/components/broadlink/const.py   |  2 +-
 .../components/broadlink/manifest.json        |  2 +-
 homeassistant/components/broadlink/switch.py  | 24 +++++++++++++++++++
 homeassistant/components/broadlink/updater.py | 10 ++++++++
 requirements_all.txt                          |  2 +-
 requirements_test_all.txt                     |  2 +-
 6 files changed, 38 insertions(+), 4 deletions(-)

diff --git a/homeassistant/components/broadlink/const.py b/homeassistant/components/broadlink/const.py
index 09a667b0969..adb5a437ae5 100644
--- a/homeassistant/components/broadlink/const.py
+++ b/homeassistant/components/broadlink/const.py
@@ -8,7 +8,7 @@ DOMAIN = "broadlink"
 DOMAINS_AND_TYPES = (
     (REMOTE_DOMAIN, ("RM2", "RM4")),
     (SENSOR_DOMAIN, ("A1", "RM2", "RM4")),
-    (SWITCH_DOMAIN, ("MP1", "RM2", "RM4", "SP1", "SP2")),
+    (SWITCH_DOMAIN, ("MP1", "RM2", "RM4", "SP1", "SP2", "SP4", "SP4B")),
 )
 
 DEFAULT_PORT = 80
diff --git a/homeassistant/components/broadlink/manifest.json b/homeassistant/components/broadlink/manifest.json
index 9c6e571ec86..0562bc306a5 100644
--- a/homeassistant/components/broadlink/manifest.json
+++ b/homeassistant/components/broadlink/manifest.json
@@ -2,7 +2,7 @@
   "domain": "broadlink",
   "name": "Broadlink",
   "documentation": "https://www.home-assistant.io/integrations/broadlink",
-  "requirements": ["broadlink==0.15.0"],
+  "requirements": ["broadlink==0.16.0"],
   "codeowners": ["@danielhiversen", "@felipediel"],
   "config_flow": true
 }
diff --git a/homeassistant/components/broadlink/switch.py b/homeassistant/components/broadlink/switch.py
index 4067d1e535d..644255d7d17 100644
--- a/homeassistant/components/broadlink/switch.py
+++ b/homeassistant/components/broadlink/switch.py
@@ -121,6 +121,9 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
     elif device.api.type == "SP2":
         switches = [BroadlinkSP2Switch(device)]
 
+    elif device.api.type in {"SP4", "SP4B"}:
+        switches = [BroadlinkSP4Switch(device)]
+
     elif device.api.type == "MP1":
         switches = [BroadlinkMP1Slot(device, slot) for slot in range(1, 5)]
 
@@ -294,6 +297,27 @@ class BroadlinkSP2Switch(BroadlinkSP1Switch):
         self.async_write_ha_state()
 
 
+class BroadlinkSP4Switch(BroadlinkSP1Switch):
+    """Representation of a Broadlink SP4 switch."""
+
+    def __init__(self, device, *args, **kwargs):
+        """Initialize the switch."""
+        super().__init__(device, *args, **kwargs)
+        self._state = self._coordinator.data["pwr"]
+
+    @property
+    def assumed_state(self):
+        """Return True if unable to access real state of the switch."""
+        return False
+
+    @callback
+    def update_data(self):
+        """Update data."""
+        if self._coordinator.last_update_success:
+            self._state = self._coordinator.data["pwr"]
+        self.async_write_ha_state()
+
+
 class BroadlinkMP1Slot(BroadlinkSwitch):
     """Representation of a Broadlink MP1 slot."""
 
diff --git a/homeassistant/components/broadlink/updater.py b/homeassistant/components/broadlink/updater.py
index 6d3bbc5b6c1..22f26d41b9c 100644
--- a/homeassistant/components/broadlink/updater.py
+++ b/homeassistant/components/broadlink/updater.py
@@ -31,6 +31,8 @@ def get_update_manager(device):
         "RM4": BroadlinkRMUpdateManager,
         "SP1": BroadlinkSP1UpdateManager,
         "SP2": BroadlinkSP2UpdateManager,
+        "SP4": BroadlinkSP4UpdateManager,
+        "SP4B": BroadlinkSP4UpdateManager,
     }
     return update_managers[device.api.type](device)
 
@@ -153,3 +155,11 @@ class BroadlinkSP2UpdateManager(BroadlinkUpdateManager):
         except (CommandNotSupportedError, StorageError):
             data["load_power"] = None
         return data
+
+
+class BroadlinkSP4UpdateManager(BroadlinkUpdateManager):
+    """Manages updates for Broadlink SP4 devices."""
+
+    async def async_fetch_data(self):
+        """Fetch data from the device."""
+        return await self.device.async_request(self.device.api.get_state)
diff --git a/requirements_all.txt b/requirements_all.txt
index 94c46007577..9f10f1b0bde 100644
--- a/requirements_all.txt
+++ b/requirements_all.txt
@@ -380,7 +380,7 @@ boto3==1.9.252
 bravia-tv==1.0.6
 
 # homeassistant.components.broadlink
-broadlink==0.15.0
+broadlink==0.16.0
 
 # homeassistant.components.brother
 brother==0.1.18
diff --git a/requirements_test_all.txt b/requirements_test_all.txt
index 927ef726c8a..763dc77be22 100644
--- a/requirements_test_all.txt
+++ b/requirements_test_all.txt
@@ -204,7 +204,7 @@ bond-api==0.1.8
 bravia-tv==1.0.6
 
 # homeassistant.components.broadlink
-broadlink==0.15.0
+broadlink==0.16.0
 
 # homeassistant.components.brother
 brother==0.1.18
-- 
GitLab