-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
netgear_reboot.py
50 lines (34 loc) · 1.37 KB
/
netgear_reboot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""
REboot netgear router when nobody is home
"""
import logging
import datetime
from homeassistant.helpers.event import track_time_change
from homeassistant.util import dt as dt_util
from pynetgear import Netgear
# The domain of your component. Should be equal to the name of your component.
DOMAIN = "netgear_reboot"
# List of component names (string) your component depends upon.
# We depend on group because group will be loaded after all the components that
# initialize devices have been setup.
DEPENDENCIES = ['group', ]
# Shortcut for the logger
_LOGGER = logging.getLogger(__name__)
def setup(hass, config):
"""Setup component."""
last_trigger = dt_util.now()
netgear = Netgear(password='PSW')
def check_netgear(_=None):
now = dt_util.now()
if hass.states.get('group.tracker').state == 'home' or (now - hass.states.get('group.tracker').last_updated) < datetime.timedelta(hours=1):
_LOGGER.error("Netgear reboot home")
return
nonlocal last_trigger
if now - last_trigger < datetime.timedelta(hours=30):
_LOGGER.error("Netgear reboot %s", now - last_trigger)
return
res = netgear.reboot()
_LOGGER.error("Netgear reboot %s", res)
last_trigger = dt_util.now()
track_time_change(hass, check_netgear, second=22, minute=6, hour=[2, 4, 9, 13])
return True