Allow separate URL for REST switch state (#39557)
This commit is contained in:
1 parent
ebc31c0f08
commit
f01a0f9151
2 files changed
+73
-22
No files matched your search
@@ -30,6 +30,7 @@ _LOGGER = logging.getLogger(__name__)
|
||||
CONF_BODY_OFF = "body_off"
|
||||
CONF_BODY_ON = "body_on"
|
||||
CONF_IS_ON_TEMPLATE = "is_on_template"
|
||||
CONF_STATE_RESOURCE = "state_resource"
|
||||
|
||||
DEFAULT_METHOD = "post"
|
||||
DEFAULT_BODY_OFF = "OFF"
|
||||
@@ -43,6 +44,7 @@ SUPPORT_REST_METHODS = ["post", "put"]
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||
{
|
||||
vol.Required(CONF_RESOURCE): cv.url,
|
||||
vol.Optional(CONF_STATE_RESOURCE): cv.url,
|
||||
vol.Optional(CONF_HEADERS): {cv.string: cv.string},
|
||||
vol.Optional(CONF_BODY_OFF, default=DEFAULT_BODY_OFF): cv.template,
|
||||
vol.Optional(CONF_BODY_ON, default=DEFAULT_BODY_ON): cv.template,
|
||||
@@ -73,6 +75,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
|
||||
username = config.get(CONF_USERNAME)
|
||||
password = config.get(CONF_PASSWORD)
|
||||
resource = config.get(CONF_RESOURCE)
|
||||
state_resource = config.get(CONF_STATE_RESOURCE) or resource
|
||||
verify_ssl = config.get(CONF_VERIFY_SSL)
|
||||
|
||||
auth = None
|
||||
@@ -91,6 +94,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
|
||||
switch = RestSwitch(
|
||||
name,
|
||||
resource,
|
||||
state_resource,
|
||||
method,
|
||||
headers,
|
||||
auth,
|
||||
@@ -122,6 +126,7 @@ class RestSwitch(SwitchEntity):
|
||||
self,
|
||||
name,
|
||||
resource,
|
||||
state_resource,
|
||||
method,
|
||||
headers,
|
||||
auth,
|
||||
@@ -135,6 +140,7 @@ class RestSwitch(SwitchEntity):
|
||||
self._state = None
|
||||
self._name = name
|
||||
self._resource = resource
|
||||
self._state_resource = state_resource
|
||||
self._method = method
|
||||
self._headers = headers
|
||||
self._auth = auth
|
||||
@@ -213,7 +219,7 @@ class RestSwitch(SwitchEntity):
|
||||
|
||||
with async_timeout.timeout(self._timeout):
|
||||
req = await websession.get(
|
||||
self._resource, auth=self._auth, headers=self._headers
|
||||
self._state_resource, auth=self._auth, headers=self._headers
|
||||
)
|
||||
text = await req.text()
|
||||
|
||||
|
||||
Reference in new issue
Block a user