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

Device info #155

Closed chemelli74 closed 1 year ago

chemelli74 commented 2 years ago

Add a property to get some useful device information.

cc @mib1185

chemelli74 commented 1 year ago

Update to reflect last code changes.

Simone

chemelli74 commented 1 year ago

Klaus, can you be so kind to review (and if ready, merge) this one as well ?

After that, from the HA point of view, we would like to see a new release of the library so we can start working on a deep optimization of the code.

Simone

chemelli74 commented 1 year ago

Kind reminder ;-)

Have a nice summer,

Simone

kbr commented 1 year ago

I know that I have introduced a namedtuple as a return value from the library. But I think this is not a good idea, because different datatypes are now returned depending on the called methods. And all method just wrap the call_action() method in one or the other way – which returns a dict with the argument names as defined by the AVM documentation. To not get confused, the library should keep this as far as possible.

To help to do this I have introduced the ArgumentNamespace class which represents a namespace (similar to a namedtuple) but also behaves like a dictionary. This can help make it easy to deal with the return values from call_action() calls. For example the code for this pull request could rewritten like:

from fritzconnection.lib.fritztools import ArgumentNamespace

    # part of FritzStatus:
    def device_info(self):
        info = self.fc.call_action("DeviceInfo:1", "GetInfo")
        extract = "NewSerialNumber", "NewModelName", "NewSoftwareVersion"
        return ArgumentNamespace(info, extract=extract)

# there is a fritzstatus instance "fs":
info = fs.device_info()

# now it is possible to access the content via attributes:
info.model_name
# or like a dictionary
info['model_name']

Assuming that there is a FritzConnetion instance fc, it could also be more compact:

from fritzconnection.lib.fritztools import ArgumentNamespace

def device_info():
    return ArgumentNamespace(fc.call_action("DeviceInfo1", "GetInfo"))

device_info()

returning a namespace object

namespace(manufacturer_name='AVM',
          manufacturer_o_u_i='00040E',
          model_name='FRITZ!Box 7590',
          description='FRITZ!Box 7590 154.07.29',
          product_class='AVMFB7590',
          serial_number='989BCB2B93B0',
          software_version='154.07.29',
          hardware_version='FRITZ!Box 7590',
          spec_version='1.0',
          provisioning_code='000.044.004.000',
          up_time=9596438,
          device_log='long text here ...')

with all attributes and the user is free to use just a subset like

info = device_info()
info.software_version
kbr commented 1 year ago

Next release will get the implementation (as a method, not a property):

def device_info():
    return ArgumentNamespace(fc.call_action("DeviceInfo1", "GetInfo"))

so the usage is not restricted to a subset of the information.