Repository: home-assistant/core
Issue category: Performance Issue

Issue:
Fritz!box sockets switch very slowly
### The problem

If I want to switch on a Fritz!Box socket via Home Assistant, it takes a very long time until it is switched on and the entity is updated.

### What version of Home Assistant Core has the issue?

core-2024.2.5

### What was the last working version of Home Assistant Core?

_No response_

### What type of installation are you running?

Home Assistant OS

### Integration causing the issue

AVM FRITZ!SmartHome

### Link to integration documentation on our website

https://www.home-assistant.io/integrations/fritzbox/

### Diagnostics information

_No response_

### Example YAML snippet

_No response_

### Anything in the logs that might be useful for us?

_No response_

### Additional information

_No response_


Hints:

Hey there @mib1185, @flabbamann, mind taking a look at this issue as it has been labeled with an integration (`fritzbox`) you are listed as a [code owner](https://github.com/home-assistant/core/blob/dev/CODEOWNERS#L446) for? Thanks!

<details>
<summary>Code owner commands</summary>


Code owners of `fritzbox` can trigger bot actions by commenting:

- `@home-assistant close` Closes the issue.
- `@home-assistant rename Awesome new title` Renames the issue.
- `@home-assistant reopen` Reopen the issue.
- `@home-assistant unassign fritzbox` Removes the current integration label and assignees on the issue, add the integration domain after the command.
- `@home-assistant add-label needs-more-information` Add a label (needs-more-information, problem in dependency, problem in custom component) to the issue.
- `@home-assistant remove-label needs-more-information` Remove a label (needs-more-information, problem in dependency, problem in custom component) on the issue.

</details>


<sub><sup>(message by CodeOwnersMention)</sup></sub>

---

[fritzbox documentation](https://www.home-assistant.io/integrations/fritzbox)
[fritzbox source](https://github.com/home-assistant/core/tree/dev/homeassistant/components/fritzbox)
<sub><sup>(message by IssueLinks)</sup></sub>
What do you mean with "very long"? Further is there a delay between toggle the switch in HA and the real devices switches?
Maybe that's the same issue as I have:

### The problem

I have a FRITZ!DECT 200 running on a FritzBox 4060.

When I activate the switch in HA, the physical and the graphical switch get activated.
But after 1-2 seconds, the graphical switch in HA toggles back to disabled, but the physical switch stays active.
When I then activate the graphical switch in HA again, it stays activate as well as the physical switch.

Disabling the switch later on, behaves the same, just the other way around.

### What version of Home Assistant Core has the issue?

I have fresh installed version 2024.3.3.

### What was the last working version of Home Assistant Core?

As I am new to Home Assistant, it never worked for me.

### What type of installation are you running?

Standard Home Assistant OS on a Raspberry Pi 5.


Please let me know what other information I can provide to help you here.
The delay is as rincewind77 describes it. „When I activate the switch in HA, the physical and the graphical switch get activated. But after 1-2 seconds, the graphical switch in HA toggles back to disabled, but the physical switch stays active.“ After 30 seconds later the graficial switch in HA entity goes back to the on state as it is the real switch.

Have the same Problems like rincewind77 described.
FritzBox 7590 with Fritz Dect 200 and 210 (6 Devices)

Also the refresh of the Values (actual Watts) some Times refresh in 5 to 10 Seconds - Sometimes in 5 - 30 Minutes (??)

Actual HA running in a virtual Maschine on a Synology NAS - all Ressources (CPU RAM) are enough free Space.
Hi @t785634 

please enable [debug logging](https://www.home-assistant.io/docs/configuration/troubleshooting/#debug-logs-and-diagnostics),  toggle the switch in HA, wait ~90s and than provide the log file (_will automatically be downloaded when disabling debug logging_).

Note: it is better to drag the log into the comment (_which will add it as an attachment_) and not copy paste as it is hard to read logs in GitHub.

thx 👍
I ran the debug procedure on my HA.
Please find the debug logs attached.
[home-assistant_fritzbox_2024-04-02T16-43-04.487Z.log](https://github.com/home-assistant/core/files/14841191/home-assistant_fritzbox_2024-04-02T16-43-04.487Z.log)

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates.
Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍
This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.
For me the problem was not solved with version 2024.4.3
Updating to version 2024.4.3 did also not fix the problem for me.
@Rincewind77 the provided log does not met the requested log in https://github.com/home-assistant/core/issues/113749#issuecomment-2030544613 - it does not show any switch toggle action
@t785634 do you mind to also provide the requested debug log?
@mib1185 sure, attached the latest log.
I hope I did it correct this time.
I talk about a "FRITZ!DECT 200" switch btw.

[home-assistant_fritzbox_2024-04-23T17-29-34.044Z.log](https://github.com/home-assistant/core/files/15080607/home-assistant_fritzbox_2024-04-23T17-29-34.044Z.log)

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates.
Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍
This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.
With version 2024.5.2 I still have the same issue.
I also have this issue. For me it also leads to the problem, that a script that turns multiple devices on or off doesn't work reliably with the FRITZ device.
> @t785634 do you mind to also provide the requested debug log?

Do you still need my log or do you use rincewind77s log?
There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates.
Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍
This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.
With version 2024.5.5 I still have the same issue.
There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates.
Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍
This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.
Before I used Home Assistant I wrote my own tool to set and display the state of the FritzBox devices. My tool polls every 5 seconds the state of the devices. When I trigger a device, I force the local state to the new state and reset the timer for the polling. After 5 seconds the next poll will be executed and the state is all the time correct. This worked perfectly. 

Now using Home Assistant I have the same problem that many users have. I trigger the switch and it jumps back to its old position. After 30 seconds the new position is shown. I think the time between activating the device and polling the status is just too short. It would be nice if this time could be configured.

This belongs for me only to the status update. As soon I trigger a switch it is immeditaly activated / deactivated.
@mib1185 Do you still need my log or do you use rincewind77s log? Because the issue is labeled with [needs-more-information]
> @mib1185 Do you still need my log or do you use rincewind77s log? Because the issue is labeled with [needs-more-information]

In the meantime I replaced all my sockets with Tasmotas. So the problem is gone. Thank you! 
There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates.
Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍
This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.
With version 2024.7.2 I still have the same issue.
Is it possible to increase the update interval?
There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates.
Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍
This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.
With version 2024.7.4 I still have the same issue.
There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates.
Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍
This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.
in progress
Are there any updates?
Hello,
I have the same issue.
HA Version 2024.11.0
FritzBox: 6591 Cable
Device: FRITZ!DECT 210
I have the same issue. Its irritating, because the switch icon shows the wrong state for a while. 
HA Version 2024.11.0
FritzBox: 7690
Device: IKEA TRETAKT#1 (ZigBee)
Hope an update of the AVM FRITZ!SmartHome Integration will fix ist...
With version 2024.11.3 I still have the same issue.

Patch:
diff --git a/homeassistant/components/fritzbox/climate.py b/homeassistant/components/fritzbox/climate.py
index d5a81fdef1a3d3..87a87ac691f58b 100644
--- a/homeassistant/components/fritzbox/climate.py
+++ b/homeassistant/components/fritzbox/climate.py
@@ -141,7 +141,7 @@ async def async_set_temperature(self, **kwargs: Any) -> None:
             await self.async_set_hvac_mode(hvac_mode)
         elif target_temp is not None:
             await self.hass.async_add_executor_job(
-                self.data.set_target_temperature, target_temp
+                self.data.set_target_temperature, target_temp, True
             )
         else:
             return
diff --git a/homeassistant/components/fritzbox/cover.py b/homeassistant/components/fritzbox/cover.py
index de87d6f8852e43..070bb8682982b7 100644
--- a/homeassistant/components/fritzbox/cover.py
+++ b/homeassistant/components/fritzbox/cover.py
@@ -71,21 +71,21 @@ def is_closed(self) -> bool | None:
 
     async def async_open_cover(self, **kwargs: Any) -> None:
         """Open the cover."""
-        await self.hass.async_add_executor_job(self.data.set_blind_open)
+        await self.hass.async_add_executor_job(self.data.set_blind_open, True)
         await self.coordinator.async_refresh()
 
     async def async_close_cover(self, **kwargs: Any) -> None:
         """Close the cover."""
-        await self.hass.async_add_executor_job(self.data.set_blind_close)
+        await self.hass.async_add_executor_job(self.data.set_blind_close, True)
         await self.coordinator.async_refresh()
 
     async def async_set_cover_position(self, **kwargs: Any) -> None:
         """Move the cover to a specific position."""
         await self.hass.async_add_executor_job(
-            self.data.set_level_percentage, 100 - kwargs[ATTR_POSITION]
+            self.data.set_level_percentage, 100 - kwargs[ATTR_POSITION], True
         )
 
     async def async_stop_cover(self, **kwargs: Any) -> None:
         """Stop the cover."""
-        await self.hass.async_add_executor_job(self.data.set_blind_stop)
+        await self.hass.async_add_executor_job(self.data.set_blind_stop, True)
         await self.coordinator.async_refresh()
diff --git a/homeassistant/components/fritzbox/light.py b/homeassistant/components/fritzbox/light.py
index 36cb7dc8cff047..f6a1ba4cc944f4 100644
--- a/homeassistant/components/fritzbox/light.py
+++ b/homeassistant/components/fritzbox/light.py
@@ -122,7 +122,7 @@ async def async_turn_on(self, **kwargs: Any) -> None:
         """Turn the light on."""
         if kwargs.get(ATTR_BRIGHTNESS) is not None:
             level = kwargs[ATTR_BRIGHTNESS]
-            await self.hass.async_add_executor_job(self.data.set_level, level)
+            await self.hass.async_add_executor_job(self.data.set_level, level, True)
         if kwargs.get(ATTR_HS_COLOR) is not None:
             # Try setunmappedcolor first. This allows free color selection,
             # but we don't know if its supported by all devices.
@@ -133,7 +133,10 @@ async def async_turn_on(self, **kwargs: Any) -> None:
                     cast(float, kwargs[ATTR_HS_COLOR][1]) * 255.0 / 100.0
                 )
                 await self.hass.async_add_executor_job(
-                    self.data.set_unmapped_color, (unmapped_hue, unmapped_saturation)
+                    self.data.set_unmapped_color,
+                    (unmapped_hue, unmapped_saturation),
+                    0,
+                    True,
                 )
             # This will raise 400 BAD REQUEST if the setunmappedcolor is not available
             except HTTPError as err:
@@ -152,18 +155,18 @@ async def async_turn_on(self, **kwargs: Any) -> None:
                     key=lambda x: abs(x - unmapped_saturation),
                 )
                 await self.hass.async_add_executor_job(
-                    self.data.set_color, (hue, saturation)
+                    self.data.set_color, (hue, saturation), 0, True
                 )
 
         if kwargs.get(ATTR_COLOR_TEMP_KELVIN) is not None:
             await self.hass.async_add_executor_job(
-                self.data.set_color_temp, kwargs[ATTR_COLOR_TEMP_KELVIN]
+                self.data.set_color_temp, kwargs[ATTR_COLOR_TEMP_KELVIN], 0, True
             )
 
-        await self.hass.async_add_executor_job(self.data.set_state_on)
+        await self.hass.async_add_executor_job(self.data.set_state_on, True)
         await self.coordinator.async_refresh()
 
     async def async_turn_off(self, **kwargs: Any) -> None:
         """Turn the light off."""
-        await self.hass.async_add_executor_job(self.data.set_state_off)
+        await self.hass.async_add_executor_job(self.data.set_state_off, True)
         await self.coordinator.async_refresh()
diff --git a/homeassistant/components/fritzbox/switch.py b/homeassistant/components/fritzbox/switch.py
index 18b676d449ead9..d83793c77dc096 100644
--- a/homeassistant/components/fritzbox/switch.py
+++ b/homeassistant/components/fritzbox/switch.py
@@ -51,13 +51,13 @@ def is_on(self) -> bool:
     async def async_turn_on(self, **kwargs: Any) -> None:
         """Turn the switch on."""
         self.check_lock_state()
-        await self.hass.async_add_executor_job(self.data.set_switch_state_on)
+        await self.hass.async_add_executor_job(self.data.set_switch_state_on, True)
         await self.coordinator.async_refresh()
 
     async def async_turn_off(self, **kwargs: Any) -> None:
         """Turn the switch off."""
         self.check_lock_state()
-        await self.hass.async_add_executor_job(self.data.set_switch_state_off)
+        await self.hass.async_add_executor_job(self.data.set_switch_state_off, True)
         await self.coordinator.async_refresh()
 
     def check_lock_state(self) -> None:


Test patch:
diff --git a/tests/components/fritzbox/test_climate.py b/tests/components/fritzbox/test_climate.py
index 29f5742216fb84..c7896920ce9364 100644
--- a/tests/components/fritzbox/test_climate.py
+++ b/tests/components/fritzbox/test_climate.py
@@ -273,20 +273,20 @@ async def test_update_error(hass: HomeAssistant, fritz: Mock) -> None:
 @pytest.mark.parametrize(
     ("service_data", "expected_call_args"),
     [
-        ({ATTR_TEMPERATURE: 23}, [call(23)]),
+        ({ATTR_TEMPERATURE: 23}, [call(23, True)]),
         (
             {
                 ATTR_HVAC_MODE: HVACMode.OFF,
                 ATTR_TEMPERATURE: 23,
             },
-            [call(0)],
+            [call(0, True)],
         ),
         (
             {
                 ATTR_HVAC_MODE: HVACMode.HEAT,
                 ATTR_TEMPERATURE: 23,
             },
-            [call(23)],
+            [call(23, True)],
         ),
     ],
 )
@@ -316,14 +316,14 @@ async def test_set_temperature(
     ("service_data", "target_temperature", "current_preset", "expected_call_args"),
     [
         # mode off always sets target temperature to 0
-        ({ATTR_HVAC_MODE: HVACMode.OFF}, 22, PRESET_COMFORT, [call(0)]),
-        ({ATTR_HVAC_MODE: HVACMode.OFF}, 16, PRESET_ECO, [call(0)]),
-        ({ATTR_HVAC_MODE: HVACMode.OFF}, 16, None, [call(0)]),
+        ({ATTR_HVAC_MODE: HVACMode.OFF}, 22, PRESET_COMFORT, [call(0, True)]),
+        ({ATTR_HVAC_MODE: HVACMode.OFF}, 16, PRESET_ECO, [call(0, True)]),
+        ({ATTR_HVAC_MODE: HVACMode.OFF}, 16, None, [call(0, True)]),
         # mode heat sets target temperature based on current scheduled preset,
         # when not already in mode heat
-        ({ATTR_HVAC_MODE: HVACMode.HEAT}, 0.0, PRESET_COMFORT, [call(22)]),
-        ({ATTR_HVAC_MODE: HVACMode.HEAT}, 0.0, PRESET_ECO, [call(16)]),
-        ({ATTR_HVAC_MODE: HVACMode.HEAT}, 0.0, None, [call(22)]),
+        ({ATTR_HVAC_MODE: HVACMode.HEAT}, 0.0, PRESET_COMFORT, [call(22, True)]),
+        ({ATTR_HVAC_MODE: HVACMode.HEAT}, 0.0, PRESET_ECO, [call(16, True)]),
+        ({ATTR_HVAC_MODE: HVACMode.HEAT}, 0.0, None, [call(22, True)]),
         # mode heat does not set target temperature, when already in mode heat
         ({ATTR_HVAC_MODE: HVACMode.HEAT}, 16, PRESET_COMFORT, []),
         ({ATTR_HVAC_MODE: HVACMode.HEAT}, 16, PRESET_ECO, []),
@@ -380,7 +380,7 @@ async def test_set_preset_mode_comfort(hass: HomeAssistant, fritz: Mock) -> None
         {ATTR_ENTITY_ID: ENTITY_ID, ATTR_PRESET_MODE: PRESET_COMFORT},
         True,
     )
-    assert device.set_target_temperature.call_args_list == [call(22)]
+    assert device.set_target_temperature.call_args_list == [call(22, True)]
 
 
 async def test_set_preset_mode_eco(hass: HomeAssistant, fritz: Mock) -> None:
@@ -396,7 +396,7 @@ async def test_set_preset_mode_eco(hass: HomeAssistant, fritz: Mock) -> None:
         {ATTR_ENTITY_ID: ENTITY_ID, ATTR_PRESET_MODE: PRESET_ECO},
         True,
     )
-    assert device.set_target_temperature.call_args_list == [call(16)]
+    assert device.set_target_temperature.call_args_list == [call(16, True)]
 
 
 async def test_preset_mode_update(hass: HomeAssistant, fritz: Mock) -> None:
diff --git a/tests/components/fritzbox/test_cover.py b/tests/components/fritzbox/test_cover.py
index f26e65fc28a33d..087206af5e6278 100644
--- a/tests/components/fritzbox/test_cover.py
+++ b/tests/components/fritzbox/test_cover.py
@@ -99,7 +99,7 @@ async def test_set_position_cover(hass: HomeAssistant, fritz: Mock) -> None:
         {ATTR_ENTITY_ID: ENTITY_ID, ATTR_POSITION: 50},
         True,
     )
-    assert device.set_level_percentage.call_args_list == [call(50)]
+    assert device.set_level_percentage.call_args_list == [call(50, True)]
 
 
 async def test_stop_cover(hass: HomeAssistant, fritz: Mock) -> None:
diff --git a/tests/components/fritzbox/test_light.py b/tests/components/fritzbox/test_light.py
index 84fafe25521072..474fa6897107ac 100644
--- a/tests/components/fritzbox/test_light.py
+++ b/tests/components/fritzbox/test_light.py
@@ -155,8 +155,8 @@ async def test_turn_on(hass: HomeAssistant, fritz: Mock) -> None:
     assert device.set_state_on.call_count == 1
     assert device.set_level.call_count == 1
     assert device.set_color_temp.call_count == 1
-    assert device.set_color_temp.call_args_list == [call(3000)]
-    assert device.set_level.call_args_list == [call(100)]
+    assert device.set_color_temp.call_args_list == [call(3000, 0, True)]
+    assert device.set_level.call_args_list == [call(100, True)]
 
 
 async def test_turn_on_color(hass: HomeAssistant, fritz: Mock) -> None:
@@ -178,9 +178,9 @@ async def test_turn_on_color(hass: HomeAssistant, fritz: Mock) -> None:
     assert device.set_state_on.call_count == 1
     assert device.set_level.call_count == 1
     assert device.set_unmapped_color.call_count == 1
-    assert device.set_level.call_args_list == [call(100)]
+    assert device.set_level.call_args_list == [call(100, True)]
     assert device.set_unmapped_color.call_args_list == [
-        call((100, round(70 * 255.0 / 100.0)))
+        call((100, round(70 * 255.0 / 100.0)), 0, True)
     ]
 
 
@@ -212,8 +212,8 @@ async def test_turn_on_color_unsupported_api_method(
     assert device.set_state_on.call_count == 1
     assert device.set_level.call_count == 1
     assert device.set_color.call_count == 1
-    assert device.set_level.call_args_list == [call(100)]
-    assert device.set_color.call_args_list == [call((100, 70))]
+    assert device.set_level.call_args_list == [call(100, True)]
+    assert device.set_color.call_args_list == [call((100, 70), 0, True)]
 
     # test for unknown error
     error.response.status_code = 500