Skip to content

Fix typing for wemo#62157

Merged
bdraco merged 9 commits into
home-assistant:devfrom
esev:wemo_strict_typing
Dec 20, 2021
Merged

Fix typing for wemo#62157
bdraco merged 9 commits into
home-assistant:devfrom
esev:wemo_strict_typing

Conversation

@esev
Copy link
Copy Markdown
Contributor

@esev esev commented Dec 17, 2021

Proposed change

Fix & enable type checking for the wemo component.

These type checks, in wemo_device.py, uncovered an mistake with passing an Awaitable to hass.add_job rather than to hass.create_task. Fixed in #62159. As noted in the comment below, though, this wouldn't have caused an issue.

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)
  • Breaking change (fix/feature causing existing functionality to break)
  • 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

  • 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
  • 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:

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.
  • Untested files have been added to .coveragerc.

The integration reached or maintains the following Integration Quality Scale:

  • No score or internal
  • 🥈 Silver
  • 🥇 Gold
  • 🏆 Platinum

To help with the load of incoming pull requests:

@bdraco
Copy link
Copy Markdown
Member

bdraco commented Dec 19, 2021

Also fixed a bug with passing an Awaitable to hass.add_job rather than to hass.create_task, caught by the type checks, in wemo_device.py.

Its probably actually going to reach async_create_task because of https://github.com/home-assistant/core/blob/dev/homeassistant/core.py#L356

hass.create_task is better though since it is less ambiguous and just a touch faster

@bdraco
Copy link
Copy Markdown
Member

bdraco commented Dec 19, 2021

This one has a conflict

@esev esev force-pushed the wemo_strict_typing branch from 8525c18 to 519ca6c Compare December 19, 2021 05:59
@esev esev marked this pull request as ready for review December 19, 2021 06:03
@esev
Copy link
Copy Markdown
Contributor Author

esev commented Dec 19, 2021

This one has a conflict

Rebased.


_LOGGER = logging.getLogger(__name__)

HostPortTuple = Tuple[str, Optional[str]]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is the port really a string?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No it is not. Nice catch, thank you! I missed the cv.port() call. Fixed.

Comment thread homeassistant/components/wemo/__init__.py Outdated
Comment thread homeassistant/components/wemo/switch.py Outdated
def current_power_w(self):
def current_power_w(self) -> float | None:
"""Return the current power usage in W."""
if isinstance(self.wemo, Insight):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Would save some indent if you reversed the condition and return None in the indent, then outdent below

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks! Rearranged this method and the one below.

@bdraco bdraco self-requested a review December 19, 2021 08:13
"""Support for WeMo device discovery."""
from __future__ import annotations

from collections.abc import MutableSet, Sequence
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

MutableSet unused now?

Copy link
Copy Markdown
Contributor Author

@esev esev Dec 19, 2021

Choose a reason for hiding this comment

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

Yes. You're quick! I didn't get e92b075 pushed fast enough. :)

@bdraco
Copy link
Copy Markdown
Member

bdraco commented Dec 19, 2021

Will take another look tomorrow. 3am here so sleep time

async def discover_statics(self):
async def discover_statics(self) -> None:
"""Initialize or Re-Initialize connections to statically configured devices."""
if self._static_config:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If you reverse this condition and return, you can outdent below

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done. 96d9573

Comment thread homeassistant/components/wemo/switch.py Outdated
if standby_state == WEMO_STANDBY:
return STATE_STANDBY
return STATE_UNKNOWN
return ""
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
return ""
return ""

I think you can assert False since this should be unreachable

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thank you. Done. 21f3ea8

@bdraco bdraco merged commit 1318597 into home-assistant:dev Dec 20, 2021
@esev esev deleted the wemo_strict_typing branch December 20, 2021 00:09
Copy link
Copy Markdown
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.

Left a couple of comments, but could leave quite a few more of these. Why are we converting types so much in this PR?

def name(self) -> str:
"""Return the name of the device if any."""
return self.light.name
return str(self.light.name)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This seems odd, why do we convert it to a string here? Is it an int?

def unique_id(self) -> str:
"""Return the ID of this light."""
return self.light.uniqueID
return str(self.light.uniqueID)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Same as above

def brightness(self) -> int:
"""Return the brightness of this light between 0..255."""
return self.light.state.get("level", 255)
return int(self.light.state.get("level", 255))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why do we convert it here? Is it not an int?

"""Return the color temperature of this light in mireds."""
return self.light.state.get("temperature_mireds")
if (temp := self.light.state.get("temperature_mireds")) is not None:
return int(temp)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Wasn't this an int already?

@bdraco
Copy link
Copy Markdown
Member

bdraco commented Dec 20, 2021

I'm pretty sure this is because it looks like pywemo hasn't implemented PEP 561 yet

@frenck
Copy link
Copy Markdown
Member

frenck commented Dec 20, 2021

But in that case, we should cast, not convert...
so if that is the case, please adjust this in a new PR

@esev
Copy link
Copy Markdown
Contributor Author

esev commented Dec 20, 2021

Thanks for catching that. I'll send a new PR to correct this tonight after work.

@github-actions github-actions Bot locked and limited conversation to collaborators Dec 21, 2021
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.

4 participants