core icon indicating copy to clipboard operation
core copied to clipboard

Code quality improvements at Home Connect

Open Diegorro98 opened this issue 1 year ago • 7 comments

Breaking change

Proposed change

  • Added types to all arguments and return values to all functions
  • Defined class members and its types outside the constructor
  • Improved logic at binary sensor

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

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:

Diegorro98 avatar Sep 20 '24 12:09 Diegorro98

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

Code owner commands

Code owners of home_connect 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 home_connect 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 Sep 20 '24 12:09 home-assistant[bot]

@joostlek These are all the things I found that I think they need quality improvements, do you know any tool or command that can help me find any other code quality issues?

Diegorro98 avatar Sep 20 '24 12:09 Diegorro98

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks :+1:

Learn more about our pull request process.

home-assistant[bot] avatar Sep 20 '24 12:09 home-assistant[bot]

We want our functions completely typed in general. Instance variables are usually inferred when set (unless mypy complains, then you add one). So we don't have to overwrite every instance variable with a type outside of the constructor

joostlek avatar Sep 20 '24 12:09 joostlek

I noticed that in some components, the entity description tuples are placed at the end of the file This might be a good idea to apply because entity descriptions can be quite large, and if they are placed before classes and logic, classes and logic will be be at the end of the file, which means you have to scroll all the way down to get to the code, which is quite inconvenient and sensor. (And also specifying the type of the tuples might not be necessary) https://github.com/home-assistant/core/blob/9189af7e84a36e2deafb88cabad58ab231008c9f/homeassistant/components/home_connect/binary_sensor.py#L52-L65 https://github.com/home-assistant/core/blob/9189af7e84a36e2deafb88cabad58ab231008c9f/homeassistant/components/home_connect/light.py#L52-L63 https://github.com/home-assistant/core/blob/9189af7e84a36e2deafb88cabad58ab231008c9f/homeassistant/components/home_connect/sensor.py#L53-L90 https://github.com/home-assistant/core/blob/9189af7e84a36e2deafb88cabad58ab231008c9f/homeassistant/components/home_connect/switch.py#L40-L54

Diegorro98 avatar Sep 20 '24 14:09 Diegorro98

Also, I had a question that this PR might be the place to be answered to then make the necessary changes and add them to this PR. At strings.json, is there any specified order for the first level keys? I've been setting them always alphabetically ordered, but right now in this component, entity object is at the end of the file, should we move it?

Diegorro98 avatar Sep 20 '24 15:09 Diegorro98

Usually the order in the file is

Entity definitions Setup logic Entity classes

joostlek avatar Sep 22 '24 09:09 joostlek

@joostlek regarding the strings.json file, does it require a particular order? I'm asking because on the dev branch, assuming alphabetical sorting is used, the entity object seems to be misplaced in this component.

Diegorro98 avatar Sep 23 '24 11:09 Diegorro98

We don't really enforce an order in the strings.json, but it usually starts with the config flow and then the rest

joostlek avatar Sep 23 '24 11:09 joostlek

Cool, good to know, I will leave the current order then in the other PRs, thanks!

Diegorro98 avatar Sep 23 '24 11:09 Diegorro98