From df6d19c58baf9947207accd64562464cee0e9a9e Mon Sep 17 00:00:00 2001 From: danieldotnl Date: Fri, 5 Apr 2024 09:16:23 +0000 Subject: [PATCH] Separate http settings for form submit and scrape request --- custom_components/multiscrape/__init__.py | 3 ++- custom_components/multiscrape/const.py | 1 - custom_components/multiscrape/form.py | 4 ++-- custom_components/multiscrape/schema.py | 25 +++++++++++++---------- custom_components/multiscrape/service.py | 3 ++- 5 files changed, 20 insertions(+), 16 deletions(-) diff --git a/custom_components/multiscrape/__init__.py b/custom_components/multiscrape/__init__.py index 7550647f..14c30774 100644 --- a/custom_components/multiscrape/__init__.py +++ b/custom_components/multiscrape/__init__.py @@ -122,9 +122,10 @@ async def _async_process_config(hass: HomeAssistant, config) -> bool: form_submit_config = conf.get(CONF_FORM_SUBMIT) form_submitter = None if form_submit_config: + form_http = create_http_wrapper(config_name, form_submit_config, hass, file_manager) parser = conf.get(CONF_PARSER) form_submitter = create_form_submitter( - config_name, form_submit_config, hass, http, file_manager, parser + config_name, form_submit_config, hass, form_http, file_manager, parser ) scraper = create_scraper(config_name, conf, hass, file_manager) diff --git a/custom_components/multiscrape/const.py b/custom_components/multiscrape/const.py index 3b4d1c3e..87502d29 100644 --- a/custom_components/multiscrape/const.py +++ b/custom_components/multiscrape/const.py @@ -29,7 +29,6 @@ CONF_FORM_SELECT = "select" CONF_FORM_INPUT = "input" CONF_FORM_INPUT_FILTER = "input_filter" -CONF_FORM_RESOURCE = "resource" CONF_FORM_SUBMIT_ONCE = "submit_once" CONF_FORM_RESUBMIT_ERROR = "resubmit_on_error" CONF_LOG_RESPONSE = "log_response" diff --git a/custom_components/multiscrape/form.py b/custom_components/multiscrape/form.py index a5d941ed..54e9a592 100644 --- a/custom_components/multiscrape/form.py +++ b/custom_components/multiscrape/form.py @@ -5,9 +5,9 @@ from bs4 import BeautifulSoup from homeassistant.core import HomeAssistant +from homeassistant.const import CONF_RESOURCE from .const import ( - CONF_FORM_RESOURCE, CONF_FORM_SELECT, CONF_FORM_INPUT, CONF_FORM_INPUT_FILTER, @@ -23,7 +23,7 @@ def create_form_submitter(config_name, config, hass, http, file_manager, parser): """Create a form submitter instance.""" - resource = config.get(CONF_FORM_RESOURCE) + resource = config.get(CONF_RESOURCE) select = config.get(CONF_FORM_SELECT) input_values = config.get(CONF_FORM_INPUT) input_filter = config.get(CONF_FORM_INPUT_FILTER) diff --git a/custom_components/multiscrape/schema.py b/custom_components/multiscrape/schema.py index e07c6a9b..fe8f01e6 100644 --- a/custom_components/multiscrape/schema.py +++ b/custom_components/multiscrape/schema.py @@ -39,7 +39,6 @@ from .const import CONF_ATTR from .const import CONF_FORM_INPUT from .const import CONF_FORM_INPUT_FILTER -from .const import CONF_FORM_RESOURCE from .const import CONF_FORM_RESUBMIT_ERROR from .const import CONF_FORM_SELECT from .const import CONF_FORM_SUBMIT @@ -75,16 +74,7 @@ _LOGGER = logging.getLogger(__name__) -FORM_SUBMIT_SCHEMA = { - vol.Optional(CONF_FORM_RESOURCE): cv.string, - vol.Optional(CONF_FORM_SELECT): cv.string, - vol.Optional(CONF_FORM_INPUT): vol.Schema({cv.string: cv.string}), - vol.Optional(CONF_FORM_INPUT_FILTER, default=[]): cv.ensure_list, - vol.Optional(CONF_FORM_SUBMIT_ONCE, default=False): cv.boolean, - vol.Optional(CONF_FORM_RESUBMIT_ERROR, default=True): cv.boolean, -} - -INTEGRATION_SCHEMA = { +HTTP_SCHEMA = { vol.Exclusive(CONF_RESOURCE, CONF_RESOURCE): cv.url, vol.Exclusive(CONF_RESOURCE_TEMPLATE, CONF_RESOURCE): cv.template, vol.Optional(CONF_AUTHENTICATION): vol.In( @@ -98,6 +88,19 @@ vol.Optional(CONF_PAYLOAD): cv.template, vol.Optional(CONF_VERIFY_SSL, default=DEFAULT_VERIFY_SSL): cv.boolean, vol.Optional(CONF_TIMEOUT, default=DEFAULT_TIMEOUT): cv.positive_int, +} + +FORM_SUBMIT_SCHEMA = { + **HTTP_SCHEMA, + vol.Optional(CONF_FORM_SELECT): cv.string, + vol.Optional(CONF_FORM_INPUT): vol.Schema({cv.string: cv.string}), + vol.Optional(CONF_FORM_INPUT_FILTER, default=[]): cv.ensure_list, + vol.Optional(CONF_FORM_SUBMIT_ONCE, default=False): cv.boolean, + vol.Optional(CONF_FORM_RESUBMIT_ERROR, default=True): cv.boolean, +} + +INTEGRATION_SCHEMA = { + **HTTP_SCHEMA, vol.Optional(CONF_PARSER, default=DEFAULT_PARSER): cv.string, vol.Optional(CONF_NAME): cv.string, vol.Optional(CONF_SCAN_INTERVAL): cv.time_period, diff --git a/custom_components/multiscrape/service.py b/custom_components/multiscrape/service.py index 9950e5d4..b1f06196 100644 --- a/custom_components/multiscrape/service.py +++ b/custom_components/multiscrape/service.py @@ -151,8 +151,9 @@ async def _prepare_service_request(hass: HomeAssistant, conf, config_name): form_submit_config = conf.get(CONF_FORM_SUBMIT) parser = conf.get(CONF_PARSER) if form_submit_config: + form_http = create_http_wrapper(config_name, form_submit_config, hass, None) form_submitter = create_form_submitter( - config_name, form_submit_config, hass, http, None, parser + config_name, form_submit_config, hass, form_http, None, parser ) request_manager = create_content_request_manager( config_name, conf, hass, http, form_submitter