diff --git a/homeassistant/components/person/__init__.py b/homeassistant/components/person/__init__.py index 2f850b2cb84..12ee0da3b82 100644 --- a/homeassistant/components/person/__init__.py +++ b/homeassistant/components/person/__init__.py @@ -7,7 +7,7 @@ from typing import cast import voluptuous as vol from homeassistant.auth import EVENT_USER_REMOVED -from homeassistant.components import websocket_api +from homeassistant.components import persistent_notification, websocket_api from homeassistant.components.device_tracker import ( ATTR_SOURCE_TYPE, DOMAIN as DEVICE_TRACKER_DOMAIN, @@ -278,7 +278,8 @@ async def filter_yaml_data(hass: HomeAssistant, persons: list[dict]) -> list[dic filtered.append(person_conf) if person_invalid_user: - hass.components.persistent_notification.async_create( + persistent_notification.async_create( + hass, f""" The following persons point at invalid users: diff --git a/homeassistant/components/powerwall/__init__.py b/homeassistant/components/powerwall/__init__.py index c86fa71f987..5fb974fa5c7 100644 --- a/homeassistant/components/powerwall/__init__.py +++ b/homeassistant/components/powerwall/__init__.py @@ -10,6 +10,7 @@ from tesla_powerwall import ( PowerwallUnreachableError, ) +from homeassistant.components import persistent_notification from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_IP_ADDRESS, CONF_PASSWORD, Platform from homeassistant.core import HomeAssistant, callback @@ -78,7 +79,8 @@ async def _async_handle_api_changed_error( ): # The error might include some important information about what exactly changed. _LOGGER.error(str(error)) - hass.components.persistent_notification.async_create( + persistent_notification.async_create( + hass, "It seems like your powerwall uses an unsupported version. " "Please update the software of your powerwall or if it is " "already the newest consider reporting this issue.\nSee logs for more information", diff --git a/homeassistant/components/profiler/__init__.py b/homeassistant/components/profiler/__init__.py index a8f5a2d8d1d..99d074d95fe 100644 --- a/homeassistant/components/profiler/__init__.py +++ b/homeassistant/components/profiler/__init__.py @@ -14,6 +14,7 @@ import objgraph from pyprof2calltree import convert import voluptuous as vol +from homeassistant.components import persistent_notification from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_SCAN_INTERVAL, CONF_TYPE from homeassistant.core import HomeAssistant, ServiceCall @@ -68,7 +69,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: if LOG_INTERVAL_SUB in domain_data: domain_data[LOG_INTERVAL_SUB]() - hass.components.persistent_notification.async_create( + persistent_notification.async_create( + hass, "Object growth logging has started. See [the logs](/config/logs) to track the growth of new objects.", title="Object growth logging started", notification_id="profile_object_logging", @@ -82,7 +84,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: if LOG_INTERVAL_SUB not in domain_data: return - hass.components.persistent_notification.async_dismiss("profile_object_logging") + persistent_notification.async_dismiss(hass, "profile_object_logging") domain_data.pop(LOG_INTERVAL_SUB)() def _dump_log_objects(call: ServiceCall) -> None: @@ -94,7 +96,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: objgraph.by_type(obj_type), ) - hass.components.persistent_notification.create( + persistent_notification.create( + hass, f"Objects with type {obj_type} have been dumped to the log. See [the logs](/config/logs) to review the repr of the objects.", title="Object dump completed", notification_id="profile_object_dump", @@ -206,7 +209,8 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def _async_generate_profile(hass: HomeAssistant, call: ServiceCall): start_time = int(time.time() * 1000000) - hass.components.persistent_notification.async_create( + persistent_notification.async_create( + hass, "The profile has started. This notification will be updated when it is complete.", title="Profile Started", notification_id=f"profiler_{start_time}", @@ -221,7 +225,8 @@ async def _async_generate_profile(hass: HomeAssistant, call: ServiceCall): await hass.async_add_executor_job( _write_profile, profiler, cprofile_path, callgrind_path ) - hass.components.persistent_notification.async_create( + persistent_notification.async_create( + hass, f"Wrote cProfile data to {cprofile_path} and callgrind data to {callgrind_path}", title="Profile Complete", notification_id=f"profiler_{start_time}", @@ -230,7 +235,8 @@ async def _async_generate_profile(hass: HomeAssistant, call: ServiceCall): async def _async_generate_memory_profile(hass: HomeAssistant, call: ServiceCall): start_time = int(time.time() * 1000000) - hass.components.persistent_notification.async_create( + persistent_notification.async_create( + hass, "The memory profile has started. This notification will be updated when it is complete.", title="Profile Started", notification_id=f"memory_profiler_{start_time}", @@ -242,7 +248,8 @@ async def _async_generate_memory_profile(hass: HomeAssistant, call: ServiceCall) heap_path = hass.config.path(f"heap_profile.{start_time}.hpy") await hass.async_add_executor_job(_write_memory_profile, heap, heap_path) - hass.components.persistent_notification.async_create( + persistent_notification.async_create( + hass, f"Wrote heapy memory profile to {heap_path}", title="Profile Complete", notification_id=f"memory_profiler_{start_time}", diff --git a/homeassistant/components/ps4/__init__.py b/homeassistant/components/ps4/__init__.py index 4c1bb0a0cd7..7498a74f8d2 100644 --- a/homeassistant/components/ps4/__init__.py +++ b/homeassistant/components/ps4/__init__.py @@ -6,6 +6,7 @@ from pyps4_2ndscreen.ddp import async_create_ddp_endpoint from pyps4_2ndscreen.media_art import COUNTRIES import voluptuous as vol +from homeassistant.components import persistent_notification from homeassistant.components.media_player.const import ( ATTR_MEDIA_CONTENT_TYPE, ATTR_MEDIA_TITLE, @@ -151,7 +152,8 @@ async def async_migrate_entry(hass, entry): Please remove the PS4 Integration and re-configure [here](/config/integrations).""" - hass.components.persistent_notification.async_create( + persistent_notification.async_create( + hass, title="PlayStation 4 Integration Configuration Requires Update", message=msg, notification_id="config_entry_migration", diff --git a/homeassistant/components/raincloud/__init__.py b/homeassistant/components/raincloud/__init__.py index 75952ebe7eb..33fd3cf1520 100644 --- a/homeassistant/components/raincloud/__init__.py +++ b/homeassistant/components/raincloud/__init__.py @@ -6,6 +6,7 @@ from raincloudy.core import RainCloudy from requests.exceptions import ConnectTimeout, HTTPError import voluptuous as vol +from homeassistant.components import persistent_notification from homeassistant.const import ( ATTR_ATTRIBUTION, CONF_PASSWORD, @@ -108,7 +109,8 @@ def setup(hass: HomeAssistant, config: ConfigType) -> bool: hass.data[DATA_RAINCLOUD] = RainCloudHub(raincloud) except (ConnectTimeout, HTTPError) as ex: _LOGGER.error("Unable to connect to Rain Cloud service: %s", str(ex)) - hass.components.persistent_notification.create( + persistent_notification.create( + hass, f"Error: {ex}
" "You will need to restart hass after fixing.", title=NOTIFICATION_TITLE, notification_id=NOTIFICATION_ID, diff --git a/homeassistant/components/seventeentrack/sensor.py b/homeassistant/components/seventeentrack/sensor.py index 2ece86e513e..688f4e7e2df 100644 --- a/homeassistant/components/seventeentrack/sensor.py +++ b/homeassistant/components/seventeentrack/sensor.py @@ -8,6 +8,7 @@ from py17track import Client as SeventeenTrackClient from py17track.errors import SeventeenTrackError import voluptuous as vol +from homeassistant.components import persistent_notification from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity from homeassistant.const import ( ATTR_ATTRIBUTION, @@ -254,8 +255,8 @@ class SeventeenTrackPackageSensor(SensorEntity): title = NOTIFICATION_DELIVERED_TITLE.format(identification) notification_id = NOTIFICATION_DELIVERED_TITLE.format(self._tracking_number) - self.hass.components.persistent_notification.create( - message, title=title, notification_id=notification_id + persistent_notification.create( + self.hass, message, title=title, notification_id=notification_id ) diff --git a/homeassistant/components/skybell/__init__.py b/homeassistant/components/skybell/__init__.py index 129397085f2..47e22f5b619 100644 --- a/homeassistant/components/skybell/__init__.py +++ b/homeassistant/components/skybell/__init__.py @@ -5,6 +5,7 @@ from requests.exceptions import ConnectTimeout, HTTPError from skybellpy import Skybell import voluptuous as vol +from homeassistant.components import persistent_notification from homeassistant.const import ( ATTR_ATTRIBUTION, CONF_PASSWORD, @@ -61,7 +62,8 @@ def setup(hass: HomeAssistant, config: ConfigType) -> bool: hass.data[DOMAIN] = skybell except (ConnectTimeout, HTTPError) as ex: _LOGGER.error("Unable to connect to Skybell service: %s", str(ex)) - hass.components.persistent_notification.create( + persistent_notification.create( + hass, "Error: {}
" "You will need to restart hass after fixing." "".format(ex), diff --git a/homeassistant/components/travisci/sensor.py b/homeassistant/components/travisci/sensor.py index f257f8acae9..7b15f5c0dc8 100644 --- a/homeassistant/components/travisci/sensor.py +++ b/homeassistant/components/travisci/sensor.py @@ -8,6 +8,7 @@ from travispy import TravisPy from travispy.errors import TravisError import voluptuous as vol +from homeassistant.components import persistent_notification from homeassistant.components.sensor import ( PLATFORM_SCHEMA, SensorEntity, @@ -106,7 +107,8 @@ def setup_platform( except TravisError as ex: _LOGGER.error("Unable to connect to Travis CI service: %s", str(ex)) - hass.components.persistent_notification.create( + persistent_notification.create( + hass, "Error: {}
" "You will need to restart hass after fixing." "".format(ex), diff --git a/tests/components/persistent_notification/test_init.py b/tests/components/persistent_notification/test_init.py index bc552b71770..a26e243df11 100644 --- a/tests/components/persistent_notification/test_init.py +++ b/tests/components/persistent_notification/test_init.py @@ -158,9 +158,7 @@ async def test_ws_get_notifications(hass, hass_ws_client): assert len(notifications) == 0 # Create - hass.components.persistent_notification.async_create( - "test", notification_id="Beer 2" - ) + pn.async_create(hass, "test", notification_id="Beer 2") await client.send_json({"id": 6, "type": "persistent_notification/get"}) msg = await client.receive_json() assert msg["id"] == 6 @@ -186,7 +184,7 @@ async def test_ws_get_notifications(hass, hass_ws_client): assert notifications[0]["status"] == pn.STATUS_READ # Dismiss - hass.components.persistent_notification.async_dismiss("Beer 2") + pn.async_dismiss(hass, "Beer 2") await client.send_json({"id": 8, "type": "persistent_notification/get"}) msg = await client.receive_json() notifications = msg["result"] diff --git a/tests/components/seventeentrack/test_sensor.py b/tests/components/seventeentrack/test_sensor.py index 4e22822150b..d8d75f827f7 100644 --- a/tests/components/seventeentrack/test_sensor.py +++ b/tests/components/seventeentrack/test_sensor.py @@ -2,7 +2,7 @@ from __future__ import annotations import datetime -from unittest.mock import MagicMock, patch +from unittest.mock import patch from py17track.package import Package import pytest @@ -301,12 +301,14 @@ async def test_delivered_not_shown(hass): ) ProfileMock.package_list = [package] - hass.components.persistent_notification = MagicMock() - await _setup_seventeentrack(hass, VALID_CONFIG_FULL_NO_DELIVERED) - await _goto_future(hass) + with patch( + "homeassistant.components.seventeentrack.sensor.persistent_notification" + ) as persistent_notification_mock: + await _setup_seventeentrack(hass, VALID_CONFIG_FULL_NO_DELIVERED) + await _goto_future(hass) - assert not hass.states.async_entity_ids() - hass.components.persistent_notification.create.assert_called() + assert not hass.states.async_entity_ids() + persistent_notification_mock.create.assert_called() async def test_delivered_shown(hass): @@ -324,12 +326,14 @@ async def test_delivered_shown(hass): ) ProfileMock.package_list = [package] - hass.components.persistent_notification = MagicMock() - await _setup_seventeentrack(hass, VALID_CONFIG_FULL) + with patch( + "homeassistant.components.seventeentrack.sensor.persistent_notification" + ) as persistent_notification_mock: + await _setup_seventeentrack(hass, VALID_CONFIG_FULL) - assert hass.states.get("sensor.seventeentrack_package_456") is not None - assert len(hass.states.async_entity_ids()) == 1 - hass.components.persistent_notification.create.assert_not_called() + assert hass.states.get("sensor.seventeentrack_package_456") is not None + assert len(hass.states.async_entity_ids()) == 1 + persistent_notification_mock.create.assert_not_called() async def test_becomes_delivered_not_shown_notification(hass): @@ -364,11 +368,13 @@ async def test_becomes_delivered_not_shown_notification(hass): ) ProfileMock.package_list = [package_delivered] - hass.components.persistent_notification = MagicMock() - await _goto_future(hass) + with patch( + "homeassistant.components.seventeentrack.sensor.persistent_notification" + ) as persistent_notification_mock: + await _goto_future(hass) - hass.components.persistent_notification.create.assert_called() - assert not hass.states.async_entity_ids() + persistent_notification_mock.create.assert_called() + assert not hass.states.async_entity_ids() async def test_summary_correctly_updated(hass):