Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auth and headers support for REST sensor #3592

Merged
merged 2 commits into from Oct 4, 2016
Merged

Auth and headers support for REST sensor #3592

merged 2 commits into from Oct 4, 2016

Conversation

fabaff
Copy link
Member

@fabaff fabaff commented Sep 29, 2016

Description:
Support for HTTP authentication and headers for the REST sensor.

This will require an update of the REST Binary sensor and the PI Hole sensor.

Pull request in home-assistant.io with documentation (if applicable): home-assistant/home-assistant.io#1050

Example entry for configuration.yaml (if applicable):

sensor:
  - platform: rest
    resource: http://IP_ADDRESS:5000/sensor
    username: ha1
    password: test1
    authetication: basic
    headers:
      User-agent: Home Assistant
      Content-Type: application/json

With the configuration sample from above the header will look like this:

Content-Length: 
Host: 127.0.0.1:5000
Authorization: Basic aGExOnRlc3Qx
Accept-Encoding: identity
Content-Type: application/json
User-Agent: Home Assistant

Checklist:

If user exposed functionality or configuration variables are added/changed:

If code communicates with devices, web services, or a:

  • Local tests with tox run successfully. Your PR cannot be merged unless tests pass
  • New dependencies have been added to the REQUIREMENTS variable (example).
  • New dependencies are only imported inside functions that use them (example).
  • New dependencies have been added to requirements_all.txt by running script/gen_requirements_all.py.
  • New files were added to .coveragerc.

@fabaff
Copy link
Member Author

fabaff commented Sep 29, 2016

Perhaps we should supersede this PR with an aiohttp implementation.

@balloob
Copy link
Member

balloob commented Oct 2, 2016

Let's leave the aiohttp implementation until we have some better examples. We will also need all aiohttp platforms to be covered by tests as async is hard.

def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the REST binary sensor."""
name = config.get(CONF_NAME)
resource = config.get(CONF_RESOURCE)
method = config.get(CONF_METHOD)
payload = config.get(CONF_PAYLOAD)
verify_ssl = config.get(CONF_VERIFY_SSL)
username = config.get(CONF_USERNAME)
password = config.get(CONF_PASSWORD)
headers = json.loads(config.get(CONF_HEADERS, '{}'))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specify default in PLATFORM_SCHEMA. Also, YAML parses JSON so I doubt the string is really valid JSON here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also never expect JSON in YAML. Just make it a dict:

binary_sensor:
  platform: rest
  headers:
    hello: world

Thus validation it as {cv.string: cv.string} will do.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. Thanks.

@balloob
Copy link
Member

balloob commented Oct 4, 2016

🐬

@balloob balloob merged commit a072047 into home-assistant:dev Oct 4, 2016
@fabaff fabaff deleted the rest-auth-header branch October 5, 2016 08:55
hartmms pushed a commit to hartmms/home-assistant that referenced this pull request Oct 5, 2016
* Add auth and header support

* Update header part
@home-assistant home-assistant locked and limited conversation to collaborators Mar 17, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants