bremor/bureau_of_meteorology

NameError: name 'timezone' is not defined

Closed this issue ยท 14 comments

This error originated from a custom integration.

Logger: homeassistant
Source: custom_components/bureau_of_meteorology/sensor.py:340 
integration: Bureau of Meteorology (documentation, issues) 
First occurred: July 14, 2024 at 5:07:31 PM (302 occurrences) 
Last logged: 6:17:43 PM

Error doing job: Task exception was never retrieved (None)
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 255, in _handle_refresh_interval
    await self._async_refresh(log_failures=True, scheduled=True)
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 411, in _async_refresh
    self.async_update_listeners()
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 165, in async_update_listeners
    update_callback()
  File "/config/custom_components/bureau_of_meteorology/sensor.py", line 203, in _update_callback
    self.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1007, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1133, in _async_write_ha_state
    state, attr, capabilities, shadowed_attr = self.__async_calculate_state()
                                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1068, in __async_calculate_state
    state = self._stringify_state(available)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1013, in _stringify_state
    if (state := self.state) is None:
                 ^^^^^^^^^^
  File "/config/custom_components/bureau_of_meteorology/sensor.py", line 340, in state
    utc = timezone.utc
          ^^^^^^^^
NameError: name 'timezone' is not defined

Potentially related to #230

Same error here and the temperature no longer updates unless I reload the integration.

2024-07-20 08:41:12.690 ERROR (MainThread) [homeassistant] Error doing job: Task exception was never retrieved (None)
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 255, in _handle_refresh_interval
await self._async_refresh(log_failures=True, scheduled=True)
File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 411, in _async_refresh
self.async_update_listeners()
File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 165, in async_update_listeners
update_callback()
File "/config/custom_components/bureau_of_meteorology/sensor.py", line 203, in _update_callback
self.async_write_ha_state()
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1000, in async_write_ha_state
self._async_write_ha_state()
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1126, in _async_write_ha_state
state, attr, capabilities, shadowed_attr = self.__async_calculate_state()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1061, in __async_calculate_state
state = self._stringify_state(available)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1006, in _stringify_state
if (state := self.state) is None:
^^^^^^^^^^
File "/config/custom_components/bureau_of_meteorology/sensor.py", line 340, in state
utc = timezone.utc
^^^^^^^^
NameError: name 'timezone' is not defined

I also am getting this error.

Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1530, in _async_process_registry_update_or_remove
self.async_write_ha_state()
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1007, in async_write_ha_state
self._async_write_ha_state()
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1133, in _async_write_ha_state
state, attr, capabilities, shadowed_attr = self.__async_calculate_state()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1068, in __async_calculate_state
state = self._stringify_state(available)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1013, in _stringify_state
if (state := self.state) is None:
^^^^^^^^^^
File "/config/custom_components/bureau_of_meteorology/sensor.py", line 340, in state
utc = timezone.utc
^^^^^^^^
NameError: name 'timezone' is not defined

I changed the following in sensor.py and it seems to have fixed this for me.

At the top of the file, added as line 3:
import pytz

Then down at the line that is triggering the error, I changed:
utc = timezone.utc
local = timezone(self.collector.locations_data["data"]["timezone"])

to

utc = pytz.utc
local = pytz.timezone(self.collector.locations_data["data"]["timezone"])

After restarting Home Assistant, the error is gone, and my sensor updates as normal.

I changed the following in sensor.py and it seems to have fixed this for me.

At the top of the file, added as line 3: import pytz

Then down at the line that is triggering the error, I changed: utc = timezone.utc local = timezone(self.collector.locations_data["data"]["timezone"])

to

utc = pytz.utc local = pytz.timezone(self.collector.locations_data["data"]["timezone"])

After restarting Home Assistant, the error is gone, and my sensor updates as normal.

Thanks @GracieSW584 that has also removed the error for me. How often is your sensor updating now after the change? And can I confirm the sensor you are referring to is the local weather forecast, not the city forecast?

I changed the following in sensor.py and it seems to have fixed this for me.
At the top of the file, added as line 3: import pytz
Then down at the line that is triggering the error, I changed: utc = timezone.utc local = timezone(self.collector.locations_data["data"]["timezone"])
to
utc = pytz.utc local = pytz.timezone(self.collector.locations_data["data"]["timezone"])
After restarting Home Assistant, the error is gone, and my sensor updates as normal.

Thanks @GracieSW584 that has also removed the error for me. How often is your sensor updating now after the change? And can I confirm the sensor you are referring to is the local weather forecast, not the city forecast?

Can also confirm the local weather conditions are updating every 10 minutes with this change.

Looks like e31cc0b update to sensor.py specifically removed pytz as per the error reported in #222. However the change made in e31cc0b has broken the automatic update of the real-time conditions.

@Makin-Things do we have a workable solution do you think?

Thanks @GracieSW584 that has also removed the error for me. How often is your sensor updating now after the change? And can I confirm the sensor you are referring to is the local weather forecast, not the city forecast?

My current temperature is back to updating every 5 minutes as it was before things broke, and now everything else seems to be ticking along at the expected rate too.

The sensor I was referring to in particular was the sensor.locationname_outdoor_temperature, not the forecast in particular. But it all seems to have come good with that tweak at least!

Pull request created, thanks for mentioning it.

Thanks @GracieSW584 that has also removed the error for me. How often is your sensor updating now after the change? And can I confirm the sensor you are referring to is the local weather forecast, not the city forecast?

My current temperature is back to updating every 5 minutes as it was before things broke, and now everything else seems to be ticking along at the expected rate too.

The sensor I was referring to in particular was the sensor.locationname_outdoor_temperature, not the forecast in particular. But it all seems to have come good with that tweak at least!

Pull request created, thanks for mentioning it.

Good stuff @GracieSW584 that's some of your best work

I am trying to replicate this error but am not seeing it. I don't think @GracieSW584 PR can be used as pytz was removed due to issues it caused with HA (hence why it was changed out).

@dbiczo are you seeing this or have any insight since you provided the PR for the timezone warnings.

@dbiczo are you seeing this or have any insight since you provided the PR for the timezone warnings.

The warning only shows if collecting "UV forecast summary" which I wasn't.
I am testing a fix without using pytz which I think is problematic.

I have just create a pre-release 1.3.3 that should resolve the issue. Thanks @dbiczo
I have been unable to test it thoroughly as I live in a location that currently doesn't get many UV forecasts.
Can other please test and report back, thanks.

I have just create a pre-release 1.3.3 that should resolve the issue. Thanks @dbiczo I have been unable to test it thoroughly as I live in a location that currently doesn't get many UV forecasts. Can other please test and report back, thanks.

It looks good to me. I wasn't having issues with the UV forecasts in particular, but with the sensor updating at all, and this patch seems to have fixed that now. Thanks. I'll close the pull request that I created now.

Thanks @GracieSW584 for checking it out. I have now made this the latest release (ie. no longer beta).