diff --git a/.strict-typing b/.strict-typing index d7df44c9d64cac..829f890ce6a241 100644 --- a/.strict-typing +++ b/.strict-typing @@ -49,6 +49,7 @@ homeassistant.components.actiontec.* homeassistant.components.adax.* homeassistant.components.adguard.* homeassistant.components.aftership.* +homeassistant.components.ai_task.* homeassistant.components.air_quality.* homeassistant.components.airgradient.* homeassistant.components.airly.* @@ -209,6 +210,7 @@ homeassistant.components.firefly_iii.* homeassistant.components.fitbit.* homeassistant.components.flexit_bacnet.* homeassistant.components.flux_led.* +homeassistant.components.folder_watcher.* homeassistant.components.forecast_solar.* homeassistant.components.fritz.* homeassistant.components.fritzbox.* @@ -298,6 +300,7 @@ homeassistant.components.iotty.* homeassistant.components.ipp.* homeassistant.components.iqvia.* homeassistant.components.iron_os.* +homeassistant.components.isal.* homeassistant.components.islamic_prayer_times.* homeassistant.components.isy994.* homeassistant.components.jellyfin.* @@ -308,6 +311,7 @@ homeassistant.components.knocki.* homeassistant.components.knx.* homeassistant.components.kraken.* homeassistant.components.kulersky.* +homeassistant.components.labs.* homeassistant.components.lacrosse.* homeassistant.components.lacrosse_view.* homeassistant.components.lamarzocco.* @@ -403,6 +407,7 @@ homeassistant.components.opnsense.* homeassistant.components.opower.* homeassistant.components.oralb.* homeassistant.components.otbr.* +homeassistant.components.otp.* homeassistant.components.overkiz.* homeassistant.components.overseerr.* homeassistant.components.p1_monitor.* @@ -438,10 +443,12 @@ homeassistant.components.radarr.* homeassistant.components.radio_browser.* homeassistant.components.rainforest_raven.* homeassistant.components.rainmachine.* +homeassistant.components.random.* homeassistant.components.raspberry_pi.* homeassistant.components.rdw.* homeassistant.components.recollect_waste.* homeassistant.components.recorder.* +homeassistant.components.recovery_mode.* homeassistant.components.redgtech.* homeassistant.components.remember_the_milk.* homeassistant.components.remote.* @@ -473,6 +480,7 @@ homeassistant.components.schlage.* homeassistant.components.scrape.* homeassistant.components.script.* homeassistant.components.search.* +homeassistant.components.season.* homeassistant.components.select.* homeassistant.components.sensibo.* homeassistant.components.sensirion_ble.* @@ -566,6 +574,7 @@ homeassistant.components.update.* homeassistant.components.uptime.* homeassistant.components.uptime_kuma.* homeassistant.components.uptimerobot.* +homeassistant.components.usage_prediction.* homeassistant.components.usb.* homeassistant.components.uvc.* homeassistant.components.vacuum.* @@ -584,6 +593,7 @@ homeassistant.components.water_heater.* homeassistant.components.watts.* homeassistant.components.watttime.* homeassistant.components.weather.* +homeassistant.components.web_rtc.* homeassistant.components.webhook.* homeassistant.components.webostv.* homeassistant.components.websocket_api.* diff --git a/homeassistant/components/aws_s3/__init__.py b/homeassistant/components/aws_s3/__init__.py index b709595ae4adc9..57f2a45f18380a 100644 --- a/homeassistant/components/aws_s3/__init__.py +++ b/homeassistant/components/aws_s3/__init__.py @@ -5,11 +5,10 @@ import logging from typing import cast -from aiobotocore.client import AioBaseClient as S3Client from aiobotocore.session import AioSession from botocore.exceptions import ClientError, ConnectionError, ParamValidationError -from homeassistant.config_entries import ConfigEntry +from homeassistant.const import Platform from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryError, ConfigEntryNotReady @@ -21,9 +20,9 @@ DATA_BACKUP_AGENT_LISTENERS, DOMAIN, ) +from .coordinator import S3ConfigEntry, S3DataUpdateCoordinator -type S3ConfigEntry = ConfigEntry[S3Client] - +_PLATFORMS = (Platform.SENSOR,) _LOGGER = logging.getLogger(__name__) @@ -64,7 +63,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: S3ConfigEntry) -> bool: translation_key="cannot_connect", ) from err - entry.runtime_data = client + coordinator = S3DataUpdateCoordinator( + hass, + entry=entry, + client=client, + ) + await coordinator.async_config_entry_first_refresh() + entry.runtime_data = coordinator def notify_backup_listeners() -> None: for listener in hass.data.get(DATA_BACKUP_AGENT_LISTENERS, []): @@ -72,11 +77,16 @@ def notify_backup_listeners() -> None: entry.async_on_unload(entry.async_on_state_change(notify_backup_listeners)) + await hass.config_entries.async_forward_entry_setups(entry, _PLATFORMS) + return True async def async_unload_entry(hass: HomeAssistant, entry: S3ConfigEntry) -> bool: """Unload a config entry.""" - client = entry.runtime_data - await client.__aexit__(None, None, None) + unload_ok = await hass.config_entries.async_unload_platforms(entry, _PLATFORMS) + if not unload_ok: + return False + coordinator = entry.runtime_data + await coordinator.client.__aexit__(None, None, None) return True diff --git a/homeassistant/components/aws_s3/backup.py b/homeassistant/components/aws_s3/backup.py index 97e2baeec8d126..0d03afa6ac51e6 100644 --- a/homeassistant/components/aws_s3/backup.py +++ b/homeassistant/components/aws_s3/backup.py @@ -20,6 +20,7 @@ from . import S3ConfigEntry from .const import CONF_BUCKET, DATA_BACKUP_AGENT_LISTENERS, DOMAIN +from .helpers import async_list_backups_from_s3 _LOGGER = logging.getLogger(__name__) CACHE_TTL = 300 @@ -93,7 +94,7 @@ class S3BackupAgent(BackupAgent): def __init__(self, hass: HomeAssistant, entry: S3ConfigEntry) -> None: """Initialize the S3 agent.""" super().__init__() - self._client = entry.runtime_data + self._client = entry.runtime_data.client self._bucket: str = entry.data[CONF_BUCKET] self.name = entry.title self.unique_id = entry.entry_id @@ -316,35 +317,8 @@ async def _list_backups(self) -> dict[str, AgentBackup]: if time() <= self._cache_expiration: return self._backup_cache - backups = {} - paginator = self._client.get_paginator("list_objects_v2") - metadata_files: list[dict[str, Any]] = [] - async for page in paginator.paginate(Bucket=self._bucket): - metadata_files.extend( - obj - for obj in page.get("Contents", []) - if obj["Key"].endswith(".metadata.json") - ) - - for metadata_file in metadata_files: - try: - # Download and parse metadata file - metadata_response = await self._client.get_object( - Bucket=self._bucket, Key=metadata_file["Key"] - ) - metadata_content = await metadata_response["Body"].read() - metadata_json = json.loads(metadata_content) - except (BotoCoreError, json.JSONDecodeError) as err: - _LOGGER.warning( - "Failed to process metadata file %s: %s", - metadata_file["Key"], - err, - ) - continue - backup = AgentBackup.from_dict(metadata_json) - backups[backup.backup_id] = backup - - self._backup_cache = backups + backups_list = await async_list_backups_from_s3(self._client, self._bucket) + self._backup_cache = {b.backup_id: b for b in backups_list} self._cache_expiration = time() + CACHE_TTL return self._backup_cache diff --git a/homeassistant/components/aws_s3/coordinator.py b/homeassistant/components/aws_s3/coordinator.py new file mode 100644 index 00000000000000..52735ce364fc8c --- /dev/null +++ b/homeassistant/components/aws_s3/coordinator.py @@ -0,0 +1,70 @@ +"""DataUpdateCoordinator for AWS S3.""" + +from __future__ import annotations + +from dataclasses import dataclass +from datetime import timedelta +import logging + +from aiobotocore.client import AioBaseClient as S3Client +from botocore.exceptions import BotoCoreError + +from homeassistant.config_entries import ConfigEntry +from homeassistant.core import HomeAssistant +from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed + +from .const import CONF_BUCKET, DOMAIN +from .helpers import async_list_backups_from_s3 + +SCAN_INTERVAL = timedelta(hours=6) + +type S3ConfigEntry = ConfigEntry[S3DataUpdateCoordinator] + +_LOGGER = logging.getLogger(__name__) + + +@dataclass +class SensorData: + """Class to represent sensor data.""" + + all_backups_size: int + + +class S3DataUpdateCoordinator(DataUpdateCoordinator[SensorData]): + """Class to manage fetching AWS S3 data from single endpoint.""" + + config_entry: S3ConfigEntry + client: S3Client + + def __init__( + self, + hass: HomeAssistant, + *, + entry: S3ConfigEntry, + client: S3Client, + ) -> None: + """Initialize AWS S3 data updater.""" + super().__init__( + hass, + _LOGGER, + config_entry=entry, + name=DOMAIN, + update_interval=SCAN_INTERVAL, + ) + self.client = client + self._bucket: str = entry.data[CONF_BUCKET] + + async def _async_update_data(self) -> SensorData: + """Fetch data from AWS S3.""" + try: + backups = await async_list_backups_from_s3(self.client, self._bucket) + except BotoCoreError as error: + raise UpdateFailed( + translation_domain=DOMAIN, + translation_key="error_fetching_data", + ) from error + + all_backups_size = sum(b.size for b in backups) + return SensorData( + all_backups_size=all_backups_size, + ) diff --git a/homeassistant/components/aws_s3/entity.py b/homeassistant/components/aws_s3/entity.py new file mode 100644 index 00000000000000..24f12934ae36db --- /dev/null +++ b/homeassistant/components/aws_s3/entity.py @@ -0,0 +1,33 @@ +"""Define the AWS S3 entity.""" + +from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo +from homeassistant.helpers.entity import EntityDescription +from homeassistant.helpers.update_coordinator import CoordinatorEntity + +from .const import CONF_BUCKET, DOMAIN +from .coordinator import S3DataUpdateCoordinator + + +class S3Entity(CoordinatorEntity[S3DataUpdateCoordinator]): + """Defines a base AWS S3 entity.""" + + _attr_has_entity_name = True + + def __init__( + self, coordinator: S3DataUpdateCoordinator, description: EntityDescription + ) -> None: + """Initialize an AWS S3 entity.""" + super().__init__(coordinator) + self.entity_description = description + self._attr_unique_id = f"{coordinator.config_entry.entry_id}_{description.key}" + + @property + def device_info(self) -> DeviceInfo: + """Return device information about this AWS S3 device.""" + return DeviceInfo( + identifiers={(DOMAIN, self.coordinator.config_entry.entry_id)}, + name=f"Bucket {self.coordinator.config_entry.data[CONF_BUCKET]}", + manufacturer="AWS", + model="AWS S3", + entry_type=DeviceEntryType.SERVICE, + ) diff --git a/homeassistant/components/aws_s3/helpers.py b/homeassistant/components/aws_s3/helpers.py new file mode 100644 index 00000000000000..0eea233c797c6b --- /dev/null +++ b/homeassistant/components/aws_s3/helpers.py @@ -0,0 +1,57 @@ +"""Helpers for the AWS S3 integration.""" + +from __future__ import annotations + +import json +import logging +from typing import Any + +from aiobotocore.client import AioBaseClient as S3Client +from botocore.exceptions import BotoCoreError + +from homeassistant.components.backup import AgentBackup + +_LOGGER = logging.getLogger(__name__) + + +async def async_list_backups_from_s3( + client: S3Client, + bucket: str, +) -> list[AgentBackup]: + """List backups from an S3 bucket by reading metadata files.""" + paginator = client.get_paginator("list_objects_v2") + metadata_files: list[dict[str, Any]] = [] + async for page in paginator.paginate(Bucket=bucket): + metadata_files.extend( + obj + for obj in page.get("Contents", []) + if obj["Key"].endswith(".metadata.json") + ) + + backups: list[AgentBackup] = [] + for metadata_file in metadata_files: + try: + metadata_response = await client.get_object( + Bucket=bucket, Key=metadata_file["Key"] + ) + metadata_content = await metadata_response["Body"].read() + metadata_json = json.loads(metadata_content) + except (BotoCoreError, json.JSONDecodeError) as err: + _LOGGER.warning( + "Failed to process metadata file %s: %s", + metadata_file["Key"], + err, + ) + continue + try: + backup = AgentBackup.from_dict(metadata_json) + except (KeyError, TypeError, ValueError) as err: + _LOGGER.warning( + "Failed to parse metadata in file %s: %s", + metadata_file["Key"], + err, + ) + continue + backups.append(backup) + + return backups diff --git a/homeassistant/components/aws_s3/manifest.json b/homeassistant/components/aws_s3/manifest.json index 8ab65b5883a14b..b54c0d29423b09 100644 --- a/homeassistant/components/aws_s3/manifest.json +++ b/homeassistant/components/aws_s3/manifest.json @@ -3,9 +3,10 @@ "name": "AWS S3", "codeowners": ["@tomasbedrich"], "config_flow": true, + "dependencies": ["backup"], "documentation": "https://www.home-assistant.io/integrations/aws_s3", "integration_type": "service", - "iot_class": "cloud_push", + "iot_class": "cloud_polling", "loggers": ["aiobotocore"], "quality_scale": "bronze", "requirements": ["aiobotocore==2.21.1"] diff --git a/homeassistant/components/aws_s3/quality_scale.yaml b/homeassistant/components/aws_s3/quality_scale.yaml index 11093f4430f45d..963bf7a05f7fdc 100644 --- a/homeassistant/components/aws_s3/quality_scale.yaml +++ b/homeassistant/components/aws_s3/quality_scale.yaml @@ -3,9 +3,7 @@ rules: action-setup: status: exempt comment: Integration does not register custom actions. - appropriate-polling: - status: exempt - comment: This integration does not poll. + appropriate-polling: done brands: done common-modules: done config-flow-test-coverage: done @@ -20,12 +18,8 @@ rules: entity-event-setup: status: exempt comment: Entities of this integration does not explicitly subscribe to events. - entity-unique-id: - status: exempt - comment: This integration does not have entities. - has-entity-name: - status: exempt - comment: This integration does not have entities. + entity-unique-id: done + has-entity-name: done runtime-data: done test-before-configure: done test-before-setup: done @@ -40,21 +34,15 @@ rules: status: exempt comment: This integration does not have an options flow. docs-installation-parameters: done - entity-unavailable: - status: exempt - comment: This integration does not have entities. + entity-unavailable: done integration-owner: done log-when-unavailable: todo - parallel-updates: - status: exempt - comment: This integration does not poll. + parallel-updates: done reauthentication-flow: todo test-coverage: done # Gold - devices: - status: exempt - comment: This integration does not have entities. + devices: done diagnostics: todo discovery-update-info: status: exempt @@ -62,15 +50,11 @@ rules: discovery: status: exempt comment: S3 is a cloud service that is not discovered on the network. - docs-data-update: - status: exempt - comment: This integration does not poll. + docs-data-update: done docs-examples: status: exempt comment: The integration extends core functionality and does not require examples. - docs-known-limitations: - status: exempt - comment: No known limitations. + docs-known-limitations: done docs-supported-devices: status: exempt comment: This integration does not support physical devices. @@ -81,19 +65,11 @@ rules: docs-use-cases: done dynamic-devices: status: exempt - comment: This integration does not have devices. - entity-category: - status: exempt - comment: This integration does not have entities. - entity-device-class: - status: exempt - comment: This integration does not have entities. - entity-disabled-by-default: - status: exempt - comment: This integration does not have entities. - entity-translations: - status: exempt - comment: This integration does not have entities. + comment: This integration has a fixed set of devices. + entity-category: done + entity-device-class: done + entity-disabled-by-default: done + entity-translations: done exception-translations: done icon-translations: status: exempt @@ -104,7 +80,7 @@ rules: comment: There are no issues which can be repaired. stale-devices: status: exempt - comment: This integration does not have devices. + comment: This is a service type integration with a single device. # Platinum async-dependency: done diff --git a/homeassistant/components/aws_s3/sensor.py b/homeassistant/components/aws_s3/sensor.py new file mode 100644 index 00000000000000..95e742cb2d95ef --- /dev/null +++ b/homeassistant/components/aws_s3/sensor.py @@ -0,0 +1,66 @@ +"""Support for AWS S3 sensors.""" + +from __future__ import annotations + +from collections.abc import Callable +from dataclasses import dataclass + +from homeassistant.components.sensor import ( + SensorDeviceClass, + SensorEntity, + SensorEntityDescription, +) +from homeassistant.const import EntityCategory, UnitOfInformation +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback +from homeassistant.helpers.typing import StateType + +from .coordinator import S3ConfigEntry, SensorData +from .entity import S3Entity + +# Coordinator is used to centralize the data updates +PARALLEL_UPDATES = 0 + + +@dataclass(frozen=True, kw_only=True) +class S3SensorEntityDescription(SensorEntityDescription): + """Describes an AWS S3 sensor entity.""" + + value_fn: Callable[[SensorData], StateType] + + +SENSORS: tuple[S3SensorEntityDescription, ...] = ( + S3SensorEntityDescription( + key="backups_size", + translation_key="backups_size", + native_unit_of_measurement=UnitOfInformation.BYTES, + suggested_unit_of_measurement=UnitOfInformation.MEBIBYTES, + suggested_display_precision=0, + device_class=SensorDeviceClass.DATA_SIZE, + entity_category=EntityCategory.DIAGNOSTIC, + value_fn=lambda data: data.all_backups_size, + ), +) + + +async def async_setup_entry( + hass: HomeAssistant, + entry: S3ConfigEntry, + async_add_entities: AddConfigEntryEntitiesCallback, +) -> None: + """Set up AWS S3 sensor based on a config entry.""" + coordinator = entry.runtime_data + async_add_entities( + S3SensorEntity(coordinator, description) for description in SENSORS + ) + + +class S3SensorEntity(S3Entity, SensorEntity): + """Defines an AWS S3 sensor entity.""" + + entity_description: S3SensorEntityDescription + + @property + def native_value(self) -> StateType: + """Return the state of the sensor.""" + return self.entity_description.value_fn(self.coordinator.data) diff --git a/homeassistant/components/aws_s3/strings.json b/homeassistant/components/aws_s3/strings.json index 8eb935355c2db7..13d8cc2203b5c8 100644 --- a/homeassistant/components/aws_s3/strings.json +++ b/homeassistant/components/aws_s3/strings.json @@ -27,10 +27,20 @@ } } }, + "entity": { + "sensor": { + "backups_size": { + "name": "Total size of backups" + } + } + }, "exceptions": { "cannot_connect": { "message": "Cannot connect to endpoint" }, + "error_fetching_data": { + "message": "Error fetching data" + }, "invalid_bucket_name": { "message": "Invalid bucket name" }, diff --git a/homeassistant/components/duckdns/issue.py b/homeassistant/components/duckdns/issue.py index f2124f97fa051d..34a23fdbc639b0 100644 --- a/homeassistant/components/duckdns/issue.py +++ b/homeassistant/components/duckdns/issue.py @@ -38,3 +38,18 @@ def deprecate_yaml_issue(hass: HomeAssistant, *, import_success: bool) -> None: "url": "/config/integrations/dashboard/add?domain=duckdns" }, ) + + +def action_called_without_config_entry(hass: HomeAssistant) -> None: + """Deprecate the use of action without config entry.""" + + async_create_issue( + hass, + DOMAIN, + "deprecated_call_without_config_entry", + breaks_in_ha_version="2026.9.0", + is_fixable=False, + issue_domain=DOMAIN, + severity=IssueSeverity.WARNING, + translation_key="deprecated_call_without_config_entry", + ) diff --git a/homeassistant/components/duckdns/services.py b/homeassistant/components/duckdns/services.py index ff2368a146729b..b6a0e5174bf631 100644 --- a/homeassistant/components/duckdns/services.py +++ b/homeassistant/components/duckdns/services.py @@ -15,6 +15,7 @@ from .const import ATTR_CONFIG_ENTRY, ATTR_TXT, DOMAIN, SERVICE_SET_TXT from .coordinator import DuckDnsConfigEntry from .helpers import update_duckdns +from .issue import action_called_without_config_entry SERVICE_TXT_SCHEMA = vol.Schema( { @@ -42,6 +43,7 @@ def get_config_entry( """Return config entry or raise if not found or not loaded.""" if entry_id is None: + action_called_without_config_entry(hass) if len(entries := hass.config_entries.async_entries(DOMAIN)) != 1: raise ServiceValidationError( translation_domain=DOMAIN, diff --git a/homeassistant/components/duckdns/strings.json b/homeassistant/components/duckdns/strings.json index fdd3db2ad36f0a..87262c913e32c4 100644 --- a/homeassistant/components/duckdns/strings.json +++ b/homeassistant/components/duckdns/strings.json @@ -46,6 +46,10 @@ } }, "issues": { + "deprecated_call_without_config_entry": { + "description": "Calling the `duckdns.set_txt` action without specifying a config entry is deprecated.\n\nThe `config_entry_id` field will be required in a future release.\n\nPlease update your automations and scripts to include the `config_entry_id` parameter.", + "title": "Detected deprecated use of action without config entry" + }, "deprecated_yaml_import_issue_error": { "description": "Configuring Duck DNS using YAML is being removed but there was an error when trying to import the YAML configuration.\n\nEnsure the YAML configuration is correct and restart Home Assistant to try again or remove the Duck DNS YAML configuration from your `configuration.yaml` file and continue to [set up the integration]({url}) manually.", "title": "The Duck DNS YAML configuration import failed" diff --git a/homeassistant/components/growatt_server/coordinator.py b/homeassistant/components/growatt_server/coordinator.py index 68297e9c1c72a5..8a939a89439c35 100644 --- a/homeassistant/components/growatt_server/coordinator.py +++ b/homeassistant/components/growatt_server/coordinator.py @@ -9,6 +9,7 @@ import growattServer +from homeassistant.components.sensor import SensorStateClass from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_PASSWORD, CONF_URL, CONF_USERNAME from homeassistant.core import HomeAssistant @@ -54,6 +55,7 @@ def __init__( self.device_type = device_type self.plant_id = plant_id self.previous_values: dict[str, Any] = {} + self._pre_reset_values: dict[str, float] = {} if self.api_version == "v1": self.username = None @@ -251,6 +253,40 @@ def get_data( ) return_value = previous_value + # Suppress midnight bounce for TOTAL_INCREASING "today" sensors. + # The Growatt API sometimes delivers stale yesterday values after a midnight + # reset (0 → stale → 0), causing TOTAL_INCREASING double-counting. + if ( + entity_description.state_class is SensorStateClass.TOTAL_INCREASING + and not entity_description.never_resets + and return_value is not None + and previous_value is not None + ): + current_val = float(return_value) + prev_val = float(previous_value) + if prev_val > 0 and current_val == 0: + # Value dropped to 0 from a positive level — track it. + self._pre_reset_values[variable] = prev_val + elif variable in self._pre_reset_values: + pre_reset = self._pre_reset_values[variable] + if current_val == pre_reset: + # Value equals yesterday's final value — the API is + # serving a stale cached response (bounce) + _LOGGER.debug( + "Suppressing midnight bounce for %s: stale value %s matches " + "pre-reset value, keeping %s", + variable, + current_val, + previous_value, + ) + return_value = previous_value + elif current_val > 0: + # Genuine new-day production — clear tracking + del self._pre_reset_values[variable] + + # Note: previous_values stores the *output* value (after suppression), + # not the raw API value. This is intentional — after a suppressed bounce, + # previous_value will be 0, which is what downstream comparisons need. self.previous_values[variable] = return_value return return_value diff --git a/homeassistant/components/history_stats/__init__.py b/homeassistant/components/history_stats/__init__.py index ab416a5a50cc55..5b5fccfbb989b5 100644 --- a/homeassistant/components/history_stats/__init__.py +++ b/homeassistant/components/history_stats/__init__.py @@ -5,6 +5,7 @@ from datetime import timedelta import logging +from homeassistant.components.sensor import CONF_STATE_CLASS, SensorStateClass from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_ENTITY_ID, CONF_STATE from homeassistant.core import HomeAssistant @@ -105,6 +106,12 @@ async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> hass.config_entries.async_update_entry( config_entry, options=options, minor_version=2 ) + if config_entry.minor_version < 3: + # Set the state class to measurement for backward compatibility + options[CONF_STATE_CLASS] = SensorStateClass.MEASUREMENT + hass.config_entries.async_update_entry( + config_entry, options=options, minor_version=3 + ) _LOGGER.debug( "Migration to version %s.%s successful", diff --git a/homeassistant/components/history_stats/config_flow.py b/homeassistant/components/history_stats/config_flow.py index 9ffdee6830bfdd..593092728b01cf 100644 --- a/homeassistant/components/history_stats/config_flow.py +++ b/homeassistant/components/history_stats/config_flow.py @@ -9,6 +9,7 @@ import voluptuous as vol from homeassistant.components import websocket_api +from homeassistant.components.sensor import CONF_STATE_CLASS, SensorStateClass from homeassistant.const import CONF_ENTITY_ID, CONF_NAME, CONF_STATE, CONF_TYPE from homeassistant.core import HomeAssistant, callback from homeassistant.exceptions import HomeAssistantError @@ -39,6 +40,7 @@ CONF_PERIOD_KEYS, CONF_START, CONF_TYPE_KEYS, + CONF_TYPE_RATIO, CONF_TYPE_TIME, DEFAULT_NAME, DOMAIN, @@ -101,10 +103,19 @@ async def get_state_schema(handler: SchemaCommonFlowHandler) -> vol.Schema: async def get_options_schema(handler: SchemaCommonFlowHandler) -> vol.Schema: """Return schema for options step.""" entity_id = handler.options[CONF_ENTITY_ID] - return _get_options_schema_with_entity_id(entity_id) - - -def _get_options_schema_with_entity_id(entity_id: str) -> vol.Schema: + conf_type = handler.options[CONF_TYPE] + return _get_options_schema_with_entity_id(entity_id, conf_type) + + +def _get_options_schema_with_entity_id(entity_id: str, type: str) -> vol.Schema: + state_class_options = ( + [SensorStateClass.MEASUREMENT] + if type == CONF_TYPE_RATIO + else [ + SensorStateClass.MEASUREMENT, + SensorStateClass.TOTAL_INCREASING, + ] + ) return vol.Schema( { vol.Optional(CONF_ENTITY_ID): EntitySelector( @@ -130,6 +141,13 @@ def _get_options_schema_with_entity_id(entity_id: str) -> vol.Schema: vol.Optional(CONF_DURATION): DurationSelector( DurationSelectorConfig(enable_day=True, allow_negative=False) ), + vol.Optional(CONF_STATE_CLASS): SelectSelector( + SelectSelectorConfig( + options=state_class_options, + translation_key=CONF_STATE_CLASS, + mode=SelectSelectorMode.DROPDOWN, + ), + ), } ) @@ -158,7 +176,7 @@ def _get_options_schema_with_entity_id(entity_id: str) -> vol.Schema: class HistoryStatsConfigFlowHandler(SchemaConfigFlowHandler, domain=DOMAIN): """Handle a config flow for History stats.""" - MINOR_VERSION = 2 + MINOR_VERSION = 3 config_flow = CONFIG_FLOW options_flow = OPTIONS_FLOW @@ -201,6 +219,7 @@ async def ws_start_preview( config_entry = hass.config_entries.async_get_entry(flow_status["handler"]) entity_id = options[CONF_ENTITY_ID] name = options[CONF_NAME] + conf_type = options[CONF_TYPE] else: flow_status = hass.config_entries.options.async_get(msg["flow_id"]) config_entry = hass.config_entries.async_get_entry(flow_status["handler"]) @@ -208,6 +227,7 @@ async def ws_start_preview( raise HomeAssistantError("Config entry not found") entity_id = config_entry.options[CONF_ENTITY_ID] name = config_entry.options[CONF_NAME] + conf_type = config_entry.options[CONF_TYPE] @callback def async_preview_updated( @@ -233,7 +253,7 @@ def async_preview_updated( validated_data: Any = None try: - validated_data = (_get_options_schema_with_entity_id(entity_id))( + validated_data = (_get_options_schema_with_entity_id(entity_id, conf_type))( msg["user_input"] ) except vol.Invalid as ex: @@ -255,6 +275,7 @@ def async_preview_updated( start = validated_data.get(CONF_START) end = validated_data.get(CONF_END) duration = validated_data.get(CONF_DURATION) + state_class = validated_data.get(CONF_STATE_CLASS) history_stats = HistoryStats( hass, @@ -274,6 +295,7 @@ def async_preview_updated( name=name, unique_id=None, source_entity_id=entity_id, + state_class=state_class, ) preview_entity.hass = hass diff --git a/homeassistant/components/history_stats/sensor.py b/homeassistant/components/history_stats/sensor.py index 1bd5d491e0c046..98616b3e3759ce 100644 --- a/homeassistant/components/history_stats/sensor.py +++ b/homeassistant/components/history_stats/sensor.py @@ -10,6 +10,7 @@ import voluptuous as vol from homeassistant.components.sensor import ( + CONF_STATE_CLASS, PLATFORM_SCHEMA as SENSOR_PLATFORM_SCHEMA, SensorDeviceClass, SensorEntity, @@ -72,6 +73,16 @@ def exactly_two_period_keys[_T: dict[str, Any]](conf: _T) -> _T: return conf +def no_ratio_total[_T: dict[str, Any]](conf: _T) -> _T: + """Ensure state_class:total_increasing not used with type:ratio.""" + if ( + conf.get(CONF_TYPE) == CONF_TYPE_RATIO + and conf.get(CONF_STATE_CLASS) == SensorStateClass.TOTAL_INCREASING + ): + raise vol.Invalid("State class total_increasing not to be used with type ratio") + return conf + + PLATFORM_SCHEMA = vol.All( SENSOR_PLATFORM_SCHEMA.extend( { @@ -83,9 +94,15 @@ def exactly_two_period_keys[_T: dict[str, Any]](conf: _T) -> _T: vol.Optional(CONF_TYPE, default=CONF_TYPE_TIME): vol.In(CONF_TYPE_KEYS), vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, vol.Optional(CONF_UNIQUE_ID): cv.string, + vol.Optional( + CONF_STATE_CLASS, default=SensorStateClass.MEASUREMENT + ): vol.In( + [None, SensorStateClass.MEASUREMENT, SensorStateClass.TOTAL_INCREASING] + ), } ), exactly_two_period_keys, + no_ratio_total, ) @@ -106,6 +123,9 @@ async def async_setup_platform( sensor_type: str = config[CONF_TYPE] name: str = config[CONF_NAME] unique_id: str | None = config.get(CONF_UNIQUE_ID) + state_class: SensorStateClass | None = config.get( + CONF_STATE_CLASS, SensorStateClass.MEASUREMENT + ) history_stats = HistoryStats(hass, entity_id, entity_states, start, end, duration) coordinator = HistoryStatsUpdateCoordinator(hass, history_stats, None, name) @@ -121,6 +141,7 @@ async def async_setup_platform( name=name, unique_id=unique_id, source_entity_id=entity_id, + state_class=state_class, ) ] ) @@ -136,6 +157,7 @@ async def async_setup_entry( sensor_type: str = entry.options[CONF_TYPE] coordinator = entry.runtime_data entity_id: str = entry.options[CONF_ENTITY_ID] + state_class: SensorStateClass | None = entry.options.get(CONF_STATE_CLASS) async_add_entities( [ HistoryStatsSensor( @@ -145,6 +167,7 @@ async def async_setup_entry( name=entry.title, unique_id=entry.entry_id, source_entity_id=entity_id, + state_class=state_class, ) ] ) @@ -185,8 +208,6 @@ def _process_update(self) -> None: class HistoryStatsSensor(HistoryStatsSensorBase): """A HistoryStats sensor.""" - _attr_state_class = SensorStateClass.MEASUREMENT - def __init__( self, hass: HomeAssistant, @@ -196,6 +217,7 @@ def __init__( name: str, unique_id: str | None, source_entity_id: str, + state_class: SensorStateClass | None, ) -> None: """Initialize the HistoryStats sensor.""" super().__init__(coordinator, name) @@ -204,6 +226,7 @@ def __init__( ) = None self._attr_native_unit_of_measurement = UNITS[sensor_type] self._type = sensor_type + self._attr_state_class = state_class self._attr_unique_id = unique_id if source_entity_id: # Guard against empty source_entity_id in preview mode self.device_entry = async_entity_id_to_device( diff --git a/homeassistant/components/history_stats/strings.json b/homeassistant/components/history_stats/strings.json index d08e1ec4329ec5..304ca6e8eb5369 100644 --- a/homeassistant/components/history_stats/strings.json +++ b/homeassistant/components/history_stats/strings.json @@ -14,6 +14,7 @@ "entity_id": "[%key:component::history_stats::config::step::user::data::entity_id%]", "start": "Start", "state": "[%key:component::history_stats::config::step::user::data::state%]", + "state_class": "[%key:component::sensor::entity_component::_::state_attributes::state_class::name%]", "type": "[%key:component::history_stats::config::step::user::data::type%]" }, "data_description": { @@ -22,6 +23,7 @@ "entity_id": "[%key:component::history_stats::config::step::user::data_description::entity_id%]", "start": "When to start the measure (timestamp or datetime). Can be a template.", "state": "[%key:component::history_stats::config::step::user::data_description::state%]", + "state_class": "The state class for statistics calculation.", "type": "[%key:component::history_stats::config::step::user::data_description::type%]" }, "description": "Read the documentation for further details on how to configure the history stats sensor using these options." @@ -68,6 +70,7 @@ "entity_id": "[%key:component::history_stats::config::step::user::data::entity_id%]", "start": "[%key:component::history_stats::config::step::options::data::start%]", "state": "[%key:component::history_stats::config::step::user::data::state%]", + "state_class": "[%key:component::sensor::entity_component::_::state_attributes::state_class::name%]", "type": "[%key:component::history_stats::config::step::user::data::type%]" }, "data_description": { @@ -76,6 +79,7 @@ "entity_id": "[%key:component::history_stats::config::step::user::data_description::entity_id%]", "start": "[%key:component::history_stats::config::step::options::data_description::start%]", "state": "[%key:component::history_stats::config::step::user::data_description::state%]", + "state_class": "The state class for statistics calculation. Changing the state class will require statistics to be reset.", "type": "[%key:component::history_stats::config::step::user::data_description::type%]" }, "description": "[%key:component::history_stats::config::step::options::description%]" @@ -83,6 +87,12 @@ } }, "selector": { + "state_class": { + "options": { + "measurement": "[%key:component::sensor::entity_component::_::state_attributes::state_class::state::measurement%]", + "total_increasing": "[%key:component::sensor::entity_component::_::state_attributes::state_class::state::total_increasing%]" + } + }, "type": { "options": { "count": "Count", diff --git a/homeassistant/components/mta/manifest.json b/homeassistant/components/mta/manifest.json index b1d82533df6f52..a9a5eedfbcfe3b 100644 --- a/homeassistant/components/mta/manifest.json +++ b/homeassistant/components/mta/manifest.json @@ -8,5 +8,5 @@ "iot_class": "cloud_polling", "loggers": ["pymta"], "quality_scale": "silver", - "requirements": ["py-nymta==0.3.4"] + "requirements": ["py-nymta==0.4.0"] } diff --git a/homeassistant/components/sensorpush_cloud/manifest.json b/homeassistant/components/sensorpush_cloud/manifest.json index 3de5c4b5c86f5d..e0b4b7d8ee8499 100644 --- a/homeassistant/components/sensorpush_cloud/manifest.json +++ b/homeassistant/components/sensorpush_cloud/manifest.json @@ -4,6 +4,7 @@ "codeowners": ["@sstallion"], "config_flow": true, "documentation": "https://www.home-assistant.io/integrations/sensorpush_cloud", + "integration_type": "hub", "iot_class": "cloud_polling", "loggers": ["sensorpush_api", "sensorpush_ha"], "quality_scale": "bronze", diff --git a/homeassistant/components/sharkiq/manifest.json b/homeassistant/components/sharkiq/manifest.json index 4b669ae7b7fff6..02bb3419000bf9 100644 --- a/homeassistant/components/sharkiq/manifest.json +++ b/homeassistant/components/sharkiq/manifest.json @@ -4,6 +4,7 @@ "codeowners": ["@JeffResc", "@funkybunch", "@TheOneOgre"], "config_flow": true, "documentation": "https://www.home-assistant.io/integrations/sharkiq", + "integration_type": "hub", "iot_class": "cloud_polling", "loggers": ["sharkiq"], "requirements": ["sharkiq==1.5.0"] diff --git a/homeassistant/components/sleepiq/manifest.json b/homeassistant/components/sleepiq/manifest.json index c29929bf62bea9..39a889997f8f92 100644 --- a/homeassistant/components/sleepiq/manifest.json +++ b/homeassistant/components/sleepiq/manifest.json @@ -9,6 +9,7 @@ } ], "documentation": "https://www.home-assistant.io/integrations/sleepiq", + "integration_type": "hub", "iot_class": "cloud_polling", "loggers": ["asyncsleepiq"], "requirements": ["asyncsleepiq==1.7.0"] diff --git a/homeassistant/components/slimproto/manifest.json b/homeassistant/components/slimproto/manifest.json index f270e020740118..4ce170bf078e6c 100644 --- a/homeassistant/components/slimproto/manifest.json +++ b/homeassistant/components/slimproto/manifest.json @@ -5,6 +5,7 @@ "codeowners": ["@marcelveldt"], "config_flow": true, "documentation": "https://www.home-assistant.io/integrations/slimproto", + "integration_type": "device", "iot_class": "local_push", "requirements": ["aioslimproto==3.0.0"] } diff --git a/homeassistant/components/smappee/manifest.json b/homeassistant/components/smappee/manifest.json index 0f407d67816606..11255392f908a0 100644 --- a/homeassistant/components/smappee/manifest.json +++ b/homeassistant/components/smappee/manifest.json @@ -5,6 +5,7 @@ "config_flow": true, "dependencies": ["auth"], "documentation": "https://www.home-assistant.io/integrations/smappee", + "integration_type": "hub", "iot_class": "cloud_polling", "loggers": ["paho_mqtt", "pysmappee"], "requirements": ["pysmappee==0.2.29"], diff --git a/homeassistant/components/tuya/binary_sensor.py b/homeassistant/components/tuya/binary_sensor.py index 642553b128c2e7..430e9f71b72637 100644 --- a/homeassistant/components/tuya/binary_sensor.py +++ b/homeassistant/components/tuya/binary_sensor.py @@ -473,14 +473,16 @@ def is_on(self) -> bool | None: """Return true if sensor is on.""" return self._read_wrapper(self._dpcode_wrapper) - async def _handle_state_update( + async def _process_device_update( self, - updated_status_properties: list[str] | None, + updated_status_properties: list[str], dp_timestamps: dict[str, int] | None, - ) -> None: - """Handle state update, only if this entity's dpcode was actually updated.""" - if self._dpcode_wrapper.skip_update( + ) -> bool: + """Called when Tuya device sends an update with updated properties. + + Returns True if the Home Assistant state should be written, + or False if the state write should be skipped. + """ + return not self._dpcode_wrapper.skip_update( self.device, updated_status_properties, dp_timestamps - ): - return - self.async_write_ha_state() + ) diff --git a/homeassistant/components/tuya/entity.py b/homeassistant/components/tuya/entity.py index c6cc76c22cf4e1..393eb71afe54a5 100644 --- a/homeassistant/components/tuya/entity.py +++ b/homeassistant/components/tuya/entity.py @@ -59,7 +59,33 @@ async def _handle_state_update( updated_status_properties: list[str] | None, dp_timestamps: dict[str, int] | None, ) -> None: - self.async_write_ha_state() + """Called when Tuya device sends an update.""" + if ( + # If updated_status_properties is None, we should not skip, + # as we don't have information on what was updated + # This happens for example on online/offline updates, where + # we still want to update the entity state but we have nothing + # to process + updated_status_properties is None + # If we have data to process, we check if we should skip the + # state_write based on the dpcode wrapper logic + or await self._process_device_update( + updated_status_properties, dp_timestamps + ) + ): + self.async_write_ha_state() + + async def _process_device_update( + self, + updated_status_properties: list[str], + dp_timestamps: dict[str, int] | None, + ) -> bool: + """Called when Tuya device sends an update with updated properties. + + Returns True if the Home Assistant state should be written, + or False if the state write should be skipped. + """ + return True async def _async_send_commands(self, commands: list[dict[str, Any]]) -> None: """Send a list of commands to the device.""" diff --git a/homeassistant/components/tuya/event.py b/homeassistant/components/tuya/event.py index 4ac2c269fa347c..583940f28dbc93 100644 --- a/homeassistant/components/tuya/event.py +++ b/homeassistant/components/tuya/event.py @@ -215,16 +215,21 @@ def __init__( self._dpcode_wrapper = dpcode_wrapper self._attr_event_types = dpcode_wrapper.options - async def _handle_state_update( + async def _process_device_update( self, - updated_status_properties: list[str] | None, + updated_status_properties: list[str], dp_timestamps: dict[str, int] | None, - ) -> None: + ) -> bool: + """Called when Tuya device sends an update with updated properties. + + Returns True if the Home Assistant state should be written, + or False if the state write should be skipped. + """ if self._dpcode_wrapper.skip_update( self.device, updated_status_properties, dp_timestamps ) or not (event_data := self._dpcode_wrapper.read_device_status(self.device)): - return + return False event_type, event_attributes = event_data self._trigger_event(event_type, event_attributes) - self.async_write_ha_state() + return True diff --git a/homeassistant/components/tuya/models.py b/homeassistant/components/tuya/models.py index f5937b32a294e2..07cb251e9e1fa3 100644 --- a/homeassistant/components/tuya/models.py +++ b/homeassistant/components/tuya/models.py @@ -46,7 +46,7 @@ def initialize(self, device: CustomerDevice) -> None: def skip_update( self, device: CustomerDevice, - updated_status_properties: list[str] | None, + updated_status_properties: list[str], dp_timestamps: dict[str, int] | None, ) -> bool: """Determine if the wrapper should skip an update. @@ -85,7 +85,7 @@ def __init__(self, dpcode: str) -> None: def skip_update( self, device: CustomerDevice, - updated_status_properties: list[str] | None, + updated_status_properties: list[str], dp_timestamps: dict[str, int] | None, ) -> bool: """Determine if the wrapper should skip an update. @@ -252,20 +252,13 @@ def __init__(self, dpcode: str, type_information: IntegerTypeInformation) -> Non def skip_update( self, device: CustomerDevice, - updated_status_properties: list[str] | None, + updated_status_properties: list[str], dp_timestamps: dict[str, int] | None, ) -> bool: """Override skip_update to process delta updates. Processes delta accumulation before determining if update should be skipped. """ - # If updated_status_properties is None, we should not skip, - # as we don't have information on what was updated - # This happens for example on online/offline updates, where - # we still want to update the entity state but we have nothing - # to accumulate, so we return False to not skip the update - if updated_status_properties is None: - return False if ( super().skip_update(device, updated_status_properties, dp_timestamps) or dp_timestamps is None diff --git a/homeassistant/components/tuya/number.py b/homeassistant/components/tuya/number.py index a8534f4c489b4f..faa76d1a392451 100644 --- a/homeassistant/components/tuya/number.py +++ b/homeassistant/components/tuya/number.py @@ -551,17 +551,19 @@ def native_value(self) -> float | None: """Return the entity value to represent the entity state.""" return self._read_wrapper(self._dpcode_wrapper) - async def _handle_state_update( + async def _process_device_update( self, - updated_status_properties: list[str] | None, + updated_status_properties: list[str], dp_timestamps: dict[str, int] | None, - ) -> None: - """Handle state update, only if this entity's dpcode was actually updated.""" - if self._dpcode_wrapper.skip_update( + ) -> bool: + """Called when Tuya device sends an update with updated properties. + + Returns True if the Home Assistant state should be written, + or False if the state write should be skipped. + """ + return not self._dpcode_wrapper.skip_update( self.device, updated_status_properties, dp_timestamps - ): - return - self.async_write_ha_state() + ) async def async_set_native_value(self, value: float) -> None: """Set new value.""" diff --git a/homeassistant/components/tuya/select.py b/homeassistant/components/tuya/select.py index 8e884d47cf7ea4..f5078b4012045d 100644 --- a/homeassistant/components/tuya/select.py +++ b/homeassistant/components/tuya/select.py @@ -407,17 +407,19 @@ def current_option(self) -> str | None: """Return the selected entity option to represent the entity state.""" return self._read_wrapper(self._dpcode_wrapper) - async def _handle_state_update( + async def _process_device_update( self, - updated_status_properties: list[str] | None, + updated_status_properties: list[str], dp_timestamps: dict[str, int] | None, - ) -> None: - """Handle state update, only if this entity's dpcode was actually updated.""" - if self._dpcode_wrapper.skip_update( + ) -> bool: + """Called when Tuya device sends an update with updated properties. + + Returns True if the Home Assistant state should be written, + or False if the state write should be skipped. + """ + return not self._dpcode_wrapper.skip_update( self.device, updated_status_properties, dp_timestamps - ): - return - self.async_write_ha_state() + ) async def async_select_option(self, option: str) -> None: """Change the selected option.""" diff --git a/homeassistant/components/tuya/sensor.py b/homeassistant/components/tuya/sensor.py index 8f91f7a4f88464..90789c33aef060 100644 --- a/homeassistant/components/tuya/sensor.py +++ b/homeassistant/components/tuya/sensor.py @@ -1856,14 +1856,16 @@ def native_value(self) -> StateType: """Return the value reported by the sensor.""" return self._read_wrapper(self._dpcode_wrapper) - async def _handle_state_update( + async def _process_device_update( self, - updated_status_properties: list[str] | None, + updated_status_properties: list[str], dp_timestamps: dict[str, int] | None, - ) -> None: - """Handle state update, only if this entity's dpcode was actually updated.""" - if self._dpcode_wrapper.skip_update( + ) -> bool: + """Called when Tuya device sends an update with updated properties. + + Returns True if the Home Assistant state should be written, + or False if the state write should be skipped. + """ + return not self._dpcode_wrapper.skip_update( self.device, updated_status_properties, dp_timestamps - ): - return - self.async_write_ha_state() + ) diff --git a/homeassistant/components/tuya/siren.py b/homeassistant/components/tuya/siren.py index 4bd803b19a04b5..7031923673359c 100644 --- a/homeassistant/components/tuya/siren.py +++ b/homeassistant/components/tuya/siren.py @@ -107,17 +107,19 @@ def is_on(self) -> bool | None: """Return true if siren is on.""" return self._read_wrapper(self._dpcode_wrapper) - async def _handle_state_update( + async def _process_device_update( self, - updated_status_properties: list[str] | None, + updated_status_properties: list[str], dp_timestamps: dict[str, int] | None, - ) -> None: - """Handle state update, only if this entity's dpcode was actually updated.""" - if self._dpcode_wrapper.skip_update( + ) -> bool: + """Called when Tuya device sends an update with updated properties. + + Returns True if the Home Assistant state should be written, + or False if the state write should be skipped. + """ + return not self._dpcode_wrapper.skip_update( self.device, updated_status_properties, dp_timestamps - ): - return - self.async_write_ha_state() + ) async def async_turn_on(self, **kwargs: Any) -> None: """Turn the siren on.""" diff --git a/homeassistant/components/tuya/switch.py b/homeassistant/components/tuya/switch.py index dce5fec0ef07eb..353ff432bef54e 100644 --- a/homeassistant/components/tuya/switch.py +++ b/homeassistant/components/tuya/switch.py @@ -1040,17 +1040,19 @@ def is_on(self) -> bool | None: """Return true if switch is on.""" return self._read_wrapper(self._dpcode_wrapper) - async def _handle_state_update( + async def _process_device_update( self, - updated_status_properties: list[str] | None, + updated_status_properties: list[str], dp_timestamps: dict[str, int] | None, - ) -> None: - """Handle state update, only if this entity's dpcode was actually updated.""" - if self._dpcode_wrapper.skip_update( + ) -> bool: + """Called when Tuya device sends an update with updated properties. + + Returns True if the Home Assistant state should be written, + or False if the state write should be skipped. + """ + return not self._dpcode_wrapper.skip_update( self.device, updated_status_properties, dp_timestamps - ): - return - self.async_write_ha_state() + ) async def async_turn_on(self, **kwargs: Any) -> None: """Turn the switch on.""" diff --git a/homeassistant/components/tuya/valve.py b/homeassistant/components/tuya/valve.py index 01bf0f054f68c6..e617f59264e8e4 100644 --- a/homeassistant/components/tuya/valve.py +++ b/homeassistant/components/tuya/valve.py @@ -137,17 +137,19 @@ def is_closed(self) -> bool | None: return None return not is_open - async def _handle_state_update( + async def _process_device_update( self, - updated_status_properties: list[str] | None, + updated_status_properties: list[str], dp_timestamps: dict[str, int] | None, - ) -> None: - """Handle state update, only if this entity's dpcode was actually updated.""" - if self._dpcode_wrapper.skip_update( + ) -> bool: + """Called when Tuya device sends an update with updated properties. + + Returns True if the Home Assistant state should be written, + or False if the state write should be skipped. + """ + return not self._dpcode_wrapper.skip_update( self.device, updated_status_properties, dp_timestamps - ): - return - self.async_write_ha_state() + ) async def async_open_valve(self) -> None: """Open the valve.""" diff --git a/homeassistant/components/uptime_kuma/manifest.json b/homeassistant/components/uptime_kuma/manifest.json index dc323e7b088a8d..b234ca2ab683b7 100644 --- a/homeassistant/components/uptime_kuma/manifest.json +++ b/homeassistant/components/uptime_kuma/manifest.json @@ -8,5 +8,5 @@ "iot_class": "cloud_polling", "loggers": ["pythonkuma"], "quality_scale": "platinum", - "requirements": ["pythonkuma==0.4.1"] + "requirements": ["pythonkuma==0.5.0"] } diff --git a/homeassistant/generated/integrations.json b/homeassistant/generated/integrations.json index 546a664ac57376..d10c5015dc7356 100644 --- a/homeassistant/generated/integrations.json +++ b/homeassistant/generated/integrations.json @@ -257,7 +257,7 @@ "aws_s3": { "integration_type": "service", "config_flow": true, - "iot_class": "cloud_push", + "iot_class": "cloud_polling", "name": "AWS S3" }, "fire_tv": { @@ -6290,7 +6290,7 @@ }, "slimproto": { "name": "SlimProto (Squeezebox players)", - "integration_type": "hub", + "integration_type": "device", "config_flow": true, "iot_class": "local_push" }, diff --git a/mypy.ini b/mypy.ini index 5d93f1943bed5a..c68f0f50179a7e 100644 --- a/mypy.ini +++ b/mypy.ini @@ -245,6 +245,16 @@ disallow_untyped_defs = true warn_return_any = true warn_unreachable = true +[mypy-homeassistant.components.ai_task.*] +check_untyped_defs = true +disallow_incomplete_defs = true +disallow_subclassing_any = true +disallow_untyped_calls = true +disallow_untyped_decorators = true +disallow_untyped_defs = true +warn_return_any = true +warn_unreachable = true + [mypy-homeassistant.components.air_quality.*] check_untyped_defs = true disallow_incomplete_defs = true @@ -1846,6 +1856,16 @@ disallow_untyped_defs = true warn_return_any = true warn_unreachable = true +[mypy-homeassistant.components.folder_watcher.*] +check_untyped_defs = true +disallow_incomplete_defs = true +disallow_subclassing_any = true +disallow_untyped_calls = true +disallow_untyped_decorators = true +disallow_untyped_defs = true +warn_return_any = true +warn_unreachable = true + [mypy-homeassistant.components.forecast_solar.*] check_untyped_defs = true disallow_incomplete_defs = true @@ -2736,6 +2756,16 @@ disallow_untyped_defs = true warn_return_any = true warn_unreachable = true +[mypy-homeassistant.components.isal.*] +check_untyped_defs = true +disallow_incomplete_defs = true +disallow_subclassing_any = true +disallow_untyped_calls = true +disallow_untyped_decorators = true +disallow_untyped_defs = true +warn_return_any = true +warn_unreachable = true + [mypy-homeassistant.components.islamic_prayer_times.*] check_untyped_defs = true disallow_incomplete_defs = true @@ -2836,6 +2866,16 @@ disallow_untyped_defs = true warn_return_any = true warn_unreachable = true +[mypy-homeassistant.components.labs.*] +check_untyped_defs = true +disallow_incomplete_defs = true +disallow_subclassing_any = true +disallow_untyped_calls = true +disallow_untyped_decorators = true +disallow_untyped_defs = true +warn_return_any = true +warn_unreachable = true + [mypy-homeassistant.components.lacrosse.*] check_untyped_defs = true disallow_incomplete_defs = true @@ -3786,6 +3826,16 @@ disallow_untyped_defs = true warn_return_any = true warn_unreachable = true +[mypy-homeassistant.components.otp.*] +check_untyped_defs = true +disallow_incomplete_defs = true +disallow_subclassing_any = true +disallow_untyped_calls = true +disallow_untyped_decorators = true +disallow_untyped_defs = true +warn_return_any = true +warn_unreachable = true + [mypy-homeassistant.components.overkiz.*] check_untyped_defs = true disallow_incomplete_defs = true @@ -4136,6 +4186,16 @@ disallow_untyped_defs = true warn_return_any = true warn_unreachable = true +[mypy-homeassistant.components.random.*] +check_untyped_defs = true +disallow_incomplete_defs = true +disallow_subclassing_any = true +disallow_untyped_calls = true +disallow_untyped_decorators = true +disallow_untyped_defs = true +warn_return_any = true +warn_unreachable = true + [mypy-homeassistant.components.raspberry_pi.*] check_untyped_defs = true disallow_incomplete_defs = true @@ -4176,6 +4236,16 @@ disallow_untyped_defs = true warn_return_any = true warn_unreachable = true +[mypy-homeassistant.components.recovery_mode.*] +check_untyped_defs = true +disallow_incomplete_defs = true +disallow_subclassing_any = true +disallow_untyped_calls = true +disallow_untyped_decorators = true +disallow_untyped_defs = true +warn_return_any = true +warn_unreachable = true + [mypy-homeassistant.components.redgtech.*] check_untyped_defs = true disallow_incomplete_defs = true @@ -4486,6 +4556,16 @@ disallow_untyped_defs = true warn_return_any = true warn_unreachable = true +[mypy-homeassistant.components.season.*] +check_untyped_defs = true +disallow_incomplete_defs = true +disallow_subclassing_any = true +disallow_untyped_calls = true +disallow_untyped_decorators = true +disallow_untyped_defs = true +warn_return_any = true +warn_unreachable = true + [mypy-homeassistant.components.select.*] check_untyped_defs = true disallow_incomplete_defs = true @@ -5419,6 +5499,16 @@ disallow_untyped_defs = true warn_return_any = true warn_unreachable = true +[mypy-homeassistant.components.usage_prediction.*] +check_untyped_defs = true +disallow_incomplete_defs = true +disallow_subclassing_any = true +disallow_untyped_calls = true +disallow_untyped_decorators = true +disallow_untyped_defs = true +warn_return_any = true +warn_unreachable = true + [mypy-homeassistant.components.usb.*] check_untyped_defs = true disallow_incomplete_defs = true @@ -5599,6 +5689,16 @@ disallow_untyped_defs = true warn_return_any = true warn_unreachable = true +[mypy-homeassistant.components.web_rtc.*] +check_untyped_defs = true +disallow_incomplete_defs = true +disallow_subclassing_any = true +disallow_untyped_calls = true +disallow_untyped_decorators = true +disallow_untyped_defs = true +warn_return_any = true +warn_unreachable = true + [mypy-homeassistant.components.webhook.*] check_untyped_defs = true disallow_incomplete_defs = true diff --git a/requirements_all.txt b/requirements_all.txt index 71eb8041077a09..42eda5d8686826 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1859,7 +1859,7 @@ py-nextbusnext==2.3.0 py-nightscout==1.2.2 # homeassistant.components.mta -py-nymta==0.3.4 +py-nymta==0.4.0 # homeassistant.components.schluter py-schluter==0.1.7 @@ -2657,7 +2657,7 @@ python-xbox==0.1.3 pythonegardia==1.0.52 # homeassistant.components.uptime_kuma -pythonkuma==0.4.1 +pythonkuma==0.5.0 # homeassistant.components.tile pytile==2024.12.0 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 2d10f6e08df378..dd445d01a7c494 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -1608,7 +1608,7 @@ py-nextbusnext==2.3.0 py-nightscout==1.2.2 # homeassistant.components.mta -py-nymta==0.3.4 +py-nymta==0.4.0 # homeassistant.components.ecovacs py-sucks==0.9.11 @@ -2244,7 +2244,7 @@ python-telegram-bot[socks]==22.1 python-xbox==0.1.3 # homeassistant.components.uptime_kuma -pythonkuma==0.4.1 +pythonkuma==0.5.0 # homeassistant.components.tile pytile==2024.12.0 diff --git a/tests/components/aws_s3/__init__.py b/tests/components/aws_s3/__init__.py index 90e4652bb2b826..a807e2ac57e33b 100644 --- a/tests/components/aws_s3/__init__.py +++ b/tests/components/aws_s3/__init__.py @@ -1,6 +1,8 @@ """Tests for the AWS S3 integration.""" +from homeassistant.components.backup import DOMAIN as BACKUP_DOMAIN from homeassistant.core import HomeAssistant +from homeassistant.setup import async_setup_component from tests.common import MockConfigEntry @@ -9,6 +11,7 @@ async def setup_integration( hass: HomeAssistant, mock_config_entry: MockConfigEntry ) -> None: """Set up the S3 integration for testing.""" + assert await async_setup_component(hass, BACKUP_DOMAIN, {BACKUP_DOMAIN: {}}) mock_config_entry.add_to_hass(hass) await hass.config_entries.async_setup(mock_config_entry.entry_id) await hass.async_block_till_done() diff --git a/tests/components/aws_s3/snapshots/test_sensor.ambr b/tests/components/aws_s3/snapshots/test_sensor.ambr new file mode 100644 index 00000000000000..64f9c56dcff494 --- /dev/null +++ b/tests/components/aws_s3/snapshots/test_sensor.ambr @@ -0,0 +1,177 @@ +# serializer version: 1 +# name: test_sensor[large].2 + DeviceRegistryEntrySnapshot({ + 'area_id': None, + 'config_entries': , + 'config_entries_subentries': , + 'configuration_url': None, + 'connections': set({ + }), + 'disabled_by': None, + 'entry_type': , + 'hw_version': None, + 'id': , + 'identifiers': set({ + tuple( + 'aws_s3', + 'test', + ), + }), + 'labels': set({ + }), + 'manufacturer': 'AWS', + 'model': 'AWS S3', + 'model_id': None, + 'name': 'Bucket test', + 'name_by_user': None, + 'primary_config_entry': , + 'serial_number': None, + 'sw_version': None, + 'via_device_id': None, + }) +# --- +# name: test_sensor[large][sensor.bucket_test_total_size_of_backups-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.bucket_test_total_size_of_backups', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Total size of backups', + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 0, + }), + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Total size of backups', + 'platform': 'aws_s3', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'backups_size', + 'unique_id': 'test_backups_size', + 'unit_of_measurement': , + }) +# --- +# name: test_sensor[large][sensor.bucket_test_total_size_of_backups-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'data_size', + 'friendly_name': 'Bucket test Total size of backups', + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.bucket_test_total_size_of_backups', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '20.0', + }) +# --- +# name: test_sensor[small].2 + DeviceRegistryEntrySnapshot({ + 'area_id': None, + 'config_entries': , + 'config_entries_subentries': , + 'configuration_url': None, + 'connections': set({ + }), + 'disabled_by': None, + 'entry_type': , + 'hw_version': None, + 'id': , + 'identifiers': set({ + tuple( + 'aws_s3', + 'test', + ), + }), + 'labels': set({ + }), + 'manufacturer': 'AWS', + 'model': 'AWS S3', + 'model_id': None, + 'name': 'Bucket test', + 'name_by_user': None, + 'primary_config_entry': , + 'serial_number': None, + 'sw_version': None, + 'via_device_id': None, + }) +# --- +# name: test_sensor[small][sensor.bucket_test_total_size_of_backups-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.bucket_test_total_size_of_backups', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Total size of backups', + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 0, + }), + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Total size of backups', + 'platform': 'aws_s3', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'backups_size', + 'unique_id': 'test_backups_size', + 'unit_of_measurement': , + }) +# --- +# name: test_sensor[small][sensor.bucket_test_total_size_of_backups-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'data_size', + 'friendly_name': 'Bucket test Total size of backups', + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.bucket_test_total_size_of_backups', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '1.0', + }) +# --- diff --git a/tests/components/aws_s3/test_backup.py b/tests/components/aws_s3/test_backup.py index b62b10b801a343..e599a546c02544 100644 --- a/tests/components/aws_s3/test_backup.py +++ b/tests/components/aws_s3/test_backup.py @@ -387,7 +387,8 @@ async def test_agents_download( ) assert resp.status == 200 assert await resp.content.read() == b"backup data" - assert mock_client.get_object.call_count == 2 # One for metadata, one for tar file + # Coordinator first refresh reads metadata (1) + download reads metadata (1) + tar (1) + assert mock_client.get_object.call_count == 3 async def test_error_during_delete( @@ -431,11 +432,14 @@ async def test_cache_expiration( unique_id="test-unique-id", title="Test S3", ) - mock_entry.runtime_data = mock_client + mock_entry.runtime_data = MagicMock(client=mock_client) # Create agent agent = S3BackupAgent(hass, mock_entry) + # Reset call counts from coordinator's initial refresh + mock_client.reset_mock() + # Mock metadata response metadata_content = json.dumps(test_backup.as_dict()) mock_body = AsyncMock() @@ -542,7 +546,7 @@ async def test_list_backups_with_pagination( } # Setup mock client - mock_client = mock_config_entry.runtime_data + mock_client = mock_config_entry.runtime_data.client mock_client.get_paginator.return_value.paginate.return_value.__aiter__.return_value = [ page1, page2, diff --git a/tests/components/aws_s3/test_sensor.py b/tests/components/aws_s3/test_sensor.py new file mode 100644 index 00000000000000..0af9192ba6c0e0 --- /dev/null +++ b/tests/components/aws_s3/test_sensor.py @@ -0,0 +1,113 @@ +"""Tests for the AWS S3 sensor platform.""" + +from __future__ import annotations + +import json +from unittest.mock import AsyncMock + +from botocore.exceptions import BotoCoreError +from freezegun.api import FrozenDateTimeFactory +from syrupy.assertion import SnapshotAssertion + +from homeassistant.components.aws_s3.coordinator import SCAN_INTERVAL +from homeassistant.components.backup import AgentBackup +from homeassistant.const import STATE_UNAVAILABLE +from homeassistant.core import HomeAssistant +from homeassistant.helpers import device_registry as dr, entity_registry as er + +from . import setup_integration + +from tests.common import MockConfigEntry, async_fire_time_changed, snapshot_platform + + +async def test_sensor( + hass: HomeAssistant, + device_registry: dr.DeviceRegistry, + entity_registry: er.EntityRegistry, + snapshot: SnapshotAssertion, + mock_config_entry: MockConfigEntry, + mock_client: AsyncMock, +) -> None: + """Test the creation and values of the AWS S3 sensors.""" + await setup_integration(hass, mock_config_entry) + + await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id) + + assert ( + entity_entry := entity_registry.async_get( + "sensor.bucket_test_total_size_of_backups" + ) + ) + + assert entity_entry.device_id + assert (device_entry := device_registry.async_get(entity_entry.device_id)) + assert device_entry == snapshot + + +async def test_sensor_availability( + hass: HomeAssistant, + mock_client: AsyncMock, + mock_config_entry: MockConfigEntry, + freezer: FrozenDateTimeFactory, +) -> None: + """Test the availability handling of the AWS S3 sensors.""" + await setup_integration(hass, mock_config_entry) + + mock_client.get_paginator.return_value.paginate.side_effect = BotoCoreError() + + freezer.tick(SCAN_INTERVAL) + async_fire_time_changed(hass) + await hass.async_block_till_done() + + assert (state := hass.states.get("sensor.bucket_test_total_size_of_backups")) + assert state.state == STATE_UNAVAILABLE + + mock_client.get_paginator.return_value.paginate.side_effect = None + mock_client.get_paginator.return_value.paginate.return_value.__aiter__.return_value = [ + {"Contents": []} + ] + freezer.tick(SCAN_INTERVAL) + async_fire_time_changed(hass) + await hass.async_block_till_done() + + assert (state := hass.states.get("sensor.bucket_test_total_size_of_backups")) + assert state.state != STATE_UNAVAILABLE + + +async def test_calculate_backups_size( + hass: HomeAssistant, + mock_client: AsyncMock, + mock_config_entry: MockConfigEntry, + freezer: FrozenDateTimeFactory, + test_backup: AgentBackup, +) -> None: + """Test the total size of backups calculation.""" + mock_client.get_paginator.return_value.paginate.return_value.__aiter__.return_value = [ + {"Contents": []} + ] + await setup_integration(hass, mock_config_entry) + + assert (state := hass.states.get("sensor.bucket_test_total_size_of_backups")) + assert state.state == "0.0" + + # Add a backup + metadata_content = json.dumps(test_backup.as_dict()) + mock_body = AsyncMock() + mock_body.read.return_value = metadata_content.encode() + mock_client.get_object.return_value = {"Body": mock_body} + + mock_client.get_paginator.return_value.paginate.return_value.__aiter__.return_value = [ + { + "Contents": [ + {"Key": "backup.tar"}, + {"Key": "backup.metadata.json"}, + ] + } + ] + + freezer.tick(SCAN_INTERVAL) + async_fire_time_changed(hass) + await hass.async_block_till_done() + + assert (state := hass.states.get("sensor.bucket_test_total_size_of_backups")) + assert float(state.state) > 0 diff --git a/tests/components/duckdns/test_init.py b/tests/components/duckdns/test_init.py index 11e7dfbb9cf8cc..8821b292d166f5 100644 --- a/tests/components/duckdns/test_init.py +++ b/tests/components/duckdns/test_init.py @@ -19,6 +19,7 @@ from homeassistant.const import CONF_ACCESS_TOKEN, CONF_DOMAIN from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError, ServiceValidationError +from homeassistant.helpers import issue_registry as ir from homeassistant.util.dt import utcnow from .conftest import TEST_SUBDOMAIN, TEST_TOKEN @@ -118,7 +119,9 @@ async def test_setup_backoff( @pytest.mark.usefixtures("setup_duckdns") async def test_service_set_txt( - hass: HomeAssistant, aioclient_mock: AiohttpClientMocker + hass: HomeAssistant, + aioclient_mock: AiohttpClientMocker, + issue_registry: ir.IssueRegistry, ) -> None: """Test set txt service call.""" # Empty the fixture mock requests @@ -140,6 +143,11 @@ async def test_service_set_txt( ) assert aioclient_mock.call_count == 1 + assert issue_registry.async_get_issue( + domain=DOMAIN, + issue_id="deprecated_call_without_config_entry", + ) + @pytest.mark.usefixtures("setup_duckdns") async def test_service_clear_txt( diff --git a/tests/components/growatt_server/test_sensor.py b/tests/components/growatt_server/test_sensor.py index f7d520a524ccd6..666ad9e6c614e2 100644 --- a/tests/components/growatt_server/test_sensor.py +++ b/tests/components/growatt_server/test_sensor.py @@ -138,6 +138,301 @@ async def test_sensor_unavailable_on_coordinator_error( assert state.state == STATE_UNAVAILABLE +async def test_midnight_bounce_suppression( + hass: HomeAssistant, + mock_growatt_v1_api, + mock_config_entry: MockConfigEntry, + freezer: FrozenDateTimeFactory, +) -> None: + """Test that stale yesterday values after midnight reset are suppressed. + + The Growatt API sometimes delivers stale yesterday values after a midnight + reset (9.5 → 0 → 9.5 → 0), causing TOTAL_INCREASING double-counting. + """ + with patch("homeassistant.components.growatt_server.PLATFORMS", [Platform.SENSOR]): + await setup_integration(hass, mock_config_entry) + + entity_id = "sensor.test_plant_total_energy_today" + + # Initial state: 12.5 kWh produced today + state = hass.states.get(entity_id) + assert state is not None + assert state.state == "12.5" + + # Step 1: Midnight reset — API returns 0 (legitimate reset) + mock_growatt_v1_api.plant_energy_overview.return_value = { + "today_energy": 0, + "total_energy": 1250.0, + "current_power": 0, + } + freezer.tick(timedelta(minutes=5)) + async_fire_time_changed(hass) + await hass.async_block_till_done(wait_background_tasks=True) + + state = hass.states.get(entity_id) + assert state is not None + assert state.state == "0" + + # Step 2: Stale bounce — API returns yesterday's value (12.5) after reset + mock_growatt_v1_api.plant_energy_overview.return_value = { + "today_energy": 12.5, + "total_energy": 1250.0, + "current_power": 0, + } + freezer.tick(timedelta(minutes=5)) + async_fire_time_changed(hass) + await hass.async_block_till_done(wait_background_tasks=True) + + # Bounce should be suppressed — state stays at 0 + state = hass.states.get(entity_id) + assert state is not None + assert state.state == "0" + + # Step 3: Another reset arrives — still 0 (no double-counting) + mock_growatt_v1_api.plant_energy_overview.return_value = { + "today_energy": 0, + "total_energy": 1250.0, + "current_power": 0, + } + freezer.tick(timedelta(minutes=5)) + async_fire_time_changed(hass) + await hass.async_block_till_done(wait_background_tasks=True) + + state = hass.states.get(entity_id) + assert state is not None + assert state.state == "0" + + # Step 4: Genuine new production — small value passes through + mock_growatt_v1_api.plant_energy_overview.return_value = { + "today_energy": 0.1, + "total_energy": 1250.1, + "current_power": 500, + } + freezer.tick(timedelta(minutes=5)) + async_fire_time_changed(hass) + await hass.async_block_till_done(wait_background_tasks=True) + + state = hass.states.get(entity_id) + assert state is not None + assert state.state == "0.1" + + +async def test_normal_reset_no_bounce( + hass: HomeAssistant, + mock_growatt_v1_api, + mock_config_entry: MockConfigEntry, + freezer: FrozenDateTimeFactory, +) -> None: + """Test that normal midnight reset without bounce passes through correctly.""" + with patch("homeassistant.components.growatt_server.PLATFORMS", [Platform.SENSOR]): + await setup_integration(hass, mock_config_entry) + + entity_id = "sensor.test_plant_total_energy_today" + + # Initial state: 9.5 kWh + mock_growatt_v1_api.plant_energy_overview.return_value = { + "today_energy": 9.5, + "total_energy": 1250.0, + "current_power": 0, + } + freezer.tick(timedelta(minutes=5)) + async_fire_time_changed(hass) + await hass.async_block_till_done(wait_background_tasks=True) + + state = hass.states.get(entity_id) + assert state is not None + assert state.state == "9.5" + + # Midnight reset — API returns 0 + mock_growatt_v1_api.plant_energy_overview.return_value = { + "today_energy": 0, + "total_energy": 1250.0, + "current_power": 0, + } + freezer.tick(timedelta(minutes=5)) + async_fire_time_changed(hass) + await hass.async_block_till_done(wait_background_tasks=True) + + state = hass.states.get(entity_id) + assert state is not None + assert state.state == "0" + + # No bounce — genuine new production starts + mock_growatt_v1_api.plant_energy_overview.return_value = { + "today_energy": 0.1, + "total_energy": 1250.1, + "current_power": 500, + } + freezer.tick(timedelta(minutes=5)) + async_fire_time_changed(hass) + await hass.async_block_till_done(wait_background_tasks=True) + + state = hass.states.get(entity_id) + assert state is not None + assert state.state == "0.1" + + # Production continues normally + mock_growatt_v1_api.plant_energy_overview.return_value = { + "today_energy": 1.5, + "total_energy": 1251.5, + "current_power": 2000, + } + freezer.tick(timedelta(minutes=5)) + async_fire_time_changed(hass) + await hass.async_block_till_done(wait_background_tasks=True) + + state = hass.states.get(entity_id) + assert state is not None + assert state.state == "1.5" + + +async def test_midnight_bounce_repeated( + hass: HomeAssistant, + mock_growatt_v1_api, + mock_config_entry: MockConfigEntry, + freezer: FrozenDateTimeFactory, +) -> None: + """Test multiple consecutive stale bounces are all suppressed.""" + with patch("homeassistant.components.growatt_server.PLATFORMS", [Platform.SENSOR]): + await setup_integration(hass, mock_config_entry) + + entity_id = "sensor.test_plant_total_energy_today" + + # Set up a known pre-reset value + mock_growatt_v1_api.plant_energy_overview.return_value = { + "today_energy": 8.0, + "total_energy": 1250.0, + "current_power": 0, + } + freezer.tick(timedelta(minutes=5)) + async_fire_time_changed(hass) + await hass.async_block_till_done(wait_background_tasks=True) + + assert hass.states.get(entity_id).state == "8.0" + + # Midnight reset + mock_growatt_v1_api.plant_energy_overview.return_value = { + "today_energy": 0, + "total_energy": 1250.0, + "current_power": 0, + } + freezer.tick(timedelta(minutes=5)) + async_fire_time_changed(hass) + await hass.async_block_till_done(wait_background_tasks=True) + + assert hass.states.get(entity_id).state == "0" + + # First stale bounce — suppressed + mock_growatt_v1_api.plant_energy_overview.return_value = { + "today_energy": 8.0, + "total_energy": 1250.0, + "current_power": 0, + } + freezer.tick(timedelta(minutes=5)) + async_fire_time_changed(hass) + await hass.async_block_till_done(wait_background_tasks=True) + + assert hass.states.get(entity_id).state == "0" + + # Back to 0 + mock_growatt_v1_api.plant_energy_overview.return_value = { + "today_energy": 0, + "total_energy": 1250.0, + "current_power": 0, + } + freezer.tick(timedelta(minutes=5)) + async_fire_time_changed(hass) + await hass.async_block_till_done(wait_background_tasks=True) + + assert hass.states.get(entity_id).state == "0" + + # Second stale bounce — also suppressed + mock_growatt_v1_api.plant_energy_overview.return_value = { + "today_energy": 8.0, + "total_energy": 1250.0, + "current_power": 0, + } + freezer.tick(timedelta(minutes=5)) + async_fire_time_changed(hass) + await hass.async_block_till_done(wait_background_tasks=True) + + assert hass.states.get(entity_id).state == "0" + + # Back to 0 again + mock_growatt_v1_api.plant_energy_overview.return_value = { + "today_energy": 0, + "total_energy": 1250.0, + "current_power": 0, + } + freezer.tick(timedelta(minutes=5)) + async_fire_time_changed(hass) + await hass.async_block_till_done(wait_background_tasks=True) + + assert hass.states.get(entity_id).state == "0" + + # Finally, genuine new production passes through + mock_growatt_v1_api.plant_energy_overview.return_value = { + "today_energy": 0.2, + "total_energy": 1250.2, + "current_power": 1000, + } + freezer.tick(timedelta(minutes=5)) + async_fire_time_changed(hass) + await hass.async_block_till_done(wait_background_tasks=True) + + assert hass.states.get(entity_id).state == "0.2" + + +async def test_non_total_increasing_sensor_unaffected_by_bounce_suppression( + hass: HomeAssistant, + mock_growatt_v1_api, + mock_config_entry: MockConfigEntry, + freezer: FrozenDateTimeFactory, +) -> None: + """Test that non-TOTAL_INCREASING sensors are not affected by bounce suppression. + + The total_energy_output sensor (totalEnergy) has state_class=TOTAL, + so bounce suppression (which only targets TOTAL_INCREASING) should not apply. + """ + with patch("homeassistant.components.growatt_server.PLATFORMS", [Platform.SENSOR]): + await setup_integration(hass, mock_config_entry) + + # total_energy_output uses state_class=TOTAL (not TOTAL_INCREASING) + entity_id = "sensor.test_plant_total_lifetime_energy_output" + + state = hass.states.get(entity_id) + assert state is not None + assert state.state == "1250.0" + + # Simulate API returning 0 — no bounce suppression on TOTAL sensors + mock_growatt_v1_api.plant_energy_overview.return_value = { + "today_energy": 12.5, + "total_energy": 0, + "current_power": 2500, + } + freezer.tick(timedelta(minutes=5)) + async_fire_time_changed(hass) + await hass.async_block_till_done(wait_background_tasks=True) + + state = hass.states.get(entity_id) + assert state is not None + assert state.state == "0" + + # Value recovers — passes through without suppression + mock_growatt_v1_api.plant_energy_overview.return_value = { + "today_energy": 12.5, + "total_energy": 1250.0, + "current_power": 2500, + } + freezer.tick(timedelta(minutes=5)) + async_fire_time_changed(hass) + await hass.async_block_till_done(wait_background_tasks=True) + + state = hass.states.get(entity_id) + assert state is not None + assert state.state == "1250.0" + + @pytest.mark.usefixtures("entity_registry_enabled_by_default") async def test_total_sensors_classic_api( hass: HomeAssistant, diff --git a/tests/components/history_stats/conftest.py b/tests/components/history_stats/conftest.py index f8075179e944d7..63288aeff44b5a 100644 --- a/tests/components/history_stats/conftest.py +++ b/tests/components/history_stats/conftest.py @@ -15,6 +15,7 @@ DEFAULT_NAME, DOMAIN, ) +from homeassistant.components.sensor import CONF_STATE_CLASS from homeassistant.config_entries import SOURCE_USER from homeassistant.const import CONF_ENTITY_ID, CONF_NAME, CONF_STATE, CONF_TYPE from homeassistant.core import HomeAssistant, State @@ -48,6 +49,7 @@ async def get_config_to_integration_load() -> dict[str, Any]: CONF_TYPE: "count", CONF_START: "{{ as_timestamp(utcnow()) - 3600 }}", CONF_END: "{{ utcnow() }}", + CONF_STATE_CLASS: "measurement", } diff --git a/tests/components/history_stats/test_config_flow.py b/tests/components/history_stats/test_config_flow.py index 7b2ee47215e98c..ee57303884ad4d 100644 --- a/tests/components/history_stats/test_config_flow.py +++ b/tests/components/history_stats/test_config_flow.py @@ -17,6 +17,7 @@ DOMAIN, ) from homeassistant.components.recorder import Recorder +from homeassistant.components.sensor import CONF_STATE_CLASS from homeassistant.const import CONF_ENTITY_ID, CONF_NAME, CONF_STATE, CONF_TYPE from homeassistant.core import HomeAssistant, State from homeassistant.data_entry_flow import FlowResultType @@ -91,6 +92,7 @@ async def test_options_flow( user_input={ CONF_END: "{{ utcnow() }}", CONF_DURATION: {"hours": 8, "minutes": 0, "seconds": 0, "days": 20}, + CONF_STATE_CLASS: "total_increasing", }, ) await hass.async_block_till_done() @@ -103,6 +105,7 @@ async def test_options_flow( CONF_TYPE: "count", CONF_END: "{{ utcnow() }}", CONF_DURATION: {"hours": 8, "minutes": 0, "seconds": 0, "days": 20}, + CONF_STATE_CLASS: "total_increasing", } await hass.async_block_till_done() @@ -387,6 +390,7 @@ def _fake_states(*args, **kwargs): CONF_STATE: ["on"], CONF_END: "{{ now() }}", CONF_START: "{{ today_at() }}", + CONF_STATE_CLASS: "measurement", }, title=DEFAULT_NAME, ) @@ -422,6 +426,7 @@ def _fake_states(*args, **kwargs): CONF_STATE: ["on"], CONF_END: end, CONF_START: "{{ today_at() }}", + CONF_STATE_CLASS: "measurement", }, } ) diff --git a/tests/components/history_stats/test_init.py b/tests/components/history_stats/test_init.py index fa003119f32b95..4e3b96e020d716 100644 --- a/tests/components/history_stats/test_init.py +++ b/tests/components/history_stats/test_init.py @@ -16,6 +16,7 @@ DEFAULT_NAME, DOMAIN, ) +from homeassistant.components.sensor import CONF_STATE_CLASS, SensorStateClass from homeassistant.config_entries import ConfigEntry, ConfigEntryState from homeassistant.const import CONF_ENTITY_ID, CONF_NAME, CONF_STATE, CONF_TYPE from homeassistant.core import Event, HomeAssistant, callback @@ -419,7 +420,58 @@ async def test_migration_1_1( assert history_stats_entity_entry.device_id == sensor_entity_entry.device_id assert history_stats_config_entry.version == 1 - assert history_stats_config_entry.minor_version == 2 + assert ( + history_stats_config_entry.minor_version + == HistoryStatsConfigFlowHandler.MINOR_VERSION + ) + + +@pytest.mark.usefixtures("recorder_mock") +async def test_migration_1_2( + hass: HomeAssistant, + device_registry: dr.DeviceRegistry, + entity_registry: er.EntityRegistry, + sensor_entity_entry: er.RegistryEntry, + sensor_device: dr.DeviceEntry, +) -> None: + """Test migration from v1.2 sets state_class to measurement.""" + + history_stats_config_entry = MockConfigEntry( + data={}, + domain=DOMAIN, + options={ + CONF_NAME: DEFAULT_NAME, + CONF_ENTITY_ID: sensor_entity_entry.entity_id, + CONF_STATE: ["on"], + CONF_TYPE: "count", + CONF_START: "{{ as_timestamp(utcnow()) - 3600 }}", + CONF_END: "{{ utcnow() }}", + }, + title="My history stats", + version=1, + minor_version=2, + ) + history_stats_config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(history_stats_config_entry.entry_id) + await hass.async_block_till_done() + + assert history_stats_config_entry.state is ConfigEntryState.LOADED + + assert ( + history_stats_config_entry.options.get(CONF_STATE_CLASS) + == SensorStateClass.MEASUREMENT + ) + assert history_stats_config_entry.version == 1 + assert ( + history_stats_config_entry.minor_version + == HistoryStatsConfigFlowHandler.MINOR_VERSION + ) + + assert hass.states.get("sensor.my_history_stats") is not None + assert ( + hass.states.get("sensor.my_history_stats").attributes.get(CONF_STATE_CLASS) + == SensorStateClass.MEASUREMENT + ) @pytest.mark.usefixtures("recorder_mock") diff --git a/tests/components/history_stats/test_sensor.py b/tests/components/history_stats/test_sensor.py index 5b98000997e059..fa75e72f4e1e13 100644 --- a/tests/components/history_stats/test_sensor.py +++ b/tests/components/history_stats/test_sensor.py @@ -120,6 +120,16 @@ async def test_setup_multiple_states( "end": "{{ utcnow() }}", "duration": "01:00", }, + { + "platform": "history_stats", + "entity_id": "binary_sensor.test_id", + "name": "Test", + "state": "on", + "start": "{{ as_timestamp(utcnow()) - 3600 }}", + "end": "{{ utcnow() }}", + "type": "ratio", + "state_class": "total_increasing", + }, ], ) @pytest.mark.usefixtures("hass") @@ -321,6 +331,7 @@ def _fake_states(*args, **kwargs): "start": "{{ as_timestamp(utcnow()) - 3600 }}", "end": "{{ utcnow() }}", "type": "time", + "state_class": "measurement", }, { "platform": "history_stats", @@ -330,6 +341,7 @@ def _fake_states(*args, **kwargs): "start": "{{ as_timestamp(utcnow()) - 3600 }}", "end": "{{ utcnow() }}", "type": "time", + "state_class": "total_increasing", }, { "platform": "history_stats", @@ -362,6 +374,20 @@ def _fake_states(*args, **kwargs): assert hass.states.get("sensor.sensor3").state == "2" assert hass.states.get("sensor.sensor4").state == "50.0" + assert ( + hass.states.get("sensor.sensor1").attributes.get("state_class") == "measurement" + ) + assert ( + hass.states.get("sensor.sensor2").attributes.get("state_class") + == "total_increasing" + ) + assert ( + hass.states.get("sensor.sensor3").attributes.get("state_class") == "measurement" + ) + assert ( + hass.states.get("sensor.sensor4").attributes.get("state_class") == "measurement" + ) + async def test_measure(recorder_mock: Recorder, hass: HomeAssistant) -> None: """Test the history statistics sensor measure.""" diff --git a/tests/components/uptime_kuma/conftest.py b/tests/components/uptime_kuma/conftest.py index 9c388ce2c254ac..40b93f0ef90cbc 100644 --- a/tests/components/uptime_kuma/conftest.py +++ b/tests/components/uptime_kuma/conftest.py @@ -70,6 +70,7 @@ def mock_pythonkuma() -> Generator[AsyncMock]: monitor_response_time_seconds_1d=0.10920649819494585, monitor_response_time_seconds_30d=0.0993296843901052, monitor_response_time_seconds_365d=0.1043971646081903, + monitor_tags=["tag1", "tag2:value"], ) monitor_2 = UptimeKumaMonitor( monitor_id=2, @@ -88,6 +89,7 @@ def mock_pythonkuma() -> Generator[AsyncMock]: monitor_response_time_seconds_1d=0.16390272373540857, monitor_response_time_seconds_30d=0.3371273224043715, monitor_response_time_seconds_365d=0.34270098747886596, + monitor_tags=["tag1", "tag2:value"], ) monitor_3 = UptimeKumaMonitor( monitor_id=3, @@ -106,6 +108,7 @@ def mock_pythonkuma() -> Generator[AsyncMock]: monitor_response_time_seconds_1d=None, monitor_response_time_seconds_30d=None, monitor_response_time_seconds_365d=None, + monitor_tags=[], ) with ( diff --git a/tests/components/uptime_kuma/snapshots/test_diagnostics.ambr b/tests/components/uptime_kuma/snapshots/test_diagnostics.ambr index adca1e02227065..63acc7cd39cb08 100644 --- a/tests/components/uptime_kuma/snapshots/test_diagnostics.ambr +++ b/tests/components/uptime_kuma/snapshots/test_diagnostics.ambr @@ -13,6 +13,10 @@ 'monitor_response_time_seconds_30d': 0.0993296843901052, 'monitor_response_time_seconds_365d': 0.1043971646081903, 'monitor_status': 1, + 'monitor_tags': list([ + 'tag1', + 'tag2:value', + ]), 'monitor_type': 'http', 'monitor_uptime_ratio_1d': 1, 'monitor_uptime_ratio_30d': 0.9993369956431142, @@ -31,6 +35,10 @@ 'monitor_response_time_seconds_30d': 0.3371273224043715, 'monitor_response_time_seconds_365d': 0.34270098747886596, 'monitor_status': 1, + 'monitor_tags': list([ + 'tag1', + 'tag2:value', + ]), 'monitor_type': 'port', 'monitor_uptime_ratio_1d': 0.9992223950233281, 'monitor_uptime_ratio_30d': 0.9990979870869731, @@ -49,6 +57,8 @@ 'monitor_response_time_seconds_30d': None, 'monitor_response_time_seconds_365d': None, 'monitor_status': 0, + 'monitor_tags': list([ + ]), 'monitor_type': 'json-query', 'monitor_uptime_ratio_1d': None, 'monitor_uptime_ratio_30d': None, diff --git a/tests/components/uptime_kuma/test_sensor.py b/tests/components/uptime_kuma/test_sensor.py index 873c16c4174bac..5e38d0a6b34af1 100644 --- a/tests/components/uptime_kuma/test_sensor.py +++ b/tests/components/uptime_kuma/test_sensor.py @@ -64,6 +64,7 @@ async def test_migrate_unique_id( monitor_port="null", monitor_status=MonitorStatus.UP, monitor_url="test", + monitor_tags=["tag1", "tag2:value"], ) } mock_pythonkuma.version = UptimeKumaVersion( @@ -86,6 +87,7 @@ async def test_migrate_unique_id( monitor_port="null", monitor_status=MonitorStatus.UP, monitor_url="test", + monitor_tags=["tag1", "tag2:value"], ) } mock_pythonkuma.version = UptimeKumaVersion(