diff --git a/homeassistant/components/pvpc_hourly_pricing/sensor.py b/homeassistant/components/pvpc_hourly_pricing/sensor.py index ff0b01f9ae4..199e20d3e22 100644 --- a/homeassistant/components/pvpc_hourly_pricing/sensor.py +++ b/homeassistant/components/pvpc_hourly_pricing/sensor.py @@ -1,10 +1,4 @@ -""" -Sensor to collect the reference daily prices of electricity ('PVPC') in Spain. - -For more details about this platform, please refer to the documentation at -https://www.home-assistant.io/integrations/pvpc_hourly_pricing/ -""" -from datetime import timedelta +"""Sensor to collect the reference daily prices of electricity ('PVPC') in Spain.""" import logging from random import randint from typing import Optional @@ -15,10 +9,7 @@ from homeassistant import config_entries from homeassistant.const import CONF_NAME from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.aiohttp_client import async_get_clientsession -from homeassistant.helpers.event import ( - async_track_point_in_time, - async_track_time_change, -) +from homeassistant.helpers.event import async_call_later, async_track_time_change from homeassistant.helpers.restore_state import RestoreEntity import homeassistant.util.dt as dt_util @@ -144,18 +135,14 @@ class ElecPriceSensor(RestoreEntity): self._pvpc_data.source_available = False return - retry_delay = 2 * self._pvpc_data.timeout + retry_delay = 2 * self._num_retries * self._pvpc_data.timeout _LOGGER.debug( "%s: Bad update[retry:%d], will try again in %d s", self.entity_id, self._num_retries, retry_delay, ) - async_track_point_in_time( - self.hass, - self.async_update_prices, - dt_util.now() + timedelta(seconds=retry_delay), - ) + async_call_later(self.hass, retry_delay, self.async_update_prices) return if not prices: diff --git a/tests/components/pvpc_hourly_pricing/test_sensor_logic.py b/tests/components/pvpc_hourly_pricing/test_sensor.py similarity index 91% rename from tests/components/pvpc_hourly_pricing/test_sensor_logic.py rename to tests/components/pvpc_hourly_pricing/test_sensor.py index c6ae6fa57c2..fdab7fd1008 100644 --- a/tests/components/pvpc_hourly_pricing/test_sensor_logic.py +++ b/tests/components/pvpc_hourly_pricing/test_sensor.py @@ -1,4 +1,4 @@ -"""Tests for the pvpc_hourly_pricing component.""" +"""Tests for the pvpc_hourly_pricing sensor component.""" from datetime import datetime, timedelta import logging from unittest.mock import patch @@ -27,7 +27,9 @@ async def _process_time_step( return state -async def test_availability(hass, caplog, pvpc_aioclient_mock: AiohttpClientMocker): +async def test_sensor_availability( + hass, caplog, pvpc_aioclient_mock: AiohttpClientMocker +): """Test sensor availability and handling of cloud access.""" hass.config.time_zone = timezone("Europe/Madrid") config = {DOMAIN: [{CONF_NAME: "test_dst", ATTR_TARIFF: "discrimination"}]} @@ -59,7 +61,7 @@ async def test_availability(hass, caplog, pvpc_aioclient_mock: AiohttpClientMock ) assert num_warnings == 1 assert num_errors == 0 - assert pvpc_aioclient_mock.call_count == 7 + assert pvpc_aioclient_mock.call_count == 9 # check that it is silent until it becomes available again caplog.clear() @@ -67,19 +69,19 @@ async def test_availability(hass, caplog, pvpc_aioclient_mock: AiohttpClientMock # silent mode for _ in range(21): await _process_time_step(hass, mock_data, value="unavailable") - assert pvpc_aioclient_mock.call_count == 28 + assert pvpc_aioclient_mock.call_count == 30 assert len(caplog.messages) == 0 # warning about data access recovered await _process_time_step(hass, mock_data, value="unavailable") - assert pvpc_aioclient_mock.call_count == 29 + assert pvpc_aioclient_mock.call_count == 31 assert len(caplog.messages) == 1 assert caplog.records[0].levelno == logging.WARNING # working ok again await _process_time_step(hass, mock_data, "price_00h", value=0.06821) - assert pvpc_aioclient_mock.call_count == 30 + assert pvpc_aioclient_mock.call_count == 32 await _process_time_step(hass, mock_data, "price_01h", value=0.06627) - assert pvpc_aioclient_mock.call_count == 31 + assert pvpc_aioclient_mock.call_count == 33 assert len(caplog.messages) == 1 assert caplog.records[0].levelno == logging.WARNING