Repository: home-assistant/core
Pull request: Allow duplicate state updates when force_update is set on an esphome sensor
## Proposed change
<!--
  Describe the big picture of your changes here to communicate to the
  maintainers why we should accept this pull request. If it fixes a bug
  or resolves a feature request, be sure to link to that issue in the
  additional information section.
-->
fixes #91221

`sensor` appears to be the only special case so I did not build a more generalized solution here.

```
aioesphomeapi % git grep force_update     
aioesphomeapi/api.proto:  bool force_update = 8;
aioesphomeapi/model.py:    force_update: bool = False
```


## Type of change
<!--
  What type of change does your PR introduce to Home Assistant?
  NOTE: Please, check only 1! box!
  If your PR requires multiple boxes to be checked, you'll most likely need to
  split it into multiple PRs. This makes things easier and faster to code review.
-->

- [ ] Dependency upgrade
- [x] Bugfix (non-breaking change which fixes an issue)
- [ ] New integration (thank you!)
- [ ] New feature (which adds functionality to an existing integration)
- [ ] Deprecation (breaking change to happen in the future)
- [ ] Breaking change (fix/feature causing existing functionality to break)
- [ ] Code quality improvements to existing code or addition of tests

## Additional information
<!--
  Details are important, and help maintainers processing your PR.
  Please be sure to fill out additional details, if applicable.
-->

- This PR fixes or closes issue: fixes #
- This PR is related to issue: 
- Link to documentation pull request: 

## Checklist
<!--
  Put an `x` in the boxes that apply. You can also fill these out after
  creating the PR. If you're unsure about any of them, don't hesitate to ask.
  We're here to help! This is simply a reminder of what we are going to look
  for before merging your code.
-->

- [x] The code change is tested and works locally.
- [ ] Local tests pass. **Your PR cannot be merged unless tests pass**
- [ ] There is no commented out code in this PR.
- [ ] I have followed the [development checklist][dev-checklist]
- [ ] I have followed the [perfect PR recommendations][perfect-pr]
- [ ] The code has been formatted using Black (`black --fast homeassistant tests`)
- [ ] Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

- [ ] Documentation added/updated for [www.home-assistant.io][docs-repository]

If the code communicates with devices, web services, or third-party tools:

- [ ] The [manifest file][manifest-docs] has all fields filled out correctly.  
      Updated and included derived files by running: `python3 -m script.hassfest`.
- [ ] New or updated dependencies have been added to `requirements_all.txt`.  
      Updated by running `python3 -m script.gen_requirements_all`.
- [ ] For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.
- [ ] Untested files have been added to `.coveragerc`.

<!--
  This project is very active and we have a high turnover of pull requests.

  Unfortunately, the number of incoming pull requests is higher than what our
  reviewers can review and merge so there is a long backlog of pull requests
  waiting for review. You can help here!
  
  By reviewing another pull request, you will help raise the code quality of
  that pull request and the final review will be faster. This way the general
  pace of pull request reviews will go up and your wait time will go down.
  
  When picking a pull request to review, try to choose one that hasn't yet
  been reviewed.

  Thanks for helping out!
-->

To help with the load of incoming pull requests:

- [ ] I have reviewed two other [open pull requests][prs] in this repository.

[prs]: https://github.com/home-assistant/core/pulls?q=is%3Aopen+is%3Apr+-author%3A%40me+-draft%3Atrue+-label%3Awaiting-for-upstream+sort%3Acreated-desc+review%3Anone+-status%3Afailure

<!--
  Thank you for contributing <3

  Below, some useful links you could explore:
-->
[dev-checklist]: https://developers.home-assistant.io/docs/development_checklist/
[manifest-docs]: https://developers.home-assistant.io/docs/creating_integration_manifest/
[quality-scale]: https://developers.home-assistant.io/docs/integration_quality_scale_index/
[docs-repository]: https://github.com/home-assistant/home-assistant.io
[perfect-pr]: https://developers.home-assistant.io/docs/review-process/#creating-the-perfect-pr


Problem statement:
ESPHome sensor force update option not working
### The problem

With the force update option set to true, the state of the sensor in Home Assistant don't change when the value stayed the same.


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

core-2023.4.2

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

core-2023.3.6

### What type of installation are you running?

Home Assistant OS

### Integration causing the issue

_No response_

### Link to integration documentation on our website

_No response_

### 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 from issue discussion:
Have the same issue. Gone nuts because of this.

I use bluetooth temperature sensors all over the house and I rely on the "last update" time to decide whether a sensor has gone offline.

Since a few days  some sensors started to go offline and could not understand what happened.
Changed the ESP32 to a model with Ethernet, so I get better bluetooth signal without WiFi interference.
Just to see now that the behavior is the same, with a sensor which is 2m close to the ESP.

Looking at the ESPHome log I see that it received temperature data a few seconds ago (the same value, of couse, as the HA sensor).
The HA sensor however has it's last_updated time set to 40 minutes ago.

