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

Door entities as enum sensors at Home Connect #126158

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

Conversation

Diegorro98
Copy link
Contributor

@Diegorro98 Diegorro98 commented Sep 17, 2024

Breaking change

Proposed change

Because door states are specified to be enumeration data types at Home Connect API Documentation (link), door entity sensors might should be sensors with Enum device class.

This change would deprecate door binary sensor (maybe no needed for recently added binary door sensors as they have not been released yet).

Also added more door sensors.

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

This PR will be keept in draft until #126143 gets merged, because it will allow to refactor code.

  • 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 home-assistant bot added cla-signed deprecation Indicates a breaking change to happen in the future integration: home_connect labels Sep 17, 2024
@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

Also I'd like to explore whether it's better to retrieve the available options for an enum device class sensor entity from the Home Connect API, rather than hardcoding them in the entity description.

@joostlek
Copy link
Member

Downside about dynamically fetching is the lack of translations, and thus a suboptimal user experience for a lot of people

@Diegorro98
Copy link
Contributor Author

That's a good point,
Another downside, but about hardcoding options, is that if a value that it is not in the hardcoded in the options is sent from the API, it will raise an exception

@Diegorro98
Copy link
Contributor Author

Diegorro98 commented Sep 18, 2024

Btw, I just discovered that refrigeration doors possible states are Refrigeration.Common.EnumType.Door.States.Closed and Refrigeration.Common.EnumType.Door.States.Open, but I can't find such values at API docs. @beastie29a, is this something that you found by fetching your own appliances' states?

@joostlek
Copy link
Member

Yes, but I think it's a nice way to gather all possible values. It might bite in the start for a group of users, but in the end you're left with a lot more knowledge about the API

@beastie29a
Copy link
Contributor

Btw, I just discovered that refrigeration doors possible states are Refrigeration.Common.EnumType.Door.States.Closed and Refrigeration.Common.EnumType.Door.States.Open, but I can't find such values at API docs. @beastie29a, is this something that you found by fetching your own appliances' states?

Correct, and there's one other undocumented API common door state I found BSH.Common.EnumType.DoorState.Ajar. I don't know how or what can trigger it, but it was interesting seeing it returned from the API.

@beastie29a
Copy link
Contributor

Also I'd like to explore whether it's better to retrieve the available options for an enum device class sensor entity from the Home Connect API, rather than hardcoding them in the entity description.

The integration is already plagued with API rate limit issues and adding more would probably make the UX worse in my opinion. Though, I do think it would be ideal to fetch the values from the API.

There's an experiment I'm working on inside the integration to get cached values from the entity registry and have it managed by the DataUpdateCoordinator, which I would recommend using here for coordinating the API data retrieval.

This is sort of a POC for what I had in mind where we load data from the entity_registry and then query that data to see if an API call needs to be made.

I wasn't able to find an example in other integrations so I'm certain it doesn't follow the rules 😅 , though on discord I found a thread that pointed to good information from someone with the same requirements and also linked an architecture discussion along the same lines.

Now the reason I'd say use the DataUpdateCoordinator is to take advantage if/when new features become available such as caching data from the API and a way to force an update say for example if the units change from Celsius to Fahrenheit in the Set Point Temperature entity.

Comment on lines +74 to +75
"Closed",
"Open",
Copy link
Contributor

Choose a reason for hiding this comment

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

Since it was only 2 states I ended up switching these to the binary_sensor platform in PR #125490 .

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It makes sense.
Given that the API docs specifies that door states use an enum data type, I believe enum sensors are a better choice.
Here’s why: if any of these entities, which we currently assume that they have only 2 states, suddenly support an additional state (like a locked state for doors, or any new possible state), we need to ensure compatibility. This would mean deprecating the binary door sensor and introducing a new enum sensor, along with handling any collateral effects.

@@ -182,12 +331,12 @@ async def async_update(self) -> None:
class HomeConnectAlarmSensor(HomeConnectEntity, SensorEntity):
"""Sensor entity setup using SensorEntityDescription."""

entity_description: HomeConnectSensorEntityDescription
entity_description: HomeConnectAlarmSensorEntityDescription
Copy link
Contributor

Choose a reason for hiding this comment

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

To me the main reason to use EntityDescription data classes was to have a unified class for the Platform to make it easier for entities to be added in the future since a developer or user would only need to add an item to the appropriate tuple instead of duplicating a class and changing its logic.

Separate classes also make it similar to the current method of adding entities which, as you've probably seen in the switch entity, can make adding additional entities complicated and add more duplicate code than necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I understand your idea, however, I think that simplifying so much the sensor entity class results in more complicated entity descriptions that contains duplicated code.
Also, this PR was about door enum entities, code quailty improvements and simplifications comes in another PR I'm preparing these days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants