Fix late review comments for Scrape (#81259)

* Review comments from #74325

* Remove moved test

* Fix init

* Handle no data

* Remove print

* Fix tests

* PlatformNotReady if no data

* Recover failed platform setup

* Fix broken test

* patch context

* reset test init

* Move to platform

* asyncio gather

* Remove duplicate code
This commit is contained in:
G Johansson 2022-11-02 17:52:36 +01:00 committed by GitHub
parent 442c5ccc06
commit 93d74cafdc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 113 additions and 73 deletions

View file

@ -1,15 +1,21 @@
"""Test Scrape component setup process."""
from __future__ import annotations
from datetime import datetime
from unittest.mock import patch
import pytest
from homeassistant.components.scrape.const import DOMAIN
from homeassistant.components.scrape.sensor import SCAN_INTERVAL
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from homeassistant.setup import async_setup_component
from . import MockRestData, return_integration_config
from tests.common import async_fire_time_changed
async def test_setup_config(hass: HomeAssistant) -> None:
"""Test setup from yaml."""
@ -35,8 +41,10 @@ async def test_setup_config(hass: HomeAssistant) -> None:
assert len(mock_setup.mock_calls) == 1
async def test_setup_no_data_fails(hass: HomeAssistant) -> None:
"""Test setup entry no data fails."""
async def test_setup_no_data_fails_with_recovery(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None:
"""Test setup entry no data fails and recovers."""
config = {
DOMAIN: [
return_integration_config(
@ -45,15 +53,25 @@ async def test_setup_no_data_fails(hass: HomeAssistant) -> None:
]
}
mocker = MockRestData("test_scrape_sensor_no_data")
with patch(
"homeassistant.components.scrape.coordinator.RestData",
return_value=MockRestData("test_scrape_sensor_no_data"),
"homeassistant.components.rest.RestData",
return_value=mocker,
):
assert not await async_setup_component(hass, DOMAIN, config)
assert await async_setup_component(hass, DOMAIN, config)
await hass.async_block_till_done()
state = hass.states.get("sensor.ha_version")
assert state is None
assert "Platform scrape not ready yet" in caplog.text
mocker.payload = "test_scrape_sensor"
async_fire_time_changed(hass, datetime.utcnow() + SCAN_INTERVAL)
await hass.async_block_till_done()
state = hass.states.get("sensor.ha_version")
assert state is None
assert state.state == "Current Version: 2021.12.10"
async def test_setup_config_no_configuration(hass: HomeAssistant) -> None:
@ -65,3 +83,30 @@ async def test_setup_config_no_configuration(hass: HomeAssistant) -> None:
entities = er.async_get(hass)
assert entities.entities == {}
async def test_setup_config_no_sensors(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None:
"""Test setup from yaml with no configured sensors finalize properly."""
config = {
DOMAIN: [
{
"resource": "https://www.address.com",
"verify_ssl": True,
},
{
"resource": "https://www.address2.com",
"verify_ssl": True,
"sensor": None,
},
]
}
mocker = MockRestData("test_scrape_sensor")
with patch(
"homeassistant.components.rest.RestData",
return_value=mocker,
):
assert await async_setup_component(hass, DOMAIN, config)
await hass.async_block_till_done()