* Implement config flow in the Broadlink integration * General improvements to the Broadlink config flow * Remove unnecessary else after return * Fix translations * Rename device to device_entry * Add tests for the config flow * Improve docstrings * Test we do not accept more than one config entry per device * Improve helpers * Allow empty packets * Allow multiple config files for switches related to the same device * Rename mock_device to mock_api * General improvements * Make new attempts before marking the device as unavailable * Let the name be the template for the entity_id * Handle OSError * Test network unavailable in the configuration flow * Rename lock attribute * Update manifest.json * Import devices from platforms * Test import flow * Add deprecation warnings * General improvements * Rename deprecate to discontinue * Test device setup * Add type attribute to mock api * Test we handle an update failure at startup * Remove BroadlinkDevice from tests * Remove device.py from .coveragerc * Add tests for the config flow * Add tests for the device * Test device registry and update listener * Test MAC address validation * Add tests for the device * Extract domains and types to a helper function * Do not patch integration details * Add tests for the device * Set device classes where appropriate * Set an appropriate connection class * Do not set device class for custom switches * Fix tests and improve code readability * Use RM4 to test authentication errors * Handle BroadlinkException in the authentication
128 lines
3.9 KiB
Python
128 lines
3.9 KiB
Python
"""Support for fetching data from Broadlink devices."""
|
|
from abc import ABC, abstractmethod
|
|
from datetime import timedelta
|
|
import logging
|
|
|
|
from broadlink.exceptions import (
|
|
AuthorizationError,
|
|
BroadlinkException,
|
|
CommandNotSupportedError,
|
|
StorageError,
|
|
)
|
|
|
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
|
from homeassistant.util import dt
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
def get_update_manager(device):
|
|
"""Return an update manager for a given Broadlink device."""
|
|
update_managers = {
|
|
"A1": BroadlinkA1UpdateManager,
|
|
"MP1": BroadlinkMP1UpdateManager,
|
|
"RM2": BroadlinkRMUpdateManager,
|
|
"RM4": BroadlinkRMUpdateManager,
|
|
"SP1": BroadlinkSP1UpdateManager,
|
|
"SP2": BroadlinkSP2UpdateManager,
|
|
}
|
|
return update_managers[device.api.type](device)
|
|
|
|
|
|
class BroadlinkUpdateManager(ABC):
|
|
"""Representation of a Broadlink update manager.
|
|
|
|
Implement this class to manage fetching data from the device and to
|
|
monitor device availability.
|
|
"""
|
|
|
|
def __init__(self, device):
|
|
"""Initialize the update manager."""
|
|
self.device = device
|
|
self.coordinator = DataUpdateCoordinator(
|
|
device.hass,
|
|
_LOGGER,
|
|
name="device",
|
|
update_method=self.async_update,
|
|
update_interval=timedelta(minutes=1),
|
|
)
|
|
self.available = None
|
|
self.last_update = None
|
|
|
|
async def async_update(self):
|
|
"""Fetch data from the device and update availability."""
|
|
try:
|
|
data = await self.async_fetch_data()
|
|
|
|
except (BroadlinkException, OSError) as err:
|
|
if self.available and (
|
|
dt.utcnow() - self.last_update > timedelta(minutes=3)
|
|
or isinstance(err, (AuthorizationError, OSError))
|
|
):
|
|
self.available = False
|
|
_LOGGER.warning(
|
|
"Disconnected from the device at %s", self.device.api.host[0]
|
|
)
|
|
raise UpdateFailed(err)
|
|
|
|
else:
|
|
if self.available is False:
|
|
_LOGGER.warning(
|
|
"Connected to the device at %s", self.device.api.host[0]
|
|
)
|
|
self.available = True
|
|
self.last_update = dt.utcnow()
|
|
return data
|
|
|
|
@abstractmethod
|
|
async def async_fetch_data(self):
|
|
"""Fetch data from the device."""
|
|
|
|
|
|
class BroadlinkA1UpdateManager(BroadlinkUpdateManager):
|
|
"""Manages updates for Broadlink A1 devices."""
|
|
|
|
async def async_fetch_data(self):
|
|
"""Fetch data from the device."""
|
|
return await self.device.async_request(self.device.api.check_sensors_raw)
|
|
|
|
|
|
class BroadlinkMP1UpdateManager(BroadlinkUpdateManager):
|
|
"""Manages updates for Broadlink MP1 devices."""
|
|
|
|
async def async_fetch_data(self):
|
|
"""Fetch data from the device."""
|
|
return await self.device.async_request(self.device.api.check_power)
|
|
|
|
|
|
class BroadlinkRMUpdateManager(BroadlinkUpdateManager):
|
|
"""Manages updates for Broadlink RM2 and RM4 devices."""
|
|
|
|
async def async_fetch_data(self):
|
|
"""Fetch data from the device."""
|
|
return await self.device.async_request(self.device.api.check_sensors)
|
|
|
|
|
|
class BroadlinkSP1UpdateManager(BroadlinkUpdateManager):
|
|
"""Manages updates for Broadlink SP1 devices."""
|
|
|
|
async def async_fetch_data(self):
|
|
"""Fetch data from the device."""
|
|
return None
|
|
|
|
|
|
class BroadlinkSP2UpdateManager(BroadlinkUpdateManager):
|
|
"""Manages updates for Broadlink SP2 devices."""
|
|
|
|
async def async_fetch_data(self):
|
|
"""Fetch data from the device."""
|
|
data = {}
|
|
data["state"] = await self.device.async_request(self.device.api.check_power)
|
|
try:
|
|
data["load_power"] = await self.device.async_request(
|
|
self.device.api.get_energy
|
|
)
|
|
except (CommandNotSupportedError, StorageError):
|
|
data["load_power"] = None
|
|
return data
|