Skip to content

Commit

Permalink
Reduce complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
Shutgun committed Dec 8, 2020
1 parent 840af2a commit 95c69e6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
18 changes: 10 additions & 8 deletions devolo_plc_api/network/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ def discover_network() -> Dict[str, Device]:

def _add(zeroconf: Zeroconf, service_type: str, name: str, state_change: ServiceStateChange):
"""" Create a device object to each matching device. """
if state_change is ServiceStateChange.Added:
service_info = zeroconf.get_service_info(service_type, name)
if service_info is None:
return
if state_change is not ServiceStateChange.Added:
return

info = Device.info_from_service(service_info)
if info is None or info["properties"]["MT"] in ("2600", "2601"):
return # Don't react on devolo Home Control central units
service_info = zeroconf.get_service_info(service_type, name)
if service_info is None:
return

_devices[info["properties"]["SN"]] = Device(ip=info["address"], deviceapi=info, zeroconf_instance=zeroconf)
info = Device.info_from_service(service_info)
if info is None or info["properties"]["MT"] in ("2600", "2601"):
return # Don't react on devolo Home Control central units

_devices[info["properties"]["SN"]] = Device(ip=info["address"], deviceapi=info, zeroconf_instance=zeroconf)
7 changes: 7 additions & 0 deletions tests/test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ def test__add(self, mocker):
network._add(Zeroconf(), "_dvl-deviceapi._tcp.local.", "_dvl-deviceapi._tcp.local.", ServiceStateChange.Added)
assert "1234567890123456" in network._devices

def test__add_wrong_state(self, mocker):
with patch("zeroconf.Zeroconf.get_service_info", return_value="service_info"), \
patch("devolo_plc_api.device.Device.info_from_service", return_value=None):
spy_device = mocker.spy(Device, "__init__")
network._add(Zeroconf(), "_dvl-deviceapi._tcp.local.", "_dvl-deviceapi._tcp.local.", ServiceStateChange.Removed)
assert spy_device.call_count == 0

def test__add_no_device(self, mocker):
with patch("zeroconf.Zeroconf.get_service_info", return_value=None):
spy_info = mocker.spy(Device, "info_from_service")
Expand Down

0 comments on commit 95c69e6

Please sign in to comment.