Align Scrape resource model with Rest integration (#81074)
This commit is contained in:
parent
dbd875cd25
commit
3d42a46871
3 changed files with 19 additions and 33 deletions
|
@ -41,7 +41,7 @@ from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
|||
|
||||
from .const import COORDINATOR, DOMAIN, PLATFORM_IDX, REST, REST_DATA, REST_IDX
|
||||
from .data import RestData
|
||||
from .schema import CONFIG_SCHEMA # noqa: F401
|
||||
from .schema import CONFIG_SCHEMA, RESOURCE_SCHEMA # noqa: F401
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
|
|
@ -5,10 +5,9 @@ from datetime import timedelta
|
|||
import logging
|
||||
from typing import Any
|
||||
|
||||
import httpx
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.rest.data import RestData
|
||||
from homeassistant.components.rest import RESOURCE_SCHEMA, create_rest_data_from_config
|
||||
from homeassistant.components.sensor import (
|
||||
CONF_STATE_CLASS,
|
||||
DEVICE_CLASSES_SCHEMA,
|
||||
|
@ -82,12 +81,15 @@ async def async_setup_platform(
|
|||
discovery_info: DiscoveryInfoType | None = None,
|
||||
) -> None:
|
||||
"""Set up the Web scrape sensor."""
|
||||
resource_config = vol.Schema(RESOURCE_SCHEMA, extra=vol.REMOVE_EXTRA)(config)
|
||||
rest = create_rest_data_from_config(hass, resource_config)
|
||||
|
||||
coordinator = ScrapeCoordinator(hass, rest, SCAN_INTERVAL)
|
||||
await coordinator.async_refresh()
|
||||
if coordinator.data is None:
|
||||
raise PlatformNotReady
|
||||
|
||||
name: str = config[CONF_NAME]
|
||||
resource: str = config[CONF_RESOURCE]
|
||||
method: str = "GET"
|
||||
payload: str | None = None
|
||||
headers: dict[str, str] | None = config.get(CONF_HEADERS)
|
||||
verify_ssl: bool = config[CONF_VERIFY_SSL]
|
||||
select: str | None = config.get(CONF_SELECT)
|
||||
attr: str | None = config.get(CONF_ATTR)
|
||||
index: int = config[CONF_INDEX]
|
||||
|
@ -95,27 +97,11 @@ async def async_setup_platform(
|
|||
device_class: str | None = config.get(CONF_DEVICE_CLASS)
|
||||
state_class: str | None = config.get(CONF_STATE_CLASS)
|
||||
unique_id: str | None = config.get(CONF_UNIQUE_ID)
|
||||
username: str | None = config.get(CONF_USERNAME)
|
||||
password: str | None = config.get(CONF_PASSWORD)
|
||||
value_template: Template | None = config.get(CONF_VALUE_TEMPLATE)
|
||||
|
||||
if value_template is not None:
|
||||
value_template.hass = hass
|
||||
|
||||
auth: httpx.DigestAuth | tuple[str, str] | None = None
|
||||
if username and password:
|
||||
if config.get(CONF_AUTHENTICATION) == HTTP_DIGEST_AUTHENTICATION:
|
||||
auth = httpx.DigestAuth(username, password)
|
||||
else:
|
||||
auth = (username, password)
|
||||
|
||||
rest = RestData(hass, method, resource, auth, headers, None, payload, verify_ssl)
|
||||
|
||||
coordinator = ScrapeCoordinator(hass, rest, SCAN_INTERVAL)
|
||||
await coordinator.async_refresh()
|
||||
if coordinator.data is None:
|
||||
raise PlatformNotReady
|
||||
|
||||
async_add_entities(
|
||||
[
|
||||
ScrapeSensor(
|
||||
|
|
|
@ -34,7 +34,7 @@ async def test_scrape_sensor(hass: HomeAssistant) -> None:
|
|||
|
||||
mocker = MockRestData("test_scrape_sensor")
|
||||
with patch(
|
||||
"homeassistant.components.scrape.sensor.RestData",
|
||||
"homeassistant.components.rest.RestData",
|
||||
return_value=mocker,
|
||||
):
|
||||
assert await async_setup_component(hass, "sensor", config)
|
||||
|
@ -56,7 +56,7 @@ async def test_scrape_sensor_value_template(hass: HomeAssistant) -> None:
|
|||
|
||||
mocker = MockRestData("test_scrape_sensor")
|
||||
with patch(
|
||||
"homeassistant.components.scrape.sensor.RestData",
|
||||
"homeassistant.components.rest.RestData",
|
||||
return_value=mocker,
|
||||
):
|
||||
assert await async_setup_component(hass, "sensor", config)
|
||||
|
@ -81,7 +81,7 @@ async def test_scrape_uom_and_classes(hass: HomeAssistant) -> None:
|
|||
|
||||
mocker = MockRestData("test_scrape_uom_and_classes")
|
||||
with patch(
|
||||
"homeassistant.components.scrape.sensor.RestData",
|
||||
"homeassistant.components.rest.RestData",
|
||||
return_value=mocker,
|
||||
):
|
||||
assert await async_setup_component(hass, "sensor", config)
|
||||
|
@ -107,7 +107,7 @@ async def test_scrape_unique_id(hass: HomeAssistant) -> None:
|
|||
|
||||
mocker = MockRestData("test_scrape_uom_and_classes")
|
||||
with patch(
|
||||
"homeassistant.components.scrape.sensor.RestData",
|
||||
"homeassistant.components.rest.RestData",
|
||||
return_value=mocker,
|
||||
):
|
||||
assert await async_setup_component(hass, "sensor", config)
|
||||
|
@ -144,7 +144,7 @@ async def test_scrape_sensor_authentication(hass: HomeAssistant) -> None:
|
|||
|
||||
mocker = MockRestData("test_scrape_sensor_authentication")
|
||||
with patch(
|
||||
"homeassistant.components.scrape.sensor.RestData",
|
||||
"homeassistant.components.rest.RestData",
|
||||
return_value=mocker,
|
||||
):
|
||||
assert await async_setup_component(hass, "sensor", config)
|
||||
|
@ -162,7 +162,7 @@ async def test_scrape_sensor_no_data(hass: HomeAssistant) -> None:
|
|||
|
||||
mocker = MockRestData("test_scrape_sensor_no_data")
|
||||
with patch(
|
||||
"homeassistant.components.scrape.sensor.RestData",
|
||||
"homeassistant.components.rest.RestData",
|
||||
return_value=mocker,
|
||||
):
|
||||
assert await async_setup_component(hass, "sensor", config)
|
||||
|
@ -178,7 +178,7 @@ async def test_scrape_sensor_no_data_refresh(hass: HomeAssistant) -> None:
|
|||
|
||||
mocker = MockRestData("test_scrape_sensor")
|
||||
with patch(
|
||||
"homeassistant.components.scrape.sensor.RestData",
|
||||
"homeassistant.components.rest.RestData",
|
||||
return_value=mocker,
|
||||
):
|
||||
assert await async_setup_component(hass, "sensor", config)
|
||||
|
@ -208,7 +208,7 @@ async def test_scrape_sensor_attribute_and_tag(hass: HomeAssistant) -> None:
|
|||
|
||||
mocker = MockRestData("test_scrape_sensor")
|
||||
with patch(
|
||||
"homeassistant.components.scrape.sensor.RestData",
|
||||
"homeassistant.components.rest.RestData",
|
||||
return_value=mocker,
|
||||
):
|
||||
assert await async_setup_component(hass, "sensor", config)
|
||||
|
@ -231,7 +231,7 @@ async def test_scrape_sensor_errors(hass: HomeAssistant) -> None:
|
|||
|
||||
mocker = MockRestData("test_scrape_sensor")
|
||||
with patch(
|
||||
"homeassistant.components.scrape.sensor.RestData",
|
||||
"homeassistant.components.rest.RestData",
|
||||
return_value=mocker,
|
||||
):
|
||||
assert await async_setup_component(hass, "sensor", config)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue