diff --git a/homeassistant/components/playstation_network/sensor.py b/homeassistant/components/playstation_network/sensor.py index 86aab0feaf625..4e91bf2f1bbf3 100644 --- a/homeassistant/components/playstation_network/sensor.py +++ b/homeassistant/components/playstation_network/sensor.py @@ -11,6 +11,7 @@ SensorDeviceClass, SensorEntity, SensorEntityDescription, + SensorStateClass, ) from homeassistant.const import PERCENTAGE from homeassistant.core import HomeAssistant @@ -61,6 +62,7 @@ class PlaystationNetworkSensor(StrEnum): value_fn=( lambda psn: psn.trophy_summary.trophy_level if psn.trophy_summary else None ), + state_class=SensorStateClass.MEASUREMENT, ), PlaystationNetworkSensorEntityDescription( key=PlaystationNetworkSensor.TROPHY_LEVEL_PROGRESS, @@ -69,6 +71,7 @@ class PlaystationNetworkSensor(StrEnum): lambda psn: psn.trophy_summary.progress if psn.trophy_summary else None ), native_unit_of_measurement=PERCENTAGE, + state_class=SensorStateClass.MEASUREMENT, ), PlaystationNetworkSensorEntityDescription( key=PlaystationNetworkSensor.EARNED_TROPHIES_PLATINUM, @@ -80,6 +83,7 @@ class PlaystationNetworkSensor(StrEnum): else None ) ), + state_class=SensorStateClass.MEASUREMENT, ), PlaystationNetworkSensorEntityDescription( key=PlaystationNetworkSensor.EARNED_TROPHIES_GOLD, @@ -89,6 +93,7 @@ class PlaystationNetworkSensor(StrEnum): psn.trophy_summary.earned_trophies.gold if psn.trophy_summary else None ) ), + state_class=SensorStateClass.MEASUREMENT, ), PlaystationNetworkSensorEntityDescription( key=PlaystationNetworkSensor.EARNED_TROPHIES_SILVER, @@ -100,6 +105,7 @@ class PlaystationNetworkSensor(StrEnum): else None ) ), + state_class=SensorStateClass.MEASUREMENT, ), PlaystationNetworkSensorEntityDescription( key=PlaystationNetworkSensor.EARNED_TROPHIES_BRONZE, @@ -111,6 +117,7 @@ class PlaystationNetworkSensor(StrEnum): else None ) ), + state_class=SensorStateClass.MEASUREMENT, ), PlaystationNetworkSensorEntityDescription( key=PlaystationNetworkSensor.ONLINE_ID, diff --git a/homeassistant/components/splunk/config_flow.py b/homeassistant/components/splunk/config_flow.py index 7a2e98a781553..6f84f9fab5d41 100644 --- a/homeassistant/components/splunk/config_flow.py +++ b/homeassistant/components/splunk/config_flow.py @@ -85,6 +85,40 @@ async def async_step_import( data=import_config, ) + async def async_step_reconfigure( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Handle reconfiguration of the Splunk integration.""" + errors: dict[str, str] = {} + + if user_input is not None: + errors = await self._async_validate_input(user_input) + + if not errors: + return self.async_update_reload_and_abort( + self._get_reconfigure_entry(), + data_updates=user_input, + title=f"{user_input[CONF_HOST]}:{user_input[CONF_PORT]}", + ) + + return self.async_show_form( + step_id="reconfigure", + data_schema=self.add_suggested_values_to_schema( + vol.Schema( + { + vol.Required(CONF_TOKEN): str, + vol.Required(CONF_HOST): str, + vol.Optional(CONF_PORT, default=DEFAULT_PORT): int, + vol.Optional(CONF_SSL, default=False): bool, + vol.Optional(CONF_VERIFY_SSL, default=True): bool, + vol.Optional(CONF_NAME): str, + } + ), + self._get_reconfigure_entry().data, + ), + errors=errors, + ) + async def async_step_reauth( self, entry_data: Mapping[str, Any] ) -> ConfigFlowResult: diff --git a/homeassistant/components/splunk/quality_scale.yaml b/homeassistant/components/splunk/quality_scale.yaml index eb74b98e13d15..acd87aff519fe 100644 --- a/homeassistant/components/splunk/quality_scale.yaml +++ b/homeassistant/components/splunk/quality_scale.yaml @@ -112,11 +112,7 @@ rules: status: exempt comment: | Integration does not create entities. - reconfiguration-flow: - status: todo - comment: | - Consider adding reconfiguration flow to allow users to update host, port, entity filter, and SSL settings without deleting and re-adding the config entry. - + reconfiguration-flow: done # Platinum async-dependency: status: todo diff --git a/homeassistant/components/splunk/strings.json b/homeassistant/components/splunk/strings.json index 822d87e759cc8..20c7df3bf61c5 100644 --- a/homeassistant/components/splunk/strings.json +++ b/homeassistant/components/splunk/strings.json @@ -6,6 +6,7 @@ "invalid_auth": "[%key:common::config_flow::error::invalid_auth%]", "invalid_config": "The YAML configuration is invalid and cannot be imported. Please check your configuration.yaml file.", "reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]", + "reconfigure_successful": "[%key:common::config_flow::abort::reconfigure_successful%]", "single_instance_allowed": "[%key:common::config_flow::abort::single_instance_allowed%]", "unknown": "[%key:common::config_flow::error::unknown%]" }, @@ -26,6 +27,25 @@ "description": "The Splunk token is no longer valid. Please enter a new HTTP Event Collector token.", "title": "Reauthenticate Splunk" }, + "reconfigure": { + "data": { + "host": "[%key:common::config_flow::data::host%]", + "name": "[%key:common::config_flow::data::name%]", + "port": "[%key:common::config_flow::data::port%]", + "ssl": "[%key:common::config_flow::data::ssl%]", + "token": "HTTP Event Collector token", + "verify_ssl": "[%key:common::config_flow::data::verify_ssl%]" + }, + "data_description": { + "host": "[%key:component::splunk::config::step::user::data_description::host%]", + "name": "[%key:component::splunk::config::step::user::data_description::name%]", + "port": "[%key:component::splunk::config::step::user::data_description::port%]", + "ssl": "[%key:component::splunk::config::step::user::data_description::ssl%]", + "token": "[%key:component::splunk::config::step::user::data_description::token%]", + "verify_ssl": "[%key:component::splunk::config::step::user::data_description::verify_ssl%]" + }, + "description": "Update your Splunk HTTP Event Collector connection settings." + }, "user": { "data": { "host": "[%key:common::config_flow::data::host%]", diff --git a/homeassistant/components/xbox/api.py b/homeassistant/components/xbox/api.py index b772cae591290..a3d3a287c96e7 100644 --- a/homeassistant/components/xbox/api.py +++ b/homeassistant/components/xbox/api.py @@ -1,13 +1,17 @@ """API for xbox bound to Home Assistant OAuth.""" -from http import HTTPStatus - -from aiohttp.client_exceptions import ClientResponseError -from httpx import AsyncClient +from aiohttp import ClientError +from httpx import AsyncClient, HTTPStatusError, RequestError from pythonxbox.authentication.manager import AuthenticationManager from pythonxbox.authentication.models import OAuth2TokenResponse +from pythonxbox.common.exceptions import AuthenticationException -from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady +from homeassistant.exceptions import ( + ConfigEntryAuthFailed, + ConfigEntryNotReady, + OAuth2TokenRequestReauthError, + OAuth2TokenRequestTransientError, +) from homeassistant.helpers.config_entry_oauth2_flow import OAuth2Session from homeassistant.util.dt import utc_from_timestamp @@ -30,16 +34,12 @@ async def refresh_tokens(self) -> None: if not self._oauth_session.valid_token: try: await self._oauth_session.async_ensure_token_valid() - except ClientResponseError as e: - if ( - HTTPStatus.BAD_REQUEST - <= e.status - < HTTPStatus.INTERNAL_SERVER_ERROR - ): - raise ConfigEntryAuthFailed( - translation_domain=DOMAIN, - translation_key="auth_exception", - ) from e + except OAuth2TokenRequestReauthError as e: + raise ConfigEntryAuthFailed( + translation_domain=DOMAIN, + translation_key="auth_exception", + ) from e + except (OAuth2TokenRequestTransientError, ClientError) as e: raise ConfigEntryNotReady( translation_domain=DOMAIN, translation_key="request_exception", @@ -47,7 +47,18 @@ async def refresh_tokens(self) -> None: self.oauth = self._get_oauth_token() # This will skip the OAuth refresh and only refresh User and XSTS tokens - await super().refresh_tokens() + try: + await super().refresh_tokens() + except AuthenticationException as e: + raise ConfigEntryAuthFailed( + translation_domain=DOMAIN, + translation_key="auth_exception", + ) from e + except (RequestError, HTTPStatusError) as e: + raise ConfigEntryNotReady( + translation_domain=DOMAIN, + translation_key="request_exception", + ) from e def _get_oauth_token(self) -> OAuth2TokenResponse: tokens = {**self._oauth_session.token} diff --git a/homeassistant/components/xbox/sensor.py b/homeassistant/components/xbox/sensor.py index 92907c92c6474..72028bbab216e 100644 --- a/homeassistant/components/xbox/sensor.py +++ b/homeassistant/components/xbox/sensor.py @@ -160,6 +160,7 @@ def title_logo(_: Person, title: Title | None) -> str | None: key=XboxSensor.GAMER_SCORE, translation_key=XboxSensor.GAMER_SCORE, value_fn=lambda x, _: x.gamer_score, + state_class=SensorStateClass.MEASUREMENT, ), XboxSensorEntityDescription( key=XboxSensor.ACCOUNT_TIER, @@ -187,11 +188,13 @@ def title_logo(_: Person, title: Title | None) -> str | None: key=XboxSensor.FOLLOWING, translation_key=XboxSensor.FOLLOWING, value_fn=lambda x, _: x.detail.following_count if x.detail else None, + state_class=SensorStateClass.MEASUREMENT, ), XboxSensorEntityDescription( key=XboxSensor.FOLLOWER, translation_key=XboxSensor.FOLLOWER, value_fn=lambda x, _: x.detail.follower_count if x.detail else None, + state_class=SensorStateClass.MEASUREMENT, ), XboxSensorEntityDescription( key=XboxSensor.NOW_PLAYING, @@ -204,6 +207,7 @@ def title_logo(_: Person, title: Title | None) -> str | None: key=XboxSensor.FRIENDS, translation_key=XboxSensor.FRIENDS, value_fn=lambda x, _: x.detail.friend_count if x.detail else None, + state_class=SensorStateClass.MEASUREMENT, ), XboxSensorEntityDescription( key=XboxSensor.IN_PARTY, diff --git a/tests/components/playstation_network/snapshots/test_sensor.ambr b/tests/components/playstation_network/snapshots/test_sensor.ambr index 55f92c1479cf8..3a525a8e278a9 100644 --- a/tests/components/playstation_network/snapshots/test_sensor.ambr +++ b/tests/components/playstation_network/snapshots/test_sensor.ambr @@ -4,7 +4,9 @@ 'aliases': set({ }), 'area_id': None, - 'capabilities': None, + 'capabilities': dict({ + 'state_class': , + }), 'config_entry_id': , 'config_subentry_id': , 'device_class': None, @@ -39,6 +41,7 @@ StateSnapshot({ 'attributes': ReadOnlyDict({ 'friendly_name': 'PublicUniversalFriend Bronze trophies', + 'state_class': , 'unit_of_measurement': 'trophies', }), 'context': , @@ -54,7 +57,9 @@ 'aliases': set({ }), 'area_id': None, - 'capabilities': None, + 'capabilities': dict({ + 'state_class': , + }), 'config_entry_id': , 'config_subentry_id': , 'device_class': None, @@ -89,6 +94,7 @@ StateSnapshot({ 'attributes': ReadOnlyDict({ 'friendly_name': 'PublicUniversalFriend Gold trophies', + 'state_class': , 'unit_of_measurement': 'trophies', }), 'context': , @@ -154,7 +160,9 @@ 'aliases': set({ }), 'area_id': None, - 'capabilities': None, + 'capabilities': dict({ + 'state_class': , + }), 'config_entry_id': , 'config_subentry_id': , 'device_class': None, @@ -189,6 +197,7 @@ StateSnapshot({ 'attributes': ReadOnlyDict({ 'friendly_name': 'PublicUniversalFriend Next level', + 'state_class': , 'unit_of_measurement': '%', }), 'context': , @@ -365,7 +374,9 @@ 'aliases': set({ }), 'area_id': None, - 'capabilities': None, + 'capabilities': dict({ + 'state_class': , + }), 'config_entry_id': , 'config_subentry_id': , 'device_class': None, @@ -400,6 +411,7 @@ StateSnapshot({ 'attributes': ReadOnlyDict({ 'friendly_name': 'PublicUniversalFriend Platinum trophies', + 'state_class': , 'unit_of_measurement': 'trophies', }), 'context': , @@ -415,7 +427,9 @@ 'aliases': set({ }), 'area_id': None, - 'capabilities': None, + 'capabilities': dict({ + 'state_class': , + }), 'config_entry_id': , 'config_subentry_id': , 'device_class': None, @@ -450,6 +464,7 @@ StateSnapshot({ 'attributes': ReadOnlyDict({ 'friendly_name': 'PublicUniversalFriend Silver trophies', + 'state_class': , 'unit_of_measurement': 'trophies', }), 'context': , @@ -465,7 +480,9 @@ 'aliases': set({ }), 'area_id': None, - 'capabilities': None, + 'capabilities': dict({ + 'state_class': , + }), 'config_entry_id': , 'config_subentry_id': , 'device_class': None, @@ -500,6 +517,7 @@ StateSnapshot({ 'attributes': ReadOnlyDict({ 'friendly_name': 'PublicUniversalFriend Trophy level', + 'state_class': , }), 'context': , 'entity_id': 'sensor.publicuniversalfriend_trophy_level', @@ -514,7 +532,9 @@ 'aliases': set({ }), 'area_id': None, - 'capabilities': None, + 'capabilities': dict({ + 'state_class': , + }), 'config_entry_id': , 'config_subentry_id': , 'device_class': None, @@ -549,6 +569,7 @@ StateSnapshot({ 'attributes': ReadOnlyDict({ 'friendly_name': 'testuser Bronze trophies', + 'state_class': , 'unit_of_measurement': 'trophies', }), 'context': , @@ -564,7 +585,9 @@ 'aliases': set({ }), 'area_id': None, - 'capabilities': None, + 'capabilities': dict({ + 'state_class': , + }), 'config_entry_id': , 'config_subentry_id': , 'device_class': None, @@ -599,6 +622,7 @@ StateSnapshot({ 'attributes': ReadOnlyDict({ 'friendly_name': 'testuser Gold trophies', + 'state_class': , 'unit_of_measurement': 'trophies', }), 'context': , @@ -664,7 +688,9 @@ 'aliases': set({ }), 'area_id': None, - 'capabilities': None, + 'capabilities': dict({ + 'state_class': , + }), 'config_entry_id': , 'config_subentry_id': , 'device_class': None, @@ -699,6 +725,7 @@ StateSnapshot({ 'attributes': ReadOnlyDict({ 'friendly_name': 'testuser Next level', + 'state_class': , 'unit_of_measurement': '%', }), 'context': , @@ -876,7 +903,9 @@ 'aliases': set({ }), 'area_id': None, - 'capabilities': None, + 'capabilities': dict({ + 'state_class': , + }), 'config_entry_id': , 'config_subentry_id': , 'device_class': None, @@ -911,6 +940,7 @@ StateSnapshot({ 'attributes': ReadOnlyDict({ 'friendly_name': 'testuser Platinum trophies', + 'state_class': , 'unit_of_measurement': 'trophies', }), 'context': , @@ -926,7 +956,9 @@ 'aliases': set({ }), 'area_id': None, - 'capabilities': None, + 'capabilities': dict({ + 'state_class': , + }), 'config_entry_id': , 'config_subentry_id': , 'device_class': None, @@ -961,6 +993,7 @@ StateSnapshot({ 'attributes': ReadOnlyDict({ 'friendly_name': 'testuser Silver trophies', + 'state_class': , 'unit_of_measurement': 'trophies', }), 'context': , @@ -976,7 +1009,9 @@ 'aliases': set({ }), 'area_id': None, - 'capabilities': None, + 'capabilities': dict({ + 'state_class': , + }), 'config_entry_id': , 'config_subentry_id': , 'device_class': None, @@ -1011,6 +1046,7 @@ StateSnapshot({ 'attributes': ReadOnlyDict({ 'friendly_name': 'testuser Trophy level', + 'state_class': , }), 'context': , 'entity_id': 'sensor.testuser_trophy_level', diff --git a/tests/components/splunk/test_config_flow.py b/tests/components/splunk/test_config_flow.py index d7ed019becd0a..dd5d33dd0cf0c 100644 --- a/tests/components/splunk/test_config_flow.py +++ b/tests/components/splunk/test_config_flow.py @@ -224,6 +224,94 @@ async def test_import_flow_already_configured( assert result["reason"] == "single_instance_allowed" +async def test_reconfigure_flow_success( + hass: HomeAssistant, mock_hass_splunk: AsyncMock, mock_config_entry: MockConfigEntry +) -> None: + """Test successful reconfigure flow.""" + mock_config_entry.add_to_hass(hass) + + result = await mock_config_entry.start_reconfigure_flow(hass) + + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "reconfigure" + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + { + CONF_TOKEN: "new-token-456", + CONF_HOST: "new-splunk.example.com", + CONF_PORT: 9088, + CONF_SSL: True, + CONF_VERIFY_SSL: False, + CONF_NAME: "Updated Splunk", + }, + ) + + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "reconfigure_successful" + assert mock_config_entry.data[CONF_HOST] == "new-splunk.example.com" + assert mock_config_entry.data[CONF_PORT] == 9088 + assert mock_config_entry.data[CONF_TOKEN] == "new-token-456" + assert mock_config_entry.data[CONF_SSL] is True + assert mock_config_entry.data[CONF_VERIFY_SSL] is False + assert mock_config_entry.data[CONF_NAME] == "Updated Splunk" + assert mock_config_entry.title == "new-splunk.example.com:9088" + + +@pytest.mark.parametrize( + ("side_effect", "error"), + [ + ([False, True], "cannot_connect"), + ([True, False], "invalid_auth"), + (Exception("Unexpected error"), "unknown"), + ], +) +async def test_reconfigure_flow_error_and_recovery( + hass: HomeAssistant, + mock_hass_splunk: AsyncMock, + mock_config_entry: MockConfigEntry, + side_effect: list[bool] | Exception, + error: str, +) -> None: + """Test reconfigure flow errors and recovery.""" + mock_config_entry.add_to_hass(hass) + + result = await mock_config_entry.start_reconfigure_flow(hass) + + mock_hass_splunk.check.side_effect = side_effect + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + { + CONF_TOKEN: "test-token-123", + CONF_HOST: "new-splunk.example.com", + CONF_PORT: 8088, + CONF_SSL: False, + }, + ) + + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "reconfigure" + assert result["errors"] == {"base": error} + + # Test recovery + mock_hass_splunk.check.side_effect = None + mock_hass_splunk.check.return_value = True + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + { + CONF_TOKEN: "test-token-123", + CONF_HOST: "new-splunk.example.com", + CONF_PORT: 8088, + CONF_SSL: False, + }, + ) + + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "reconfigure_successful" + + async def test_reauth_flow_success( hass: HomeAssistant, mock_hass_splunk: AsyncMock, mock_config_entry: MockConfigEntry ) -> None: diff --git a/tests/components/xbox/conftest.py b/tests/components/xbox/conftest.py index 9d8d1e1896875..61f17f6c359d3 100644 --- a/tests/components/xbox/conftest.py +++ b/tests/components/xbox/conftest.py @@ -104,6 +104,30 @@ def mock_authentication_manager() -> Generator[AsyncMock]: yield client +@pytest.fixture(name="oauth2_session") +def mock_oauth2_session() -> Generator[AsyncMock]: + """Mock OAuth2 session.""" + + with patch( + "homeassistant.components.xbox.OAuth2Session", autospec=True + ) as mock_client: + client = mock_client.return_value + + client.token = { + "access_token": "1234567890", + "expires_at": 1760697327.7298331, + "expires_in": 3600, + "refresh_token": "0987654321", + "scope": "XboxLive.signin XboxLive.offline_access", + "service": "xbox", + "token_type": "bearer", + "user_id": "AAAAAAAAAAAAAAAAAAAAA", + } + client.valid_token = False + + yield client + + @pytest.fixture(name="xbox_live_client") def mock_xbox_live_client() -> Generator[AsyncMock]: """Mock xbox-webapi XboxLiveClient.""" diff --git a/tests/components/xbox/snapshots/test_sensor.ambr b/tests/components/xbox/snapshots/test_sensor.ambr index a19fedf53fa06..f2a0ed67c567d 100644 --- a/tests/components/xbox/snapshots/test_sensor.ambr +++ b/tests/components/xbox/snapshots/test_sensor.ambr @@ -4,7 +4,9 @@ 'aliases': set({ }), 'area_id': None, - 'capabilities': None, + 'capabilities': dict({ + 'state_class': , + }), 'config_entry_id': , 'config_subentry_id': , 'device_class': None, @@ -39,6 +41,7 @@ StateSnapshot({ 'attributes': ReadOnlyDict({ 'friendly_name': 'erics273 Follower', + 'state_class': , 'unit_of_measurement': 'people', }), 'context': , @@ -54,7 +57,9 @@ 'aliases': set({ }), 'area_id': None, - 'capabilities': None, + 'capabilities': dict({ + 'state_class': , + }), 'config_entry_id': , 'config_subentry_id': , 'device_class': None, @@ -89,6 +94,7 @@ StateSnapshot({ 'attributes': ReadOnlyDict({ 'friendly_name': 'erics273 Following', + 'state_class': , 'unit_of_measurement': 'people', }), 'context': , @@ -104,7 +110,9 @@ 'aliases': set({ }), 'area_id': None, - 'capabilities': None, + 'capabilities': dict({ + 'state_class': , + }), 'config_entry_id': , 'config_subentry_id': , 'device_class': None, @@ -139,6 +147,7 @@ StateSnapshot({ 'attributes': ReadOnlyDict({ 'friendly_name': 'erics273 Friends', + 'state_class': , 'unit_of_measurement': 'people', }), 'context': , @@ -154,7 +163,9 @@ 'aliases': set({ }), 'area_id': None, - 'capabilities': None, + 'capabilities': dict({ + 'state_class': , + }), 'config_entry_id': , 'config_subentry_id': , 'device_class': None, @@ -189,6 +200,7 @@ StateSnapshot({ 'attributes': ReadOnlyDict({ 'friendly_name': 'erics273 Gamerscore', + 'state_class': , 'unit_of_measurement': 'points', }), 'context': , @@ -470,7 +482,9 @@ 'aliases': set({ }), 'area_id': None, - 'capabilities': None, + 'capabilities': dict({ + 'state_class': , + }), 'config_entry_id': , 'config_subentry_id': , 'device_class': None, @@ -505,6 +519,7 @@ StateSnapshot({ 'attributes': ReadOnlyDict({ 'friendly_name': 'GSR Ae Follower', + 'state_class': , 'unit_of_measurement': 'people', }), 'context': , @@ -520,7 +535,9 @@ 'aliases': set({ }), 'area_id': None, - 'capabilities': None, + 'capabilities': dict({ + 'state_class': , + }), 'config_entry_id': , 'config_subentry_id': , 'device_class': None, @@ -555,6 +572,7 @@ StateSnapshot({ 'attributes': ReadOnlyDict({ 'friendly_name': 'GSR Ae Following', + 'state_class': , 'unit_of_measurement': 'people', }), 'context': , @@ -570,7 +588,9 @@ 'aliases': set({ }), 'area_id': None, - 'capabilities': None, + 'capabilities': dict({ + 'state_class': , + }), 'config_entry_id': , 'config_subentry_id': , 'device_class': None, @@ -605,6 +625,7 @@ StateSnapshot({ 'attributes': ReadOnlyDict({ 'friendly_name': 'GSR Ae Friends', + 'state_class': , 'unit_of_measurement': 'people', }), 'context': , @@ -620,7 +641,9 @@ 'aliases': set({ }), 'area_id': None, - 'capabilities': None, + 'capabilities': dict({ + 'state_class': , + }), 'config_entry_id': , 'config_subentry_id': , 'device_class': None, @@ -655,6 +678,7 @@ StateSnapshot({ 'attributes': ReadOnlyDict({ 'friendly_name': 'GSR Ae Gamerscore', + 'state_class': , 'unit_of_measurement': 'points', }), 'context': , @@ -937,7 +961,9 @@ 'aliases': set({ }), 'area_id': None, - 'capabilities': None, + 'capabilities': dict({ + 'state_class': , + }), 'config_entry_id': , 'config_subentry_id': , 'device_class': None, @@ -972,6 +998,7 @@ StateSnapshot({ 'attributes': ReadOnlyDict({ 'friendly_name': 'Ikken Hissatsuu Follower', + 'state_class': , 'unit_of_measurement': 'people', }), 'context': , @@ -987,7 +1014,9 @@ 'aliases': set({ }), 'area_id': None, - 'capabilities': None, + 'capabilities': dict({ + 'state_class': , + }), 'config_entry_id': , 'config_subentry_id': , 'device_class': None, @@ -1022,6 +1051,7 @@ StateSnapshot({ 'attributes': ReadOnlyDict({ 'friendly_name': 'Ikken Hissatsuu Following', + 'state_class': , 'unit_of_measurement': 'people', }), 'context': , @@ -1037,7 +1067,9 @@ 'aliases': set({ }), 'area_id': None, - 'capabilities': None, + 'capabilities': dict({ + 'state_class': , + }), 'config_entry_id': , 'config_subentry_id': , 'device_class': None, @@ -1072,6 +1104,7 @@ StateSnapshot({ 'attributes': ReadOnlyDict({ 'friendly_name': 'Ikken Hissatsuu Friends', + 'state_class': , 'unit_of_measurement': 'people', }), 'context': , @@ -1087,7 +1120,9 @@ 'aliases': set({ }), 'area_id': None, - 'capabilities': None, + 'capabilities': dict({ + 'state_class': , + }), 'config_entry_id': , 'config_subentry_id': , 'device_class': None, @@ -1122,6 +1157,7 @@ StateSnapshot({ 'attributes': ReadOnlyDict({ 'friendly_name': 'Ikken Hissatsuu Gamerscore', + 'state_class': , 'unit_of_measurement': 'points', }), 'context': , diff --git a/tests/components/xbox/test_init.py b/tests/components/xbox/test_init.py index ebac15c8e916e..8f493e8c33880 100644 --- a/tests/components/xbox/test_init.py +++ b/tests/components/xbox/test_init.py @@ -1,16 +1,24 @@ """Tests for the Xbox integration.""" from datetime import timedelta -from unittest.mock import AsyncMock, Mock, patch +from http import HTTPStatus +from unittest.mock import AsyncMock, MagicMock, Mock, patch +from aiohttp import ClientError from freezegun.api import FrozenDateTimeFactory -from httpx import ConnectTimeout, HTTPStatusError, ProtocolError +from httpx import ConnectTimeout, HTTPStatusError, ProtocolError, RequestError, Response import pytest from pythonxbox.api.provider.smartglass.models import SmartglassConsoleList +from pythonxbox.common.exceptions import AuthenticationException +import respx -from homeassistant.components.xbox.const import DOMAIN +from homeassistant.components.xbox.const import DOMAIN, OAUTH2_TOKEN from homeassistant.config_entries import ConfigEntryState from homeassistant.core import HomeAssistant +from homeassistant.exceptions import ( + OAuth2TokenRequestReauthError, + OAuth2TokenRequestTransientError, +) from homeassistant.helpers import device_registry as dr from homeassistant.helpers.config_entry_oauth2_flow import ( ImplementationUnavailableError, @@ -82,6 +90,76 @@ async def test_config_implementation_not_available( assert config_entry.state is ConfigEntryState.SETUP_RETRY +@pytest.mark.parametrize( + ("state", "exception"), + [ + ( + ConfigEntryState.SETUP_ERROR, + OAuth2TokenRequestReauthError(domain=DOMAIN, request_info=Mock()), + ), + ( + ConfigEntryState.SETUP_RETRY, + OAuth2TokenRequestTransientError(domain=DOMAIN, request_info=Mock()), + ), + ( + ConfigEntryState.SETUP_RETRY, + ClientError, + ), + ], +) +@respx.mock +async def test_oauth_session_refresh_failure_exceptions( + hass: HomeAssistant, + config_entry: MockConfigEntry, + state: ConfigEntryState, + exception: Exception | type[Exception], + oauth2_session: AsyncMock, +) -> None: + """Test OAuth2 session refresh failures.""" + + oauth2_session.async_ensure_token_valid.side_effect = exception + oauth2_session.valid_token = False + + config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + + assert config_entry.state is state + + +@pytest.mark.parametrize( + ("state", "exception"), + [ + ( + ConfigEntryState.SETUP_RETRY, + HTTPStatusError( + "", request=MagicMock(), response=Response(HTTPStatus.IM_A_TEAPOT) + ), + ), + (ConfigEntryState.SETUP_RETRY, RequestError("", request=Mock())), + (ConfigEntryState.SETUP_ERROR, AuthenticationException), + ], +) +@respx.mock +async def test_oauth_session_refresh_user_and_xsts_token_exceptions( + hass: HomeAssistant, + config_entry: MockConfigEntry, + state: ConfigEntryState, + exception: Exception | type[Exception], + oauth2_session: AsyncMock, +) -> None: + """Test OAuth2 user and XSTS token refresh failures.""" + oauth2_session.valid_token = True + + respx.post(OAUTH2_TOKEN).mock(side_effect=exception) + + config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + + assert config_entry.state is state + + @pytest.mark.parametrize( "exception", [