Skip to content

Fix http call to home assistant#21

Merged
hbrennhaeuser merged 1 commit intohbrennhaeuser:mainfrom
vingerha:main
May 10, 2025
Merged

Fix http call to home assistant#21
hbrennhaeuser merged 1 commit intohbrennhaeuser:mainfrom
vingerha:main

Conversation

@vingerha
Copy link
Contributor

@vingerha vingerha commented May 10, 2025

I have a solution to allow to use the http 'action' from ntfy, it required a typo-fix and a minor change to the formatting.
The below works with this changed code:

action: notify.ntfy_ha
data:
  title: Homeassistant Notification
  message: Movement in backyard detected
  data:
    topic: test
    actions:
      - action: http
        label: Light living room on
        method: POST
        url: https://https-address-of-ha/api/services/switch/turn_on
        headers:
          Authorization: >-
            Bearer 123456789......
          Content-type: application/json
        body: "{\"entity_id\": \"switch.living-room\"}"

@vingerha
Copy link
Contributor Author

Important is to add CORS settings to the http in configuration.yaml, see
https://www.home-assistant.io/integrations/http/#cors_allowed_origins

@hbrennhaeuser hbrennhaeuser requested a review from Copilot May 10, 2025 09:49
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes the HTTP call functionality to Home Assistant by correcting a typo and updating the formatting in the header build process.

  • Corrects the use of keys for HTTP headers by switching from "extras" to "headers".
  • Adjusts the escaping methodology for the HTTP action body and adds additional debug logging for improved traceability.
Comments suppressed due to low confidence (2)

custom_components/ntfy/notify.py:324

  • The updated escaping for the body does not actually escape double or single quotes as intended. Consider using double backslashes (e.g., '\"') for proper escaping.
body = action.get('body','').replace('"', '"').replace("'", "'").replace('=', '\=')

custom_components/ntfy/notify.py:309

  • [nitpick] The added debug logging is useful for diagnosis, but verify that none of the logged data includes sensitive information such as authorization tokens.
_LOGGER.debug("action: %s", action)

Comment on lines +317 to 318
for key, value in action.get('headers').items():
tmp_header += f", headers.{key}={value}"
Copy link

Copilot AI May 10, 2025

Choose a reason for hiding this comment

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

The loop now correctly iterates over action['headers'], but please ensure that the 'headers' key always returns a dictionary as defined in the schema.

Suggested change
for key, value in action.get('headers').items():
tmp_header += f", headers.{key}={value}"
headers = action.get('headers')
if isinstance(headers, dict):
for key, value in headers.items():
tmp_header += f", headers.{key}={value}"
else:
_LOGGER.warning("Invalid headers format: expected a dictionary, got %s", type(headers).__name__)

Copilot uses AI. Check for mistakes.
@vingerha
Copy link
Contributor Author

@hbrennhaeuser I would like to add that I can only confirm this to be working when using the browser on laptop. For the app, it is not working on my ios stuff and no way to debug that (am not a ios specialist). I have no android devices so that may be something you (or anyone else) can test. My main challenges were understanding the required message, then getting the right format and resolving the cors blockers.

@hbrennhaeuser
Copy link
Owner

@vingerha I can confirm this is working on Android.
As far as i can tell the IOS-App does not support any features beside basic messages.
It sadly seems like it is not really maintained, the last updates to the GIT-Repo and to the AppStore-App were in late 2023.

hbrennhaeuser added a commit that referenced this pull request May 31, 2025
- Add documentation for homeassistant-interaction through action-buttons
- Add hint for features currently  not supported by the IOS-App
- See #21, thanks @vingerha
@vingerha
Copy link
Contributor Author

It works from the app on iPhone and iPad

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants