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

Code quality improvements at Home Connect #126323

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

Diegorro98
Copy link
Contributor

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)
  • 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 @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.

@Diegorro98
Copy link
Contributor Author

@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?

homeassistant/components/home_connect/__init__.py Outdated Show resolved Hide resolved
@@ -244,7 +244,7 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:


@Throttle(SCAN_INTERVAL)
Copy link
Member

Choose a reason for hiding this comment

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

We want to remove as much throttles as possible, so it would be interesting to explore the option of adding a coordinator

homeassistant/components/home_connect/api.py Outdated Show resolved Hide resolved
homeassistant/components/home_connect/api.py Outdated Show resolved Hide resolved
homeassistant/components/home_connect/api.py Outdated Show resolved Hide resolved
homeassistant/components/home_connect/binary_sensor.py Outdated Show resolved Hide resolved
homeassistant/components/home_connect/entity.py Outdated Show resolved Hide resolved
homeassistant/components/home_connect/entity.py Outdated Show resolved Hide resolved
homeassistant/components/home_connect/light.py Outdated Show resolved Hide resolved
homeassistant/components/home_connect/light.py Outdated Show resolved Hide resolved
@home-assistant home-assistant bot marked this pull request as draft September 20, 2024 12:26
@home-assistant
Copy link

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

Learn more about our pull request process.

@joostlek
Copy link
Member

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

@Diegorro98 Diegorro98 force-pushed the home_connect_code_quality_improvements branch 2 times, most recently from 0559f8f to f2bc45a Compare September 20, 2024 13:37
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
@Diegorro98 Diegorro98 force-pushed the home_connect_code_quality_improvements branch from f2bc45a to 9189af7 Compare September 20, 2024 14:27
@Diegorro98
Copy link
Contributor Author

Diegorro98 commented Sep 20, 2024

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)

BINARY_SENSORS: tuple[HomeConnectBinarySensorEntityDescription, ...] = (
HomeConnectBinarySensorEntityDescription(
key="Chiller Door",
state_key=REFRIGERATION_STATUS_DOOR_CHILLER,
),
HomeConnectBinarySensorEntityDescription(
key="Freezer Door",
state_key=REFRIGERATION_STATUS_DOOR_FREEZER,
),
HomeConnectBinarySensorEntityDescription(
key="Refrigerator Door",
state_key=REFRIGERATION_STATUS_DOOR_REFRIGERATOR,
),
)

LIGHTS: tuple[HomeConnectLightEntityDescription, ...] = (
HomeConnectLightEntityDescription(
key="Internal Light",
on_key=REFRIGERATION_INTERNAL_LIGHT_POWER,
brightness_key=REFRIGERATION_INTERNAL_LIGHT_BRIGHTNESS,
),
HomeConnectLightEntityDescription(
key="External Light",
on_key=REFRIGERATION_EXTERNAL_LIGHT_POWER,
brightness_key=REFRIGERATION_EXTERNAL_LIGHT_BRIGHTNESS,
),
)

SENSORS: tuple[HomeConnectSensorEntityDescription, ...] = (
HomeConnectSensorEntityDescription(
key="Door Alarm Freezer",
translation_key="alarm_sensor_freezer",
state_key=REFRIGERATION_EVENT_DOOR_ALARM_FREEZER,
appliance_types=("FridgeFreezer", "Freezer"),
),
HomeConnectSensorEntityDescription(
key="Door Alarm Refrigerator",
translation_key="alarm_sensor_fridge",
state_key=REFRIGERATION_EVENT_DOOR_ALARM_REFRIGERATOR,
appliance_types=("FridgeFreezer", "Refrigerator"),
),
HomeConnectSensorEntityDescription(
key="Temperature Alarm Freezer",
translation_key="alarm_sensor_temp",
state_key=REFRIGERATION_EVENT_TEMP_ALARM_FREEZER,
appliance_types=("FridgeFreezer", "Freezer"),
),
HomeConnectSensorEntityDescription(
key="Bean Container Empty",
translation_key="alarm_sensor_coffee_bean_container",
state_key=COFFEE_EVENT_BEAN_CONTAINER_EMPTY,
appliance_types=("CoffeeMaker",),
),
HomeConnectSensorEntityDescription(
key="Water Tank Empty",
translation_key="alarm_sensor_coffee_water_tank",
state_key=COFFEE_EVENT_WATER_TANK_EMPTY,
appliance_types=("CoffeeMaker",),
),
HomeConnectSensorEntityDescription(
key="Drip Tray Full",
translation_key="alarm_sensor_coffee_drip_tray",
state_key=COFFEE_EVENT_DRIP_TRAY_FULL,
appliance_types=("CoffeeMaker",),
),
)

SWITCHES: tuple[HomeConnectSwitchEntityDescription, ...] = (
HomeConnectSwitchEntityDescription(
key="Supermode Freezer",
on_key=REFRIGERATION_SUPERMODEFREEZER,
),
HomeConnectSwitchEntityDescription(
key="Supermode Refrigerator",
on_key=REFRIGERATION_SUPERMODEREFRIGERATOR,
),
HomeConnectSwitchEntityDescription(
key="Dispenser Enabled",
on_key=REFRIGERATION_DISPENSER,
translation_key="refrigeration_dispenser",
),
)

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.

2 participants