Skip to content

Commit

Permalink
Call async_forward_setup_entry after the first refresh in SwitchBot C…
Browse files Browse the repository at this point in the history
  • Loading branch information
mckbrmn authored Jan 15, 2025
1 parent e83ee00 commit 6a50648
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 27 deletions.
2 changes: 1 addition & 1 deletion homeassistant/components/switchbot_cloud/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
15 changes: 15 additions & 0 deletions homeassistant/components/switchbot_cloud/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

Expand Down Expand Up @@ -48,3 +49,17 @@ async def send_api_command(
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()
8 changes: 3 additions & 5 deletions homeassistant/components/switchbot_cloud/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

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
Expand All @@ -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."""
Expand Down
8 changes: 3 additions & 5 deletions homeassistant/components/switchbot_cloud/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
UnitOfPower,
UnitOfTemperature,
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from . import SwitchbotCloudData
Expand Down Expand Up @@ -166,10 +166,8 @@ def __init__(
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()
17 changes: 6 additions & 11 deletions homeassistant/components/switchbot_cloud/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,18 @@ async def async_turn_off(self, **kwargs: Any) -> None:
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):
Expand All @@ -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
Expand Down
7 changes: 2 additions & 5 deletions homeassistant/components/switchbot_cloud/vacuum.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,8 @@ async def async_start(self) -> None:
"""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

Expand All @@ -111,8 +110,6 @@ def _handle_coordinator_update(self) -> None:
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(
Expand Down

0 comments on commit 6a50648

Please sign in to comment.