Cleanup schema validation in scrape sensor (#81419)

This commit is contained in:
epenet 2022-11-02 18:46:03 +01:00 committed by GitHub
parent 466365c8de
commit d385a85ccb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -83,7 +83,8 @@ async def async_setup_platform(
discovery_info: DiscoveryInfoType | None = None, discovery_info: DiscoveryInfoType | None = None,
) -> None: ) -> None:
"""Set up the Web scrape sensor.""" """Set up the Web scrape sensor."""
entities: list[ScrapeSensor] = [] coordinator: ScrapeCoordinator
sensors_config: list[ConfigType]
if discovery_info is None: if discovery_info is None:
async_create_issue( async_create_issue(
hass, hass,
@ -99,27 +100,22 @@ async def async_setup_platform(
coordinator = ScrapeCoordinator(hass, rest, SCAN_INTERVAL) coordinator = ScrapeCoordinator(hass, rest, SCAN_INTERVAL)
sensors_config: list[tuple[ConfigType, ConfigType]] = [ sensors_config = [
( vol.Schema(TEMPLATE_SENSOR_BASE_SCHEMA.schema, extra=vol.ALLOW_EXTRA)(
config, config
vol.Schema(TEMPLATE_SENSOR_BASE_SCHEMA.schema, extra=vol.REMOVE_EXTRA)(
config
),
) )
] ]
else: else:
coordinator = discovery_info["coordinator"] coordinator = discovery_info["coordinator"]
sensors_config = [ sensors_config = discovery_info["configs"]
(sensor_config, sensor_config)
for sensor_config in discovery_info["configs"]
]
await coordinator.async_refresh() await coordinator.async_refresh()
if coordinator.data is None: if coordinator.data is None:
raise PlatformNotReady raise PlatformNotReady
for sensor_config, template_config in sensors_config: entities: list[ScrapeSensor] = []
for sensor_config in sensors_config:
value_template: Template | None = sensor_config.get(CONF_VALUE_TEMPLATE) value_template: Template | None = sensor_config.get(CONF_VALUE_TEMPLATE)
if value_template is not None: if value_template is not None:
value_template.hass = hass value_template.hass = hass
@ -128,9 +124,9 @@ async def async_setup_platform(
ScrapeSensor( ScrapeSensor(
hass, hass,
coordinator, coordinator,
template_config, sensor_config,
template_config[CONF_NAME], sensor_config[CONF_NAME],
template_config.get(CONF_UNIQUE_ID), sensor_config.get(CONF_UNIQUE_ID),
sensor_config.get(CONF_SELECT), sensor_config.get(CONF_SELECT),
sensor_config.get(CONF_ATTRIBUTE), sensor_config.get(CONF_ATTRIBUTE),
sensor_config[CONF_INDEX], sensor_config[CONF_INDEX],