Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Address late review of SwitchBee (#78412)
- Loading branch information
1 parent
383c83d
commit 491177e
Showing
14 changed files
with
313 additions
and
428 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,4 @@ | ||
"""Constants for the SwitchBee Smart Home integration.""" | ||
|
||
from switchbee.device import DeviceType | ||
|
||
DOMAIN = "switchbee" | ||
SCAN_INTERVAL_SEC = 5 | ||
CONF_SCAN_INTERVAL = "scan_interval" | ||
CONF_SWITCHES_AS_LIGHTS = "switch_as_light" | ||
CONF_DEVICES = "devices" | ||
CONF_DEFUALT_ALLOWED = [ | ||
DeviceType.Switch.display, | ||
DeviceType.TimedPowerSwitch.display, | ||
DeviceType.TimedSwitch.display, | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
"""SwitchBee integration Coordinator.""" | ||
|
||
from datetime import timedelta | ||
import logging | ||
|
||
from switchbee.api import CentralUnitAPI, SwitchBeeError | ||
from switchbee.device import DeviceType, SwitchBeeBaseDevice | ||
|
||
from homeassistant.core import HomeAssistant | ||
from homeassistant.helpers.device_registry import format_mac | ||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed | ||
|
||
from .const import DOMAIN, SCAN_INTERVAL_SEC | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
class SwitchBeeCoordinator(DataUpdateCoordinator[dict[int, SwitchBeeBaseDevice]]): | ||
"""Class to manage fetching Freedompro data API.""" | ||
|
||
def __init__( | ||
self, | ||
hass: HomeAssistant, | ||
swb_api: CentralUnitAPI, | ||
) -> None: | ||
"""Initialize.""" | ||
self.api: CentralUnitAPI = swb_api | ||
self._reconnect_counts: int = 0 | ||
self.mac_formated: str = format_mac(swb_api.mac) | ||
super().__init__( | ||
hass, | ||
_LOGGER, | ||
name=DOMAIN, | ||
update_interval=timedelta(seconds=SCAN_INTERVAL_SEC), | ||
) | ||
|
||
async def _async_update_data(self) -> dict[int, SwitchBeeBaseDevice]: | ||
"""Update data via library.""" | ||
|
||
if self._reconnect_counts != self.api.reconnect_count: | ||
self._reconnect_counts = self.api.reconnect_count | ||
_LOGGER.debug( | ||
"Central Unit re-connected again due to invalid token, total %i", | ||
self._reconnect_counts, | ||
) | ||
|
||
# The devices are loaded once during the config_entry | ||
if not self.api.devices: | ||
# Try to load the devices from the CU for the first time | ||
try: | ||
await self.api.fetch_configuration( | ||
[ | ||
DeviceType.Switch, | ||
DeviceType.TimedSwitch, | ||
DeviceType.GroupSwitch, | ||
DeviceType.TimedPowerSwitch, | ||
] | ||
) | ||
except SwitchBeeError as exp: | ||
raise UpdateFailed( | ||
f"Error communicating with API: {exp}" | ||
) from SwitchBeeError | ||
else: | ||
_LOGGER.debug("Loaded devices") | ||
|
||
# Get the state of the devices | ||
try: | ||
await self.api.fetch_states() | ||
except SwitchBeeError as exp: | ||
raise UpdateFailed( | ||
f"Error communicating with API: {exp}" | ||
) from SwitchBeeError | ||
|
||
return self.api.devices |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.