Skip to content

File add read_file action with Response #139216

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

Draft
wants to merge 17 commits into
base: dev
Choose a base branch
from

Conversation

PeteRager
Copy link
Contributor

@PeteRager PeteRager commented Feb 24, 2025

Proposed change

Add action to allow reading a file and returning the file content as the response to be used in an automation. This allows loading content or metadata; reading incoming files; and using this data in an automation. Initially, the action supports loading JSON or YAML files.

Example action call:

action: file.read_file
data:
  file_encoding: yaml
  file_name: config/media/my_file.yaml
response_variable: my_data

Sample result

aircons:
  ac1:
    info:
      climateControlModeIsRunning: false
      countDownToOff: 10
      countDownToOn: 0
      fan: high
      filterCleanStatus: 0
      freshAirStatus: "off"
      mode: vent
      myZone: 1
      name: AC One
      setTemp: 24
      state: "on"

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.

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

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.

To help with the load of incoming pull requests:

@home-assistant
Copy link

Hey there @fabaff, mind taking a look at this pull request as it has been labeled with an integration (file) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of file can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign file Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

@PeteRager PeteRager marked this pull request as draft February 24, 2025 21:36
@PeteRager PeteRager marked this pull request as ready for review February 24, 2025 22:22
"services": {
"read_file": {
"name": "Read File",
"description": "Reads a file and return the contents.",
Copy link
Contributor

Choose a reason for hiding this comment

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

-> "… and returns …" (third-person for both verbs :-))

In all three name keys use sentence-casing:

  • "Read file"
  • "File name"
  • "File encoding"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thank you, i've updated.

yaml_content = yaml.safe_load(file_content)
if isinstance(yaml_content, dict):
return yaml_content
return {"yaml": yaml_content}
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are we putting it in a dictionary?

Copy link
Contributor Author

@PeteRager PeteRager Feb 25, 2025

Choose a reason for hiding this comment

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

When the YAML file contains a list of elements - for example in the fixture file file_read_list.yaml - a list is returned. A list is not serializable as a dict (at least as the top most element) and a runtime error gets generated when HA tries to serialize. So placing it in a dict like this allows is to be serialized. I found a similar result if the YAML file contained only a string (returns a str). So if safe_load does not return a dict we need to create a dict.

I will add a comment to the code. Perhaps we should call the dict element something other than "yaml" like "data"?

We could also always create a dict regardless of the content type, this may be more consistent and simpler. I did not do that because most YAML file are typically dicts and it would add another level to navigate when processing the response.

Copy link
Contributor

@arturpragacz arturpragacz Feb 25, 2025

Choose a reason for hiding this comment

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

I like data as a key more than yaml. But my main concern is different: I don't like that users would have to guess how the response is formatted. I think that if it is a dict, you should also put in into data, so that it is consistent.
That also prevents degenerate cases, like how do you differentiate a list from a dict with a single key data and a list as a value.

Edit: I didn't see your edit when writing my response, but yes, that is what I'm proposing. I think consistency here is more important.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, makes sense to me. We should also do the same thing for the json branch? So the parsing result will always be in response["data"]

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, let's keep it the same for all.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated

Copy link
Member

@frenck frenck left a comment

Choose a reason for hiding this comment

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

Thanks for this pull request @PeteRager 👍

I do think this is something I want to check in a core architectural meeting to see if this is something we would like to have on core or not mostly because this can be a potential security risk.

I'll check and get back to you once I have more information. For now, I'm marking this PR as a draft.

../Frenck

@frenck frenck marked this pull request as draft March 1, 2025 21:13
@PeteRager
Copy link
Contributor Author

Thanks. I am restricting it to paths registered in allowlist_external_dirs

https://www.home-assistant.io/integrations/homeassistant/

@frenck
Copy link
Member

frenck commented Mar 1, 2025

Yes, I've seen. That is already good. Just want to make sure the rest agrees on it as well.

../Frenck

Copy link

There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days.
If you are the author of this PR, please leave a comment if you want to keep it open. Also, please rebase your PR onto the latest dev branch to ensure that it's up to date with the latest changes.
Thank you for your contribution!

@github-actions github-actions bot added the stale label Apr 30, 2025
@PeteRager
Copy link
Contributor Author

Keep

@github-actions github-actions bot removed the stale label Apr 30, 2025
Copy link

There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days.
If you are the author of this PR, please leave a comment if you want to keep it open. Also, please rebase your PR onto the latest dev branch to ensure that it's up to date with the latest changes.
Thank you for your contribution!

@github-actions github-actions bot added the stale label Jun 29, 2025
@PeteRager
Copy link
Contributor Author

Keep

@github-actions github-actions bot removed the stale label Jun 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants