kbr / fritzconnection

Python-Tool to communicate with the AVM Fritz!Box by the TR-064 protocol and the AHA-HTTP-Interface
MIT License
303 stars 59 forks source link

Add `updatecheck` property #168

Closed cdce8p closed 1 year ago

cdce8p commented 1 year ago

Add method to get and parse box_info. In general it returns similar information to DeviceInfo. However, the endpoint doesn't require any authorization.

For contrast DeviceInfo does require the Fritz!Box Configuration permission which is basically admin access. In Home Assistant we currently use DeviceInfo to get the Serial number. Understandably, some users aren't comfortable with giving admin access to a third party application.

References

kbr commented 1 year ago

Thank you very much for this pull-request, I was not aware of this url. It could also make the cache verification faster. Indeed the url seems to be designed for this kind of usage.

I would like to add this feature. However, based on already existing and tested functions, the code could be implemented in a more compact way, by just adding a couple of lines to the core.fritzconnection.py file.

At first the URI should get defined as a constant on line 41, so it is grouped with the other URIs for the router interface descriptions:

...
FRITZ_BOXINFO_FILE =  "jason_boxinfo.xml"
FRITZ_IGD_DESC_FILE = "igddesc.xml"
...

Line 281 would be the proper location to insert a new property updatecheck. I think this is a better name, because the content indicates this usecase and get_device_info is already used in fritzstatus, and get_box_info sounds like a synonyme of the latter. I suggest an implementation like:

@property
def updatecheck(self):
    """
    Dictionary with information about the hard- and software version of
    the device (http://fritz.box/jason_boxinfo.xml).
    """
    xml_data = get_xml_root(
        f"{self.address}/{FRITZ_BOXINFO_URL}",
        timeout=self.timeout,
        session=self.session
    )
    return {localname(elem): elem.text for elem in xml_data}

Last but not least the utility functions must get imported. So line 27 should get changed to:

from .utils import (
    get_bool_env,
    get_xml_root,
    localname
)
cdce8p commented 1 year ago

Thanks for the detailed suggestions. I've implemented them in the last commit. For now I removed the test case, let me know if I should add it back (and where it should be located).

kbr commented 1 year ago

I'm not sure whether this needs a test. But if there is one, it should be in tests/test_fritzconnection.py.