Config of the ESPHome sensor:
```
sensor:
  - platform: xiaomi_lywsd02
    mac_address: "E7:2E:00:A0:A8:E3"
    temperature:
      name: "Temperature Bedroom"
      accuracy_decimals: 1
      force_update: true
```
ESPHome log:
`[13:48:54][D][sensor:127]: 'Temperature Bedroom': Sending state 20.70000 °C with 1 decimals of accuracy`

Home Assistant sensor entity:
`{{ states.sensor.ble_temperature_bedroom }}: <template TemplateState(<state sensor.ble_temperature_bedroom=20.7; state_class=measurement, unit_of_measurement=°C, device_class=temperature, friendly_name=Bedroom temperature @ 2023-04-13T13:10:57.368251+03:00>)>`
The above problem also applies to my ESPHOME (20pcs). 
The problem appeared on 2023.04.06. 
Has anyone managed to solve the problem?
Same issue here and can confirm previous versions work.
I have 3 HA instances reading the same ESPHome sensors (all of them have force_update set to true)
2 HA instances update properly the value of the sensors even when it doesn't change, one of them isn't.

**The one that isn't working:**
HA: `core-2023.4.4`
ESPHome: `2023.4.1`
According to my sensor data, it stopped working properly on April 17th (most likely when I updated from 2023.3.x)

**First one that is working:**
HA: `core-2023.1.4`
ESPHome: `2023.4.1`

**Second one that is working:**
HA: `core-2023.1.7`
ESPHome: `2022.12.6`
I think that #89779 broke this.  As far as I can see, [this change](https://github.com/home-assistant/core/pull/89779/files#diff-4d19c88a20eb409c17b92be7229f4e91eeacd82b1037fc442f551bb65f5f9b00R206) will drop all duplicate updates, regardless of whether the entity has `force_update` set.

@bdraco can you comment on this? 

I would test a fix/revert of this since it is vital to combat [this behavior](https://github.com/home-assistant/core/issues/58130#issuecomment-1531647909), I need my force updates!
I expect its definitely that change causing the issue. We can add a check for `self.info` and allow the duplicate update if has the force update flag set.

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

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


Code owners of `esphome` 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 esphome` Removes the current integration label and assignees on the issue, add the integration domain after the command.

</details>


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

---

[esphome documentation](https://www.home-assistant.io/integrations/esphome)
[esphome source](https://github.com/home-assistant/core/tree/dev/homeassistant/components/esphome)
<sub><sup>(message by IssueLinks)</sup></sub>
Thats not going to work because info is indexed by platform and we don't know which platform the state update is for at that point.

The other option is to revert the change in https://github.com/home-assistant/core/pull/89779 but I don't want to do that since we already have a problem with state updates generating too much load (esp power sensors) and that would make the problem worse.

Commit message: Allow duplicate states when force_update is set on an esphome sensor

fixes #91221

Patch to review:
diff --git a/homeassistant/components/esphome/entry_data.py b/homeassistant/components/esphome/entry_data.py
index 61d6262250c0e1..64a7f150222cb4 100644
--- a/homeassistant/components/esphome/entry_data.py
+++ b/homeassistant/components/esphome/entry_data.py
@@ -25,6 +25,7 @@
     NumberInfo,
     SelectInfo,
     SensorInfo,
+    SensorState,
     SwitchInfo,
     TextSensorInfo,
     UserService,
@@ -240,7 +241,16 @@ def async_update_state(self, state: EntityState) -> None:
         current_state_by_type = self.state[state_type]
         current_state = current_state_by_type.get(key, _SENTINEL)
         subscription_key = (state_type, key)
-        if current_state == state and subscription_key not in stale_state:
+        if (
+            current_state == state
+            and subscription_key not in stale_state
+            and not (
+                type(state) is SensorState  # pylint: disable=unidiomatic-typecheck
+                and (platform_info := self.info.get(Platform.SENSOR))
+                and (entity_info := platform_info.get(state.key))
+                and (cast(SensorInfo, entity_info)).force_update
+            )
+        ):
             _LOGGER.debug(
                 "%s: ignoring duplicate update with and key %s: %s",
                 self.name,


Code review comments:
diff_hunk: @@ -240,7 +241,16 @@ def async_update_state(self, state: EntityState) -> None:
         current_state_by_type = self.state[state_type]
         current_state = current_state_by_type.get(key, _SENTINEL)
         subscription_key = (state_type, key)
-        if current_state == state and subscription_key not in stale_state:
+        if (
+            current_state == state
+            and subscription_key not in stale_state
+            and not (
+                type(state) is SensorState  # pylint: disable=unidiomatic-typecheck
+                and (platform_info := self.info.get(Platform.SENSOR))
+                and (entity_info := platform_info.get(state.key))
+                and (cast(SensorInfo, entity_info)).force_update
+            )
+        ):
             _LOGGER.debug(
                 "%s: ignoring duplicate update with and key %s: %s",
path: homeassistant/components/esphome/entry_data.py
text: @user1:
```suggestion
                "%s: ignoring duplicate update with key %s: %s",
```