From 8c0b44d6d52259a1c284adc3a1df77b0c07ab7c6 Mon Sep 17 00:00:00 2001 From: gibwar Date: Mon, 8 Apr 2024 02:05:46 -0600 Subject: [PATCH] Only reset requested utility meter with no tariff (#115170) --- .../components/utility_meter/sensor.py | 8 +- tests/components/utility_meter/test_sensor.py | 133 ++++++++++++++++++ 2 files changed, 140 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/utility_meter/sensor.py b/homeassistant/components/utility_meter/sensor.py index 26582df1b448f..014cd93b53b97 100644 --- a/homeassistant/components/utility_meter/sensor.py +++ b/homeassistant/components/utility_meter/sensor.py @@ -578,7 +578,13 @@ async def _async_reset_meter(self, event): async def async_reset_meter(self, entity_id): """Reset meter.""" - if self._tariff is not None and self._tariff_entity != entity_id: + if self._tariff_entity is not None and self._tariff_entity != entity_id: + return + if ( + self._tariff_entity is None + and entity_id is not None + and self.entity_id != entity_id + ): return _LOGGER.debug("Reset utility meter <%s>", self.entity_id) self._last_reset = dt_util.utcnow() diff --git a/tests/components/utility_meter/test_sensor.py b/tests/components/utility_meter/test_sensor.py index 99a63809329ee..43a71eca85e06 100644 --- a/tests/components/utility_meter/test_sensor.py +++ b/tests/components/utility_meter/test_sensor.py @@ -983,6 +983,139 @@ async def test_service_reset_no_tariffs( assert state.attributes.get("last_period") == "3" +@pytest.mark.parametrize( + ("yaml_config", "config_entry_configs"), + [ + ( + { + "utility_meter": { + "energy_bill": { + "source": "sensor.energy", + }, + "water_bill": { + "source": "sensor.water", + }, + }, + }, + None, + ), + ( + None, + [ + { + "cycle": "none", + "delta_values": False, + "name": "Energy bill", + "net_consumption": False, + "offset": 0, + "periodically_resetting": True, + "source": "sensor.energy", + "tariffs": [], + }, + { + "cycle": "none", + "delta_values": False, + "name": "Water bill", + "net_consumption": False, + "offset": 0, + "periodically_resetting": True, + "source": "sensor.water", + "tariffs": [], + }, + ], + ), + ], +) +async def test_service_reset_no_tariffs_correct_with_multi( + hass: HomeAssistant, yaml_config, config_entry_configs +) -> None: + """Test complex utility sensor service reset for multiple sensors with no tarrifs. + + See GitHub issue #114864: Service "utility_meter.reset" affects all meters. + """ + + # Home assistant is not runnit yet + hass.state = CoreState.not_running + last_reset = "2023-10-01T00:00:00+00:00" + + mock_restore_cache_with_extra_data( + hass, + [ + ( + State( + "sensor.energy_bill", + "3", + attributes={ + ATTR_LAST_RESET: last_reset, + }, + ), + {}, + ), + ( + State( + "sensor.water_bill", + "6", + attributes={ + ATTR_LAST_RESET: last_reset, + }, + ), + {}, + ), + ], + ) + + if yaml_config: + assert await async_setup_component(hass, DOMAIN, yaml_config) + await hass.async_block_till_done() + else: + for entry in config_entry_configs: + config_entry = MockConfigEntry( + data={}, + domain=DOMAIN, + options=entry, + title=entry["name"], + ) + config_entry.add_to_hass(hass) + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + + state = hass.states.get("sensor.energy_bill") + assert state + assert state.state == "3" + assert state.attributes.get("last_reset") == last_reset + assert state.attributes.get("last_period") == "0" + + state = hass.states.get("sensor.water_bill") + assert state + assert state.state == "6" + assert state.attributes.get("last_reset") == last_reset + assert state.attributes.get("last_period") == "0" + + now = dt_util.utcnow() + with freeze_time(now): + await hass.services.async_call( + domain=DOMAIN, + service=SERVICE_RESET, + service_data={}, + target={"entity_id": "sensor.energy_bill"}, + blocking=True, + ) + + await hass.async_block_till_done() + + state = hass.states.get("sensor.energy_bill") + assert state + assert state.state == "0" + assert state.attributes.get("last_reset") == now.isoformat() + assert state.attributes.get("last_period") == "3" + + state = hass.states.get("sensor.water_bill") + assert state + assert state.state == "6" + assert state.attributes.get("last_reset") == last_reset + assert state.attributes.get("last_period") == "0" + + @pytest.mark.parametrize( ("yaml_config", "config_entry_config"), [