Skip to content

Fix TypeError on round(self.humidity) (fixes #13116)#14174

Merged
balloob merged 2 commits intohome-assistant:devfrom
mvn23:mvn23-humidity-round-fix
Apr 30, 2018
Merged

Fix TypeError on round(self.humidity) (fixes #13116)#14174
balloob merged 2 commits intohome-assistant:devfrom
mvn23:mvn23-humidity-round-fix

Conversation

@mvn23
Copy link
Copy Markdown
Contributor

@mvn23 mvn23 commented Apr 29, 2018

During init, some (most?) platforms postpone the first update for a while. This produces errors in the log as home assistant is trying to round() uninitialised variables. This commit fixes that

Description:

fixes #13116

Checklist:

  • The code change is tested and works locally.
  • Local tests pass with tox.

Some weather platforms postpone the first data fetch for a while on init. As a result round(self.humidity is called before it is assigned a value, producing an error. This is a fix for that.
@homeassistant
Copy link
Copy Markdown
Contributor

Hi @mvn23,

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

Copy link
Copy Markdown
Member

@OttoWinter OttoWinter left a comment

Choose a reason for hiding this comment

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

Nice 👍 Just a quick comment about falsy values.

self.hass, self.temperature, self.temperature_unit,
self.precision),
ATTR_WEATHER_HUMIDITY: round(self.humidity)
ATTR_WEATHER_HUMIDITY: (round(self.humidity) if self.humidity
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.

The if self.humidity check will not work with falsy values like 0.0.

Write something like if self.humidity is not None or, even better, make it an additional check like the other attributes:

data = {
            ATTR_WEATHER_TEMPERATURE: show_temp(
                self.hass, self.temperature, self.temperature_unit,
                self.precision),
        }

        humidity = self.humidity
        if humidity is not None:
            data[ATTR_WEATHER_HUMIDITY] = humidity

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.

You are absolutely right, hadn't thought about that.
Even though humidity is probably never going to be 0, we should definitely take it into account.
Fixed in the commit below.

As per the suggestion from @OttoWinter, rewrite to avoid matching e.g. 0.0 as false.
@balloob balloob merged commit 6e0a3ab into home-assistant:dev Apr 30, 2018
@mvn23 mvn23 deleted the mvn23-humidity-round-fix branch April 30, 2018 22:01
@balloob balloob mentioned this pull request May 11, 2018
@home-assistant home-assistant locked and limited conversation to collaborators Sep 5, 2018
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.

TypeError: type NoneType doesn't define __round__ method on ATTR_WEATHER_HUMIDITY

4 participants