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

#148 Wake on LAN via CLI #149

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ include
lib_
.idea
x_*
.venv
73 changes: 73 additions & 0 deletions fritzconnection/cli/fritzwol.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
"""
fritzwol.py

Module to wake up a single host via the Fritzbox built-in mechanism.
This can be helpful if the host to be woken up is in a different
broadcast domain/ subnet than the client which tries to wake up.
CLI interface.

This module is part of the FritzConnection package.
https://github.com/kbr/fritzconnection
License: MIT (https://opensource.org/licenses/MIT)
Author: Maik Töpfer
"""

from fritzconnection.core.exceptions import FritzConnectionException
from fritzconnection.core.fritzconnection import FritzConnection
from ..lib.fritzhosts import FritzHosts
from . utils import get_cli_arguments, get_instance, print_header


class WakeOnLan:
samba2 marked this conversation as resolved.
Show resolved Hide resolved
def __init__(self, fc, fh):
self._fc = fc
samba2 marked this conversation as resolved.
Show resolved Hide resolved
self._fh = fh

def wakeup(self, host_name):
print(f"Waking up host '{host_name}'...")
samba2 marked this conversation as resolved.
Show resolved Hide resolved
mac = self._get_mac_address(host_name)
self._wakeup_host(mac)
print("Done")

def _get_mac_address(self, host_name):
mac = [host['mac']
samba2 marked this conversation as resolved.
Show resolved Hide resolved
for host in self._fh.get_hosts_info() if host['name'] == host_name]
if not mac:
raise WakeOnLanException(
f"Host '{host_name}' is unknown at Fritzbox.")
return mac[0]

def _wakeup_host(self, mac):
try:
self._fc.call_action(
'Hosts1',
'X_AVM-DE_WakeOnLANByMACAddress',
NewMACAddress=mac)
except FritzConnectionException as e:
raise WakeOnLanException(f"Error sending Wake on LAN command: {e}")


class WakeOnLanException(Exception):
samba2 marked this conversation as resolved.
Show resolved Hide resolved
pass


def _add_arguments(parser):
parser.add_argument('host', help='name of host to be woken up')


def main():
args = get_cli_arguments(_add_arguments)
if not args.password:
print('Exit: password required.')
else:
fc = get_instance(FritzConnection, args)
print_header(fc)
try:
WakeOnLan(fc, FritzHosts(fc)).wakeup(args.host)
except WakeOnLanException as e:
print(e)
exit(1)
samba2 marked this conversation as resolved.
Show resolved Hide resolved


if __name__ == '__main__':
main()
41 changes: 41 additions & 0 deletions fritzconnection/tests/cli/test_fritzwol.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import pytest

from fritzconnection.cli.fritzwol import WakeOnLan, WakeOnLanException
from unittest.mock import Mock

from fritzconnection.core.exceptions import FritzConnectionException


@pytest.fixture
def fritz_connection():
return Mock()


@pytest.fixture
def fritz_hosts():
mock = Mock()
mock.get_hosts_info.return_value = [{'name': 'someHost',
'mac': '00:00:00:00:00:00'}]
return mock


def test_wol_is_invoked(fritz_connection, fritz_hosts):
WakeOnLan(fritz_connection, fritz_hosts).wakeup("someHost")
fritz_hosts.get_hosts_info.assert_called_once()
fritz_connection.call_action.assert_called_once()


def test_host_not_found(fritz_connection, fritz_hosts):
with pytest.raises(WakeOnLanException):
WakeOnLan(fritz_connection, fritz_hosts).wakeup("unknownHost")

fritz_connection.assert_not_called()


def test_call_action_causes_exception(fritz_connection, fritz_hosts):
fritz_connection.call_action.side_effect = FritzConnectionException

with pytest.raises(WakeOnLanException):
WakeOnLan(fritz_connection, fritz_hosts).wakeup("somehost")

fritz_hosts.get_hosts_info.assert_called_once()
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def get_version():
"fritzphonebook = fritzconnection.cli.fritzphonebook:main",
"fritzstatus = fritzconnection.cli.fritzstatus:main",
"fritzwlan = fritzconnection.cli.fritzwlan:main",
"fritzwol = fritzconnection.cli.fritzwol:main",
]
},
)