Skip to content

Commit

Permalink
Fix RM mini 3 update manager (#40215)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipediel authored and balloob committed Sep 18, 2020
1 parent d37fe1f commit 8a39bea
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
22 changes: 22 additions & 0 deletions homeassistant/components/broadlink/updater.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
"""Support for fetching data from Broadlink devices."""
from abc import ABC, abstractmethod
from datetime import timedelta
from functools import partial
import logging

import broadlink as blk
from broadlink.exceptions import (
AuthorizationError,
BroadlinkException,
CommandNotSupportedError,
DeviceOfflineError,
StorageError,
)

Expand All @@ -18,6 +21,9 @@

def get_update_manager(device):
"""Return an update manager for a given Broadlink device."""
if device.api.model.startswith("RM mini"):
return BroadlinkRMMini3UpdateManager(device)

update_managers = {
"A1": BroadlinkA1UpdateManager,
"MP1": BroadlinkMP1UpdateManager,
Expand Down Expand Up @@ -95,6 +101,22 @@ async def async_fetch_data(self):
return await self.device.async_request(self.device.api.check_power)


class BroadlinkRMMini3UpdateManager(BroadlinkUpdateManager):
"""Manages updates for Broadlink RM mini 3 devices."""

async def async_fetch_data(self):
"""Fetch data from the device."""
hello = partial(
blk.discover,
discover_ip_address=self.device.api.host[0],
timeout=self.device.api.timeout,
)
devices = await self.device.hass.async_add_executor_job(hello)
if not devices:
raise DeviceOfflineError("The device is offline")
return {}


class BroadlinkRMUpdateManager(BroadlinkUpdateManager):
"""Manages updates for Broadlink RM2 and RM4 devices."""

Expand Down
3 changes: 3 additions & 0 deletions tests/components/broadlink/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ async def setup_entry(self, hass, mock_api=None, mock_entry=None):
with patch(
"homeassistant.components.broadlink.device.blk.gendevice",
return_value=mock_api,
), patch(
"homeassistant.components.broadlink.updater.blk.discover",
return_value=[mock_api],
):
await hass.config_entries.async_setup(mock_entry.entry_id)
await hass.async_block_till_done()
Expand Down
4 changes: 2 additions & 2 deletions tests/components/broadlink/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ async def test_device_setup_update_authorization_error(hass):

async def test_device_setup_update_authentication_error(hass):
"""Test we handle an authentication error in the update step."""
device = get_device("Living Room")
device = get_device("Garage")
mock_api = device.get_mock_api()
mock_api.check_sensors.side_effect = blke.AuthorizationError()
mock_api.auth.side_effect = (None, blke.AuthenticationError())
Expand Down Expand Up @@ -207,7 +207,7 @@ async def test_device_setup_update_authentication_error(hass):

async def test_device_setup_update_broadlink_exception(hass):
"""Test we handle a Broadlink exception in the update step."""
device = get_device("Living Room")
device = get_device("Garage")
mock_api = device.get_mock_api()
mock_api.check_sensors.side_effect = blke.BroadlinkException()
mock_entry = device.get_mock_entry()
Expand Down

0 comments on commit 8a39bea

Please sign in to comment.