core icon indicating copy to clipboard operation
core copied to clipboard

Refactor NUT commands to use objects instead of strings

Open eclair4151 opened this issue 10 months ago • 10 comments

Proposed change

This is part 1 of a 2 part PR for adding support for NUT commands with parameters. This PR does the initial work of refactoring the nut commands to be objects with an optional parameter object instead of just strings. The next PR will implement the logic to handle the parameter within Home Assistant.

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)
  • [X] Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:

Checklist

  • [X] The code change is tested and works locally.
  • [X] Local tests pass. Your PR cannot be merged unless tests pass
  • [X] There is no commented out code in this PR.
  • [X] I have followed the development checklist
  • [X] I have followed the perfect PR recommendations
  • [X] The code has been formatted using Ruff (ruff format homeassistant tests)
  • [X] 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:

eclair4151 avatar Jan 08 '25 14:01 eclair4151

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

Code owner commands

Code owners of nut 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 nut 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.

home-assistant[bot] avatar Jan 08 '25 14:01 home-assistant[bot]

So why are these device actions and not normal service actions to begin with?

joostlek avatar Jan 09 '25 09:01 joostlek

So why are these device actions and not normal service actions to begin with?

This is my first time working with the NUT component, so Nick prob has more background on that than I do, but I was assuming it was because each device only supports a subset of the commands listed above. You need to query the device, and you get back a supported set of commands, which is different for each ups. Then we match up the supported commands the UPS sends to us with the ones we know we support in the above list to expose the specific list of actions supported per device.

eclair4151 avatar Jan 09 '25 16:01 eclair4151

Because when I look at the code now I would maybe consider just creating entities out of the functionality. There are probably some things that can be changed into buttons and switches from looking at the names. Are there reasons before or against that?

For other things we can use actions calls if needed, but they have to be registered in async_setup, and thus are always available, no matter if the device supports or not, which requires a bit of extra validation

joostlek avatar Jan 09 '25 16:01 joostlek

There are probably some things that can be changed into buttons and switches from looking at the names. Are there reasons before or against that?

Many of them are capable of taking parameters so it was decided not to implement them as buttons in the original PR since it would be confusing to have buttons for some and actions for others that take parameters

bdraco avatar Jan 09 '25 16:01 bdraco

Device automations should only be a device layer on top of core interfaces.

There needs to be service actions, either entity based or integration based that perform the same actions as a foundation.

MartinHjelmare avatar Jan 10 '25 11:01 MartinHjelmare

@eclair4151 In the phase 2 PR, could you add retries to the connection polling. I have 1 UPS connected to a RPi Zero 2 and well Wifi...

rwalker777 avatar Feb 14 '25 16:02 rwalker777

I believe we would be best served to move some of the device_actions into buttons, switches, etc. This will allow users to initiate them from the GUI as well as invoke them through automations.

tdfountain avatar Mar 11 '25 06:03 tdfountain

I believe we would be best served to move some of the device_actions into buttons, switches, etc. This will allow users to initiate them from the GUI as well as invoke them through automations.

I agree, and looks like Martin does as well from the 👍 above

bdraco avatar Mar 11 '25 23:03 bdraco

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[bot] avatar May 11 '25 01:05 github-actions[bot]