Minor changes for new pvpc_hourly_pricing integration (#33177)

* Use async_call_later for retrying, instead of async_track_point_in_time
* Normalize test filename renaming it to `test_sensor`
* Remove link to docs in docstring
* Adjust test results to new behavior with async_call_later
This commit is contained in:
Eugenio Panadero 2020-03-23 22:45:04 +01:00 committed by GitHub
parent 5c2bd8b743
commit c2a9aba467
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 24 deletions

View File

@ -1,10 +1,4 @@
""" """Sensor to collect the reference daily prices of electricity ('PVPC') in Spain."""
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
import logging import logging
from random import randint from random import randint
from typing import Optional from typing import Optional
@ -15,10 +9,7 @@ from homeassistant import config_entries
from homeassistant.const import CONF_NAME from homeassistant.const import CONF_NAME
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.event import ( from homeassistant.helpers.event import async_call_later, async_track_time_change
async_track_point_in_time,
async_track_time_change,
)
from homeassistant.helpers.restore_state import RestoreEntity from homeassistant.helpers.restore_state import RestoreEntity
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
@ -144,18 +135,14 @@ class ElecPriceSensor(RestoreEntity):
self._pvpc_data.source_available = False self._pvpc_data.source_available = False
return return
retry_delay = 2 * self._pvpc_data.timeout retry_delay = 2 * self._num_retries * self._pvpc_data.timeout
_LOGGER.debug( _LOGGER.debug(
"%s: Bad update[retry:%d], will try again in %d s", "%s: Bad update[retry:%d], will try again in %d s",
self.entity_id, self.entity_id,
self._num_retries, self._num_retries,
retry_delay, retry_delay,
) )
async_track_point_in_time( async_call_later(self.hass, retry_delay, self.async_update_prices)
self.hass,
self.async_update_prices,
dt_util.now() + timedelta(seconds=retry_delay),
)
return return
if not prices: if not prices:

View File

@ -1,4 +1,4 @@
"""Tests for the pvpc_hourly_pricing component.""" """Tests for the pvpc_hourly_pricing sensor component."""
from datetime import datetime, timedelta from datetime import datetime, timedelta
import logging import logging
from unittest.mock import patch from unittest.mock import patch
@ -27,7 +27,9 @@ async def _process_time_step(
return state 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.""" """Test sensor availability and handling of cloud access."""
hass.config.time_zone = timezone("Europe/Madrid") hass.config.time_zone = timezone("Europe/Madrid")
config = {DOMAIN: [{CONF_NAME: "test_dst", ATTR_TARIFF: "discrimination"}]} 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_warnings == 1
assert num_errors == 0 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 # check that it is silent until it becomes available again
caplog.clear() caplog.clear()
@ -67,19 +69,19 @@ async def test_availability(hass, caplog, pvpc_aioclient_mock: AiohttpClientMock
# silent mode # silent mode
for _ in range(21): for _ in range(21):
await _process_time_step(hass, mock_data, value="unavailable") 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 assert len(caplog.messages) == 0
# warning about data access recovered # warning about data access recovered
await _process_time_step(hass, mock_data, value="unavailable") 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 len(caplog.messages) == 1
assert caplog.records[0].levelno == logging.WARNING assert caplog.records[0].levelno == logging.WARNING
# working ok again # working ok again
await _process_time_step(hass, mock_data, "price_00h", value=0.06821) 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) 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 len(caplog.messages) == 1
assert caplog.records[0].levelno == logging.WARNING assert caplog.records[0].levelno == logging.WARNING