forked from ikalchev/HAP-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShutdownSwitch.py
41 lines (31 loc) · 1.3 KB
/
ShutdownSwitch.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
"""Provides a switch accessory that executes sudo shutdown -h.
This allows you to halt a Raspberry Pi and then plug it off safely.
NOTE: For this to work, you need to allow passwordless /sbin/shutdown to
whichever user is running HAP-python. For example, you can do:
$ sudo visudo
$ # add the line "hap-user ALL=NOPASSWD: /sbin/shutdown"
"""
import os
import logging
from pyhap.accessory import Accessory
from pyhap.const import CATEGORY_SWITCH
import pyhap.loader as loader
logger = logging.getLogger(__name__)
class ShutdownSwitch(Accessory):
"""A switch accessory that executes sudo shutdown."""
category = CATEGORY_SWITCH
def __init__(self, *args, **kwargs):
"""Initialise and set a shutdown callback to the On characteristic."""
super().__init__(*args, **kwargs)
on_char = self.get_service("Switch")\
.get_characteristic("On")
on_char.setter_callback = self.execute_shutdown
def _set_services(self):
"""Add the Switch service."""
super()._set_services()
service_loader = loader.get_serv_loader()
self.add_service(service_loader.get_service("Switch"))
def execute_shutdown(self, _value):
"""Execute shutdown -h."""
logger.info("Executing shutdown command.")
os.system("sudo shutdown -h now")