Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make manual refresh mode persistent #464

Merged
merged 2 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion custom_components/bambu_lab/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
4 changes: 2 additions & 2 deletions custom_components/bambu_lab/pybambu/bambu_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand Down
12 changes: 10 additions & 2 deletions custom_components/bambu_lab/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Loading