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 has UPnP enabled #153

Closed chemelli74 closed 2 years ago

chemelli74 commented 2 years ago

Add a method to know if the device has UPnP configured or not.

cc @mib1185

chemelli74 commented 2 years ago

The method should not return False in case the service is not available, because this would be an ambiguous result.

True, I started from the point of view of my "needs" for such method and not a generic one.

Also the method returns None in case that there is no "NewEnable" entry in the dictionary provided by call_action(): in a boolean context None would interpreted as False masking the real error.

Indeed, same as above. Can we have also a "upnp_capable" method then ? ;-)

@property
def upnp_capable(self):
    """
    Returns a boolean whether upnp is available on the device.
    """
    return "X_AVM-DE_UPnP1" in self.fc.services

Instead it should be implemented like:

@property
def upnp_enabled(self):
    """
    Returns a boolean whether upnp is enabled or raises a
    FritzServiceError in case the service is not available.
    """
    status = self.fc.call_action("X_AVM-DE_UPnP1", "GetInfo")
    return status["NewEnable"]

The first line will raise a FritzServiceError if the API is not provided and the second line a KeyError in case of a corrupted dictionary. Both errors are unlikely, however this is a clean separation and the return value is just about the upnp-setting.

Will update code.

Simone

kbr commented 2 years ago

Can we have also a "upnp_capable" method then ? ;-)

Well, makes no real sense because this could be a generic one-liner on application level ;)

def has_service(fc, service_name):
    return service_name in fc.services

Library methods should not be trivial ones but make things easier which are harder otherwise. And if it is just because not to remember a service-name with the according action. A lot of properties in the library are about this. But in general a method should make it in the library if some non-application-specific pre- or post-processing is required for a call_action() call.