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

onvif stopped working in 2021.6.z #51530

Closed
ohadlevy opened this issue Jun 5, 2021 · 13 comments · Fixed by #51620
Closed

onvif stopped working in 2021.6.z #51530

ohadlevy opened this issue Jun 5, 2021 · 13 comments · Fixed by #51620
Assignees

Comments

@ohadlevy
Copy link
Contributor

ohadlevy commented Jun 5, 2021

The problem

The camera is no longer found, error out with the following

Logger: homeassistant.config_entries
Source: util/dt.py:54
First occurred: 9:58:24 PM (1 occurrences)
Last logged: 9:58:24 PM

Error setting up entry garage - fa7d02c72d5c for onvif
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/config_entries.py", line 293, in async_setup
    result = await component.async_setup_entry(hass, self)  # type: ignore
  File "/usr/src/homeassistant/homeassistant/components/onvif/__init__.py", line 72, in async_setup_entry
    if not await device.async_setup():
  File "/usr/src/homeassistant/homeassistant/components/onvif/device.py", line 98, in async_setup
    await self.async_check_date_and_time()
  File "/usr/src/homeassistant/homeassistant/components/onvif/device.py", line 172, in async_check_date_and_time
    dt_util.get_time_zone(device_time.TimeZone)
  File "/usr/src/homeassistant/homeassistant/util/dt.py", line 54, in get_time_zone
    return cast(dt.tzinfo, zoneinfo.ZoneInfo(time_zone_str))
  File "/usr/local/lib/python3.8/site-packages/backports/zoneinfo/_tzpath.py", line 95, in find_tzfile
    _validate_tzfile_path(key)
  File "/usr/local/lib/python3.8/site-packages/backports/zoneinfo/_tzpath.py", line 108, in _validate_tzfile_path
    if os.path.isabs(path):
  File "/usr/local/lib/python3.8/posixpath.py", line 62, in isabs
    s = os.fspath(s)
TypeError: expected str, bytes or os.PathLike object, not NoneType

What is version of Home Assistant Core has the issue?

core-2021.6

What was the last working version of Home Assistant Core?

core-2021.5

What type of installation are you running?

Home Assistant Supervised

Integration causing the issue

onvif

Link to integration documentation on our website

No response

Example YAML snippet

No response

Anything in the logs that might be useful for us?

Logger: homeassistant.config_entries
Source: util/dt.py:54
First occurred: 9:58:24 PM (1 occurrences)
Last logged: 9:58:24 PM

Error setting up entry garage - fa7d02c72d5c for onvif
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/config_entries.py", line 293, in async_setup
    result = await component.async_setup_entry(hass, self)  # type: ignore
  File "/usr/src/homeassistant/homeassistant/components/onvif/__init__.py", line 72, in async_setup_entry
    if not await device.async_setup():
  File "/usr/src/homeassistant/homeassistant/components/onvif/device.py", line 98, in async_setup
    await self.async_check_date_and_time()
  File "/usr/src/homeassistant/homeassistant/components/onvif/device.py", line 172, in async_check_date_and_time
    dt_util.get_time_zone(device_time.TimeZone)
  File "/usr/src/homeassistant/homeassistant/util/dt.py", line 54, in get_time_zone
    return cast(dt.tzinfo, zoneinfo.ZoneInfo(time_zone_str))
  File "/usr/local/lib/python3.8/site-packages/backports/zoneinfo/_tzpath.py", line 95, in find_tzfile
    _validate_tzfile_path(key)
  File "/usr/local/lib/python3.8/site-packages/backports/zoneinfo/_tzpath.py", line 108, in _validate_tzfile_path
    if os.path.isabs(path):
  File "/usr/local/lib/python3.8/posixpath.py", line 62, in isabs
    s = os.fspath(s)
TypeError: expected str, bytes or os.PathLike object, not NoneType

Additional information

No response

@djpremier
Copy link
Contributor

Possible fix:

# homeassistant/components/onvif/device.py - L172

dt_util.get_time_zone(device_time.TimeZone or dt_util.DEFAULT_TIME_ZONE)

@bdraco
Copy link
Member

bdraco commented Jun 6, 2021

Do you have the tzdata package installed ?

@ohadlevy
Copy link
Contributor Author

ohadlevy commented Jun 6, 2021 via email

@pszafer
Copy link
Contributor

pszafer commented Jun 7, 2021

@bdraco do you mean this tzdata? https://pypi.org/project/tzdata/ or system package?

Onvif component fail on Arch Linux, Alpine and Docker container, but it doesn't install tzdata from pypi.

@bdraco
Copy link
Member

bdraco commented Jun 7, 2021

@pszafer
Copy link
Contributor

pszafer commented Jun 7, 2021

Thanks @bdraco . I missed that. How it is not breaking change I can't understand...

