Call async_forward_setup_entry after the first refresh in SwitchBot Cloud (#135625)

This commit is contained in:
Mick Montorier-Aberman 2025-01-15 15:33:21 +01:00 committed by GitHub
parent e83ee00af8
commit 6a50648223
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 30 additions and 27 deletions

View File

@ -135,10 +135,10 @@ async def async_setup_entry(hass: HomeAssistant, config: ConfigEntry) -> bool:
hass.data[DOMAIN][config.entry_id] = SwitchbotCloudData(
api=api, devices=make_device_data(hass, api, devices, coordinators_by_id)
)
await hass.config_entries.async_forward_entry_setups(config, PLATFORMS)
await gather(
*[coordinator.async_refresh() for coordinator in coordinators_by_id.values()]
)
await hass.config_entries.async_forward_entry_setups(config, PLATFORMS)
return True

View File

@ -4,6 +4,7 @@ from typing import Any
from switchbot_api import Commands, Device, Remote, SwitchBotAPI
from homeassistant.core import callback
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.update_coordinator import CoordinatorEntity
@ -48,3 +49,17 @@ class SwitchBotCloudEntity(CoordinatorEntity[SwitchBotCoordinator]):
command_type,
parameters,
)
@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
self._set_attributes()
super()._handle_coordinator_update()
def _set_attributes(self) -> None:
"""Set attributes from coordinator data."""
async def async_added_to_hass(self) -> None:
"""Run when entity is about to be added to hass."""
await super().async_added_to_hass()
self._set_attributes()

View File

@ -6,7 +6,7 @@ from switchbot_api import LockCommands
from homeassistant.components.lock import LockEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import SwitchbotCloudData
@ -32,12 +32,10 @@ class SwitchBotCloudLock(SwitchBotCloudEntity, LockEntity):
_attr_name = None
@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
def _set_attributes(self) -> None:
"""Set attributes from coordinator data."""
if coord_data := self.coordinator.data:
self._attr_is_locked = coord_data["lockState"] == "locked"
self.async_write_ha_state()
async def async_lock(self, **kwargs: Any) -> None:
"""Lock the lock."""

View File

@ -17,7 +17,7 @@ from homeassistant.const import (
UnitOfPower,
UnitOfTemperature,
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import SwitchbotCloudData
@ -166,10 +166,8 @@ class SwitchBotCloudSensor(SwitchBotCloudEntity, SensorEntity):
self.entity_description = description
self._attr_unique_id = f"{device.device_id}_{description.key}"
@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
def _set_attributes(self) -> None:
"""Set attributes from coordinator data."""
if not self.coordinator.data:
return
self._attr_native_value = self.coordinator.data.get(self.entity_description.key)
self.async_write_ha_state()

View File

@ -46,21 +46,18 @@ class SwitchBotCloudSwitch(SwitchBotCloudEntity, SwitchEntity):
self._attr_is_on = False
self.async_write_ha_state()
@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
def _set_attributes(self) -> None:
"""Set attributes from coordinator data."""
if not self.coordinator.data:
return
self._attr_is_on = self.coordinator.data.get("power") == PowerState.ON.value
self.async_write_ha_state()
class SwitchBotCloudRemoteSwitch(SwitchBotCloudSwitch):
"""Representation of a SwitchBot switch provider by a remote."""
@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
def _set_attributes(self) -> None:
"""Set attributes from coordinator data."""
class SwitchBotCloudPlugSwitch(SwitchBotCloudSwitch):
@ -72,13 +69,11 @@ class SwitchBotCloudPlugSwitch(SwitchBotCloudSwitch):
class SwitchBotCloudRelaySwitchSwitch(SwitchBotCloudSwitch):
"""Representation of a SwitchBot relay switch."""
@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
def _set_attributes(self) -> None:
"""Set attributes from coordinator data."""
if not self.coordinator.data:
return
self._attr_is_on = self.coordinator.data.get("switchStatus") == 1
self.async_write_ha_state()
@callback

View File

@ -99,9 +99,8 @@ class SwitchBotCloudVacuum(SwitchBotCloudEntity, StateVacuumEntity):
"""Start or resume the cleaning task."""
await self.send_api_command(VacuumCommands.START)
@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
def _set_attributes(self) -> None:
"""Set attributes from coordinator data."""
if not self.coordinator.data:
return
@ -111,8 +110,6 @@ class SwitchBotCloudVacuum(SwitchBotCloudEntity, StateVacuumEntity):
switchbot_state = str(self.coordinator.data.get("workingStatus"))
self._attr_activity = VACUUM_SWITCHBOT_STATE_TO_HA_STATE.get(switchbot_state)
self.async_write_ha_state()
@callback
def _async_make_entity(