Scrape move yaml config to integration key (#74325)
* Scrape take 2 * cleanup * new entity name * Fix name, add tests * Use FlowResultType * Add test abort * issue * 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 * next version * Reconfig * Adjust sensor * cleanup sensor * cleanup init * Fix tests * coverage * Guard against empty sensor * naming * Remove coverage * Review comments * Remove print * Move sensor check
This commit is contained in:
parent
06773efcbd
commit
662aee17a6
8 changed files with 498 additions and 114 deletions
|
@ -7,6 +7,7 @@ from unittest.mock import patch
|
|||
from homeassistant.components.scrape.sensor import SCAN_INTERVAL
|
||||
from homeassistant.components.sensor import (
|
||||
CONF_STATE_CLASS,
|
||||
DOMAIN as SENSOR_DOMAIN,
|
||||
SensorDeviceClass,
|
||||
SensorStateClass,
|
||||
)
|
||||
|
@ -21,7 +22,7 @@ from homeassistant.core import HomeAssistant
|
|||
from homeassistant.helpers import entity_registry as er
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from . import MockRestData, return_config
|
||||
from . import MockRestData, return_config, return_integration_config
|
||||
|
||||
from tests.common import async_fire_time_changed
|
||||
|
||||
|
@ -30,102 +31,30 @@ DOMAIN = "scrape"
|
|||
|
||||
async def test_scrape_sensor(hass: HomeAssistant) -> None:
|
||||
"""Test Scrape sensor minimal."""
|
||||
config = {"sensor": return_config(select=".current-version h1", name="HA version")}
|
||||
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,
|
||||
):
|
||||
assert await async_setup_component(hass, "sensor", config)
|
||||
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"
|
||||
|
||||
|
||||
async def test_scrape_sensor_value_template(hass: HomeAssistant) -> None:
|
||||
"""Test Scrape sensor with value template."""
|
||||
async def test_scrape_sensor_platform_yaml(hass: HomeAssistant) -> None:
|
||||
"""Test Scrape sensor load from sensor platform."""
|
||||
config = {
|
||||
"sensor": return_config(
|
||||
select=".current-version h1",
|
||||
name="HA version",
|
||||
template="{{ value.split(':')[1] }}",
|
||||
)
|
||||
}
|
||||
|
||||
mocker = MockRestData("test_scrape_sensor")
|
||||
with patch(
|
||||
"homeassistant.components.rest.RestData",
|
||||
return_value=mocker,
|
||||
):
|
||||
assert await async_setup_component(hass, "sensor", config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("sensor.ha_version")
|
||||
assert state.state == "2021.12.10"
|
||||
|
||||
|
||||
async def test_scrape_uom_and_classes(hass: HomeAssistant) -> None:
|
||||
"""Test Scrape sensor for unit of measurement, device class and state class."""
|
||||
config = {
|
||||
"sensor": return_config(
|
||||
select=".current-temp h3",
|
||||
name="Current Temp",
|
||||
template="{{ value.split(':')[1] }}",
|
||||
uom="°C",
|
||||
device_class="temperature",
|
||||
state_class="measurement",
|
||||
)
|
||||
}
|
||||
|
||||
mocker = MockRestData("test_scrape_uom_and_classes")
|
||||
with patch(
|
||||
"homeassistant.components.rest.RestData",
|
||||
return_value=mocker,
|
||||
):
|
||||
assert await async_setup_component(hass, "sensor", config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("sensor.current_temp")
|
||||
assert state.state == "22.1"
|
||||
assert state.attributes[CONF_UNIT_OF_MEASUREMENT] == TEMP_CELSIUS
|
||||
assert state.attributes[CONF_DEVICE_CLASS] == SensorDeviceClass.TEMPERATURE
|
||||
assert state.attributes[CONF_STATE_CLASS] == SensorStateClass.MEASUREMENT
|
||||
|
||||
|
||||
async def test_scrape_unique_id(hass: HomeAssistant) -> None:
|
||||
"""Test Scrape sensor for unique id."""
|
||||
config = {
|
||||
"sensor": return_config(
|
||||
select=".current-temp h3",
|
||||
name="Current Temp",
|
||||
template="{{ value.split(':')[1] }}",
|
||||
unique_id="very_unique_id",
|
||||
)
|
||||
}
|
||||
|
||||
mocker = MockRestData("test_scrape_uom_and_classes")
|
||||
with patch(
|
||||
"homeassistant.components.rest.RestData",
|
||||
return_value=mocker,
|
||||
):
|
||||
assert await async_setup_component(hass, "sensor", config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("sensor.current_temp")
|
||||
assert state.state == "22.1"
|
||||
|
||||
registry = er.async_get(hass)
|
||||
entry = registry.async_get("sensor.current_temp")
|
||||
assert entry
|
||||
assert entry.unique_id == "very_unique_id"
|
||||
|
||||
|
||||
async def test_scrape_sensor_authentication(hass: HomeAssistant) -> None:
|
||||
"""Test Scrape sensor with authentication."""
|
||||
config = {
|
||||
"sensor": [
|
||||
SENSOR_DOMAIN: [
|
||||
return_config(
|
||||
select=".return",
|
||||
name="Auth page",
|
||||
|
@ -147,7 +76,170 @@ async def test_scrape_sensor_authentication(hass: HomeAssistant) -> None:
|
|||
"homeassistant.components.rest.RestData",
|
||||
return_value=mocker,
|
||||
):
|
||||
assert await async_setup_component(hass, "sensor", config)
|
||||
assert await async_setup_component(hass, SENSOR_DOMAIN, config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("sensor.auth_page")
|
||||
assert state.state == "secret text"
|
||||
state2 = hass.states.get("sensor.auth_page2")
|
||||
assert state2.state == "secret text"
|
||||
|
||||
|
||||
async def test_scrape_sensor_platform_yaml_no_data(hass: HomeAssistant, caplog) -> None:
|
||||
"""Test Scrape sensor load from sensor platform fetching no data."""
|
||||
config = {
|
||||
SENSOR_DOMAIN: [
|
||||
return_config(
|
||||
select=".return",
|
||||
name="Auth page",
|
||||
username="user@secret.com",
|
||||
password="12345678",
|
||||
authentication="digest",
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
mocker = MockRestData("test_scrape_sensor_no_data")
|
||||
with patch(
|
||||
"homeassistant.components.rest.RestData",
|
||||
return_value=mocker,
|
||||
):
|
||||
assert await async_setup_component(hass, SENSOR_DOMAIN, config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("sensor.auth_page")
|
||||
assert not state
|
||||
assert "Platform scrape not ready yet: None; Retrying in background" in caplog.text
|
||||
|
||||
|
||||
async def test_scrape_sensor_value_template(hass: HomeAssistant) -> None:
|
||||
"""Test Scrape sensor with value template."""
|
||||
config = {
|
||||
DOMAIN: [
|
||||
return_integration_config(
|
||||
sensors=[
|
||||
{
|
||||
"select": ".current-version h1",
|
||||
"name": "HA version",
|
||||
"value_template": "{{ value.split(':')[1] }}",
|
||||
}
|
||||
]
|
||||
)
|
||||
]
|
||||
}
|
||||
|
||||
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()
|
||||
|
||||
state = hass.states.get("sensor.ha_version")
|
||||
assert state.state == "2021.12.10"
|
||||
|
||||
|
||||
async def test_scrape_uom_and_classes(hass: HomeAssistant) -> None:
|
||||
"""Test Scrape sensor for unit of measurement, device class and state class."""
|
||||
config = {
|
||||
DOMAIN: [
|
||||
return_integration_config(
|
||||
sensors=[
|
||||
{
|
||||
"select": ".current-temp h3",
|
||||
"name": "Current Temp",
|
||||
"value_template": "{{ value.split(':')[1] }}",
|
||||
"unit_of_measurement": "°C",
|
||||
"device_class": "temperature",
|
||||
"state_class": "measurement",
|
||||
}
|
||||
]
|
||||
)
|
||||
]
|
||||
}
|
||||
|
||||
mocker = MockRestData("test_scrape_uom_and_classes")
|
||||
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.current_temp")
|
||||
assert state.state == "22.1"
|
||||
assert state.attributes[CONF_UNIT_OF_MEASUREMENT] == TEMP_CELSIUS
|
||||
assert state.attributes[CONF_DEVICE_CLASS] == SensorDeviceClass.TEMPERATURE
|
||||
assert state.attributes[CONF_STATE_CLASS] == SensorStateClass.MEASUREMENT
|
||||
|
||||
|
||||
async def test_scrape_unique_id(hass: HomeAssistant) -> None:
|
||||
"""Test Scrape sensor for unique id."""
|
||||
config = {
|
||||
DOMAIN: return_integration_config(
|
||||
sensors=[
|
||||
{
|
||||
"select": ".current-temp h3",
|
||||
"name": "Current Temp",
|
||||
"value_template": "{{ value.split(':')[1] }}",
|
||||
"unique_id": "very_unique_id",
|
||||
}
|
||||
]
|
||||
)
|
||||
}
|
||||
|
||||
mocker = MockRestData("test_scrape_uom_and_classes")
|
||||
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.current_temp")
|
||||
assert state.state == "22.1"
|
||||
|
||||
registry = er.async_get(hass)
|
||||
entry = registry.async_get("sensor.current_temp")
|
||||
assert entry
|
||||
assert entry.unique_id == "very_unique_id"
|
||||
|
||||
|
||||
async def test_scrape_sensor_authentication(hass: HomeAssistant) -> None:
|
||||
"""Test Scrape sensor with authentication."""
|
||||
config = {
|
||||
DOMAIN: [
|
||||
return_integration_config(
|
||||
authentication="digest",
|
||||
username="user@secret.com",
|
||||
password="12345678",
|
||||
sensors=[
|
||||
{
|
||||
"select": ".return",
|
||||
"name": "Auth page",
|
||||
},
|
||||
],
|
||||
),
|
||||
return_integration_config(
|
||||
username="user@secret.com",
|
||||
password="12345678",
|
||||
sensors=[
|
||||
{
|
||||
"select": ".return",
|
||||
"name": "Auth page2",
|
||||
},
|
||||
],
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
mocker = MockRestData("test_scrape_sensor_authentication")
|
||||
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.auth_page")
|
||||
|
@ -158,14 +250,18 @@ async def test_scrape_sensor_authentication(hass: HomeAssistant) -> None:
|
|||
|
||||
async def test_scrape_sensor_no_data(hass: HomeAssistant) -> None:
|
||||
"""Test Scrape sensor fails on no data."""
|
||||
config = {"sensor": return_config(select=".current-version h1", name="HA version")}
|
||||
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, "sensor", config)
|
||||
assert not await async_setup_component(hass, DOMAIN, config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("sensor.ha_version")
|
||||
|
@ -174,14 +270,20 @@ async def test_scrape_sensor_no_data(hass: HomeAssistant) -> None:
|
|||
|
||||
async def test_scrape_sensor_no_data_refresh(hass: HomeAssistant) -> None:
|
||||
"""Test Scrape sensor no data on refresh."""
|
||||
config = {"sensor": return_config(select=".current-version h1", name="HA version")}
|
||||
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,
|
||||
):
|
||||
assert await async_setup_component(hass, "sensor", config)
|
||||
assert await async_setup_component(hass, DOMAIN, config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("sensor.ha_version")
|
||||
|
@ -200,9 +302,18 @@ async def test_scrape_sensor_no_data_refresh(hass: HomeAssistant) -> None:
|
|||
async def test_scrape_sensor_attribute_and_tag(hass: HomeAssistant) -> None:
|
||||
"""Test Scrape sensor with attribute and tag."""
|
||||
config = {
|
||||
"sensor": [
|
||||
return_config(select="div", name="HA class", index=1, attribute="class"),
|
||||
return_config(select="template", name="HA template"),
|
||||
DOMAIN: [
|
||||
return_integration_config(
|
||||
sensors=[
|
||||
{
|
||||
"index": 1,
|
||||
"select": "div",
|
||||
"name": "HA class",
|
||||
"attribute": "class",
|
||||
},
|
||||
{"select": "template", "name": "HA template"},
|
||||
],
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
|
@ -211,7 +322,7 @@ async def test_scrape_sensor_attribute_and_tag(hass: HomeAssistant) -> None:
|
|||
"homeassistant.components.rest.RestData",
|
||||
return_value=mocker,
|
||||
):
|
||||
assert await async_setup_component(hass, "sensor", config)
|
||||
assert await async_setup_component(hass, DOMAIN, config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("sensor.ha_class")
|
||||
|
@ -223,9 +334,22 @@ async def test_scrape_sensor_attribute_and_tag(hass: HomeAssistant) -> None:
|
|||
async def test_scrape_sensor_errors(hass: HomeAssistant) -> None:
|
||||
"""Test Scrape sensor handle errors."""
|
||||
config = {
|
||||
"sensor": [
|
||||
return_config(select="div", name="HA class", index=5, attribute="class"),
|
||||
return_config(select="div", name="HA class2", attribute="classes"),
|
||||
DOMAIN: [
|
||||
return_integration_config(
|
||||
sensors=[
|
||||
{
|
||||
"index": 5,
|
||||
"select": "div",
|
||||
"name": "HA class",
|
||||
"attribute": "class",
|
||||
},
|
||||
{
|
||||
"select": "div",
|
||||
"name": "HA class2",
|
||||
"attribute": "classes",
|
||||
},
|
||||
],
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
|
@ -234,10 +358,58 @@ async def test_scrape_sensor_errors(hass: HomeAssistant) -> None:
|
|||
"homeassistant.components.rest.RestData",
|
||||
return_value=mocker,
|
||||
):
|
||||
assert await async_setup_component(hass, "sensor", config)
|
||||
assert await async_setup_component(hass, DOMAIN, config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("sensor.ha_class")
|
||||
assert state.state == STATE_UNKNOWN
|
||||
state2 = hass.states.get("sensor.ha_class2")
|
||||
assert state2.state == STATE_UNKNOWN
|
||||
|
||||
|
||||
async def test_scrape_sensor_unique_id(hass: HomeAssistant) -> None:
|
||||
"""Test Scrape sensor with unique_id."""
|
||||
config = {
|
||||
DOMAIN: [
|
||||
return_integration_config(
|
||||
sensors=[
|
||||
{
|
||||
"select": ".current-version h1",
|
||||
"name": "HA version",
|
||||
"unique_id": "ha_version_unique_id",
|
||||
}
|
||||
]
|
||||
)
|
||||
]
|
||||
}
|
||||
|
||||
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()
|
||||
|
||||
state = hass.states.get("sensor.ha_version")
|
||||
assert state.state == "Current Version: 2021.12.10"
|
||||
|
||||
entity_reg = er.async_get(hass)
|
||||
entity = entity_reg.async_get("sensor.ha_version")
|
||||
|
||||
assert entity.unique_id == "ha_version_unique_id"
|
||||
|
||||
|
||||
async def test_scrape_sensor_not_configured_sensor(hass: HomeAssistant, caplog) -> None:
|
||||
"""Test Scrape sensor with missing configured sensors."""
|
||||
config = {DOMAIN: [return_integration_config(sensors=None)]}
|
||||
|
||||
mocker = MockRestData("test_scrape_sensor")
|
||||
with patch(
|
||||
"homeassistant.components.rest.RestData",
|
||||
return_value=mocker,
|
||||
):
|
||||
assert not await async_setup_component(hass, DOMAIN, config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert "No sensors configured" in caplog.text
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue