diff --git a/custom_components/bambu_lab/coordinator.py b/custom_components/bambu_lab/coordinator.py index 1185063c..5c3cd22c 100644 --- a/custom_components/bambu_lab/coordinator.py +++ b/custom_components/bambu_lab/coordinator.py @@ -44,7 +44,8 @@ def __init__(self, hass, *, entry: ConfigEntry) -> None: username = entry.options['username'], auth_token = entry.options['auth_token'], access_code = entry.options['access_code'], - usage_hours = self.latest_usage_hours) + usage_hours = self.latest_usage_hours, + manual_refresh_mode = entry.options.get('manual_refresh_mode', False)) self._updatedDevice = False self.data = self.get_model() @@ -267,3 +268,13 @@ def get_virtual_tray_device(self): hw_version="", sw_version="" ) + + async def set_manual_refresh_mode(self, manual_refresh_mode): + await self.client.set_manual_refresh_mode(manual_refresh_mode) + options = dict(self.config_entry.options) + options['manual_refresh_mode'] = manual_refresh_mode + self._hass.config_entries.async_update_entry( + entry=self.config_entry, + title=self.get_model().info.serial, + data=self.config_entry.data, + options=options) diff --git a/custom_components/bambu_lab/pybambu/bambu_client.py b/custom_components/bambu_lab/pybambu/bambu_client.py index fc3014b5..a3378907 100644 --- a/custom_components/bambu_lab/pybambu/bambu_client.py +++ b/custom_components/bambu_lab/pybambu/bambu_client.py @@ -270,7 +270,7 @@ class BambuClient: _usage_hours: float def __init__(self, device_type: str, serial: str, host: str, local_mqtt: bool, region: str, email: str, - username: str, auth_token: str, access_code: str, usage_hours: float = 0): + username: str, auth_token: str, access_code: str, usage_hours: float = 0, manual_refresh_mode: bool = False): self.callback = None self.host = host self._local_mqtt = local_mqtt @@ -283,7 +283,7 @@ def __init__(self, device_type: str, serial: str, host: str, local_mqtt: bool, r self._usage_hours = usage_hours self._port = 1883 self._refreshed = False - self._manual_refresh_mode = False + self._manual_refresh_mode = manual_refresh_mode self._device = Device(self) self.bambu_cloud = BambuCloud(region, email, username, auth_token) diff --git a/custom_components/bambu_lab/switch.py b/custom_components/bambu_lab/switch.py index 573f53e4..b31c77ee 100644 --- a/custom_components/bambu_lab/switch.py +++ b/custom_components/bambu_lab/switch.py @@ -58,6 +58,14 @@ class BambuLabManualModeSwitch(BambuLabSwitch): entity_description = MANUAL_REFRESH_MODE_SWITCH_DESCRIPTION + def __init__( + self, + coordinator: BambuDataUpdateCoordinator, + config_entry: ConfigEntry + ) -> None: + super().__init__(coordinator, config_entry) + self._attr_is_on = self.coordinator.client.manual_refresh_mode + @property def available(self) -> bool: return self.coordinator.get_model().info.mqtt_mode == "local" @@ -70,9 +78,9 @@ def icon(self) -> str: async def async_turn_on(self, **kwargs: Any) -> None: """Enable manual refresh mode.""" self._attr_is_on = not self.coordinator.client.manual_refresh_mode - await self.coordinator.client.set_manual_refresh_mode(True) + await self.coordinator.set_manual_refresh_mode(True) async def async_turn_off(self, **kwargs: Any) -> None: """Disable manual refresh mode.""" self._attr_is_on = not self.coordinator.client.manual_refresh_mode - await self.coordinator.client.set_manual_refresh_mode(False) + await self.coordinator.set_manual_refresh_mode(False)