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

Plugwise prepare typing for binary_sensor #93162

Merged
merged 2 commits into from May 22, 2023

Conversation

CoMPaTech
Copy link
Contributor

@CoMPaTech CoMPaTech commented May 16, 2023

Proposed change

Change entitydescription approach for binary_sensor preparing for upcoming strict typing and module version bump PRs. Decreasing PR size as per #92253 / #93088

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][dev-checklist]
  • I have followed the [perfect PR recommendations][perfect-pr]
  • The code has been formatted using Black (black --fast homeassistant tests)
  • Tests have been added to verify that the new code works.

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

  • Documentation added/updated for [www.home-assistant.io][docs-repository]

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

  • The [manifest file][manifest-docs] 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.
  • Untested files have been added to .coveragerc.

To help with the load of incoming pull requests:

  • I have reviewed two other [open pull requests][prs] in this repository.

@home-assistant
Copy link

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

Code owner commands

Code owners of plugwise 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 plugwise Removes the current integration label and assignees on the pull request, add the integration domain after the command.

Copy link
Contributor

@bouwew bouwew left a comment

Choose a reason for hiding this comment

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

LGTM

@@ -35,19 +46,22 @@ class PlugwiseBinarySensorEntityDescription(BinarySensorEntityDescription):
icon="mdi:hvac",
icon_off="mdi:hvac-off",
entity_category=EntityCategory.DIAGNOSTIC,
value_fn=lambda data: data["binary_sensors"]["compressor_state"],
Copy link
Contributor

Choose a reason for hiding this comment

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

You could simplify this quite a bit by passing data["binary_sensors"] directly:

Suggested change
value_fn=lambda data: data["binary_sensors"]["compressor_state"],
value_fn=lambda data: data["compressor_state"],

Copy link
Contributor Author

@CoMPaTech CoMPaTech May 19, 2023

Choose a reason for hiding this comment

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

As per Frencks (in #93155) and you suggestion I rewrote the approach, models.py no already shows the intended parts for sensor and switch as well (hence the pragma, pw_key is used in sensor). I know we should stick to only binary_sensor as per keeping it small, but we would like to know if this is going in the right direction.

I took your suggestion of passing the key and combined it to leave lambda's out as requested in the other draft.

Copy link
Member

Choose a reason for hiding this comment

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

I really think the code became worse after this part. Like the suggestions made here by epenet have been taken and over-engineered IMHO.

The whole lookup stuff is really nog needed at all. All epenet suggested, was simply passing part of the dictionary, instead of all.

homeassistant/components/plugwise/binary_sensor.py Outdated Show resolved Hide resolved
homeassistant/components/plugwise/binary_sensor.py Outdated Show resolved Hide resolved
homeassistant/components/plugwise/binary_sensor.py Outdated Show resolved Hide resolved
@CoMPaTech CoMPaTech mentioned this pull request May 19, 2023
20 tasks
@CoMPaTech CoMPaTech marked this pull request as ready for review May 19, 2023 12:23
@CoMPaTech CoMPaTech requested a review from frenck as a code owner May 19, 2023 12:23
@CoMPaTech CoMPaTech changed the title Plugwise add value_fn for binary_sensor Plugwise prepare typing for binary_sensor May 20, 2023
@frenck
Copy link
Member

frenck commented May 21, 2023

I would strongly suggest reverting e0c02d1, it makes everything unneeded complex and not what was originally commented either.

@CoMPaTech CoMPaTech marked this pull request as draft May 21, 2023 19:49
@CoMPaTech
Copy link
Contributor Author

Setting back to draft, agreed with the less complexity + as per #93155 lambda was also not the correct route. Rethinking solution.

@frenck
Copy link
Member

frenck commented May 21, 2023

This PR original had a fine solution, epenet give a slight hint to make it a little better (which however, was ignored and next added unneeded complexity)

Copy link
Contributor

@epenet epenet left a comment

Choose a reason for hiding this comment

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

(Now) Matches what I had in mind.
Happy to merge if @frenck also approves.

@CoMPaTech
Copy link
Contributor Author

(Now) Matches what I had in mind.
Happy to merge if @frenck also approves.

Do note that in #93155 @frenck had comments on value_fn (hence the spin-off with the mixins). But happy to continue along this line if you both are.

@CoMPaTech CoMPaTech marked this pull request as ready for review May 21, 2023 21:41
@frenck
Copy link
Member

frenck commented May 22, 2023

Yup an that comment remains even now, we, we don't need to use lambdas in this case at all.

@epenet
Copy link
Contributor

epenet commented May 22, 2023

I've looked at this again.
I think the confusion around mypy is the issue of TypedDict keys.

Using lambda would have:

    value_fn: Callable[[SmileBinarySensors], bool]
...
    value_fn=lambda data: data["compressor_state"],
...
    return self.entity_description.value_fn(self.device["binary_sensors"])

Using a key (separate from the main key to be able to use literal):

    value_key: Literal[
        "cooling_enabled",
        "compressor_state",
        "cooling_state",
        "dhw_state",
        "flame_state",
        "heating_state",
        "plugwise_notification",
        "slave_boiler_state",
    ]
...
    value_key="compressor_state",
...
    return self.device["binary_sensors"][self.entity_description.value_key]

@frenck
Copy link
Member

frenck commented May 22, 2023

We do not have to use value_key, we can just use the existing key (as that is already based on the value in the dictionary).

@epenet
Copy link
Contributor

epenet commented May 22, 2023

We do not have to use value_key, we can just use the existing key (as that is already based on the value in the dictionary).

But then you lose the type hints, and end up having to ignore no-any-return/literal-required when you enable strict typing.

homeassistant/components/plugwise/binary_sensor.py:167: error: Returning Any from function declared to return "Optional[bool]"  [no-any-return]
homeassistant/components/plugwise/binary_sensor.py:167: error: TypedDict key must be a string literal; expected one of ("cooling_enabled", "compressor_state", "cooling_state", "dhw_state", "flame_state", ...)  [literal-required]

@frenck
Copy link
Member

frenck commented May 22, 2023

Right makes sense, with that in mind, I think the current lambda is the cleanest path 👍

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, @CoMPaTech 👍

../Frenck

@frenck frenck merged commit 79cafd5 into home-assistant:dev May 22, 2023
24 checks passed
@CoMPaTech CoMPaTech deleted the pw_typ_bin branch May 22, 2023 07:19
@CoMPaTech CoMPaTech mentioned this pull request May 22, 2023
20 tasks
@github-actions github-actions bot locked and limited conversation to collaborators May 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants