hass-core/tests/components/scrape/test_init.py
G Johansson b3dd59f202
Add config flow to Scrape (#81193)
* Scrape take 2

* cleanup

* new entity name

* Fix name, add tests

* Use FlowResultType

* Add test abort

* hassfest

* Remove not needed test

* clean

* Remove config entry and implement datacoordinator

* fix codeowners

* fix codeowners

* codeowners reset

* Fix coordinator

* Remove test config_flow

* Fix tests

* hassfest

* reset config flow

* reset strings

* reset sensor

* Reconfig

* Fix tests

* coverage

* Remove coverage

* Remove print

* Add config flow

* Fix config flow

* Add back init

* Add entry to sensor

* float to int

* Fix SelectSelector

* Add tests for config entry to existing

* Test config flow

* Fix test reload

* Fix rebase

* Fix strings

* clean init

* Clean test_sensor

* Align sensor setup entry

* Add error to strings

* review changes

* clean tests

* Add back options flow

* review changes

* update_listener

* Add tests

* Remove duplicate abort

* strings

* sensors to sensor

* review changes

* more review changes

* clarify test payload

* fixture name
2022-11-21 21:39:39 +01:00

127 lines
3.9 KiB
Python

"""Test Scrape component setup process."""
from __future__ import annotations
from datetime import datetime
from unittest.mock import patch
import pytest
from homeassistant import config_entries
from homeassistant.components.scrape.const import DEFAULT_SCAN_INTERVAL, DOMAIN
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 MockConfigEntry, async_fire_time_changed
async def test_setup_config(hass: HomeAssistant) -> None:
"""Test setup from yaml."""
config = {
DOMAIN: [
return_integration_config(
sensors=[{"select": ".current-version h1", "name": "HA version"}]
)
]
}
mocker = MockRestData("test_scrape_sensor")
with patch(
"homeassistant.components.rest.RestData",
return_value=mocker,
) as mock_setup:
assert await async_setup_component(hass, DOMAIN, config)
await hass.async_block_till_done()
state = hass.states.get("sensor.ha_version")
assert state.state == "Current Version: 2021.12.10"
assert len(mock_setup.mock_calls) == 1
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(
sensors=[{"select": ".current-version h1", "name": "HA version"}]
),
]
}
mocker = MockRestData("test_scrape_sensor_no_data")
with patch(
"homeassistant.components.rest.RestData",
return_value=mocker,
):
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() + DEFAULT_SCAN_INTERVAL)
await hass.async_block_till_done()
state = hass.states.get("sensor.ha_version")
assert state.state == "Current Version: 2021.12.10"
async def test_setup_config_no_configuration(hass: HomeAssistant) -> None:
"""Test setup from yaml missing configuration options."""
config = {DOMAIN: None}
assert await async_setup_component(hass, DOMAIN, config)
await hass.async_block_till_done()
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()
async def test_setup_entry(hass: HomeAssistant, loaded_entry: MockConfigEntry) -> None:
"""Test setup entry."""
assert loaded_entry.state == config_entries.ConfigEntryState.LOADED
async def test_unload_entry(hass: HomeAssistant, loaded_entry: MockConfigEntry) -> None:
"""Test unload an entry."""
assert loaded_entry.state == config_entries.ConfigEntryState.LOADED
assert await hass.config_entries.async_unload(loaded_entry.entry_id)
await hass.async_block_till_done()
assert loaded_entry.state is config_entries.ConfigEntryState.NOT_LOADED