@pszafer
Copy link
Contributor

pszafer commented Jun 7, 2021

ok, it's not tzdata which is missing, but bad json returned by Onvif device itself:

{                                                                                                             
    'DateTimeType': 'NTP',                                                                                             
    'DaylightSavings': False,                                                                                          
    'TimeZone': None,             
    'UTCDateTime': None,                                                                                               
    'LocalDateTime': {                                                                                                 
        'Time': {                                                                                                      
            'Hour': 21,                 
            'Minute': 21,                                                                                              
            'Second': 27                                  
        },                                                                                                             
        'Date': {                       
            'Year': 2021,                                                                                              
            'Month': 6,
            'Day': 7                                                                                                   
        }           
    },                                                                                                                 
    'Extension': None,                         
    '_attr_1': None                                                                                                    
}     

@pszafer
Copy link
Contributor

pszafer commented Jun 8, 2021

more info:
if HA init zoneinfo.ZoneInfo(None) or zoneinfo.ZoneInfo('None') then python is crashing:

    return cast(dt.tzinfo, zoneinfo.ZoneInfo(time_zone_str)) 
  File "/usr/lib/python3.9/zoneinfo/_tzpath.py", line 67, in find_tzfile
    _validate_tzfile_path(key)
  File "/usr/lib/python3.9/zoneinfo/_tzpath.py", line 81, in _validate_tzfile_path
    if os.path.isabs(path):
  File "/usr/lib/python3.9/posixpath.py", line 62, in isabs 
    s = os.fspath(s)

IMO this is bug in _validate_tzfile_path, it should raise ValueError on None, not crash. What do you think? Should I post bug in python3.9 issue tracker?
In the meantime I think I can make PR for this line

tzone = (
dt_util.get_time_zone(device_time.TimeZone)
or dt_util.DEFAULT_TIME_ZONE
)

to something like this:

def get_tzone():
  if not device_time.TimeZone:
    return dt_util.DEFAULT_TIME_ZONE
  return (dt_util.get_time_zone(device_time.TimeZone) or dt_util.DEFAULT_TIME_ZONE)
  

tzone = get_tzone()

so onvif module would still work.

@andbad
Copy link

andbad commented Jun 8, 2021

more info:
if HA init zoneinfo.ZoneInfo(None) or zoneinfo.ZoneInfo('None') then python is crashing:

    return cast(dt.tzinfo, zoneinfo.ZoneInfo(time_zone_str)) 
  File "/usr/lib/python3.9/zoneinfo/_tzpath.py", line 67, in find_tzfile
    _validate_tzfile_path(key)
  File "/usr/lib/python3.9/zoneinfo/_tzpath.py", line 81, in _validate_tzfile_path
    if os.path.isabs(path):
  File "/usr/lib/python3.9/posixpath.py", line 62, in isabs 
    s = os.fspath(s)

IMO this is bug in _validate_tzfile_path, it should raise ValueError on None, not crash. What do you think? Should I post bug in python3.9 issue tracker?
In the meantime I think I can make PR for this line

tzone = (
dt_util.get_time_zone(device_time.TimeZone)
or dt_util.DEFAULT_TIME_ZONE
)

to something like this:

def get_tzone():
  if not device_time.TimeZone:
    return dt_util.DEFAULT_TIME_ZONE
  return (dt_util.get_time_zone(device_time.TimeZone) or dt_util.DEFAULT_TIME_ZONE)
  

tzone = get_tzone()

so onvif module would still work.

This works for me.

By(t)e

@djpremier
Copy link
Contributor

Possible fix:

# homeassistant/components/onvif/device.py - L172

dt_util.get_time_zone(device_time.TimeZone or dt_util.DEFAULT_TIME_ZONE)

This one is "cleaner", doesn't it work?

@pszafer
Copy link
Contributor

pszafer commented Jun 8, 2021

what if timezone would be set for as some garbage string eg Moon. get timezone would fail.

dt_util.get_time_zone(device_time.TimeZone or dt_util.DEFAULT_TIME_ZONE) or dt_util.DEFAULT_TIME_ZONE

@djpremier
Copy link
Contributor

what if timezone would be set for as some garbage string eg Moon. get timezone would fail.

dt_util.get_time_zone(device_time.TimeZone or dt_util.DEFAULT_TIME_ZONE) or dt_util.DEFAULT_TIME_ZONE

But it wouldn't be a wrong event, don't you think? Because there is no logic in accepting an incorrect time zone.

That's what I think, as much as it's good to prevent the code from falling into error, accepting a value that isn't correct I believe it's not a good alternative.

@pszafer
Copy link
Contributor

pszafer commented Jun 8, 2021

@djpremier I decided to fix it in dt_util. No need to change anything in onvif itself.

@github-actions github-actions bot locked and limited conversation to collaborators Jul 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants