coreGreenberet / homematicip-samples

A collection of samples and simple scripts for the homematicip rest api wrapper
GNU General Public License v3.0
12 stars 4 forks source link

'CheckPresenceOnPing' keeps reenabling security zones when 'no one is at home' #4

Open user45876 opened 5 years ago

user45876 commented 5 years ago

When setting up CheckPresenceOnPing, it keeps reenabling the security zones, when nobody is at home (no device is reachable). This can be problematic, as this causes a popup on every configured client and also spams the alarm log.

Expected behaviour would be that on 'noone is home' and already activated security zone, nothing is done.

user45876 commented 5 years ago

Well, without actually knowing what I am doing - this does it for me, AFAICT. Straightened the outputs a little and added a timestamp in front for easier logging.

#!/usr/bin/env python3
import homematicip
from homematicip.home import Home
import ping3
from socket import gaierror
import datetime

config = homematicip.find_and_load_config_file()

SCANABLE_DEVICES = ['192.168.128.101', '192.168.128.108']
ACTIVATE_EXTERNAL_ZONE = True
ACTIVATE_INTERNAL_ZONE = False

DEACTIVATE_ON_PRESENCE = True

def main():
    if config is None:
        print("{} COULD NOT DETECT CONFIG FILE")
        return

    home = Home()
    home.set_auth_token(config.auth_token)
    home.init(config.access_point)

    if not home.get_current_state():
        return
    for ip in SCANABLE_DEVICES:
        try:
            res = ping3.ping(ip)
            if res != None:
                if DEACTIVATE_ON_PRESENCE:
                    for g in home.groups:
                        if isinstance(g, homematicip.group.SecurityZoneGroup) and g.active:
                            print("{} Someone is at home, security zones are active -> deactivating security zones.".format(datetime.datetime.now().strftime("%y%m%d-%H:%M")))
                            home.set_security_zones_activation(False,False)
                            return
                    print("{} Someone is at home, security zones are deavtivated -> do nothing.".format(datetime.datetime.now().strftime("%y%m%d-%H:%M")))
                else:
                    print("{} Someone is at home, security zones are inactive -> do nothing.".format(datetime.datetime.now().strftime("%y%m%d-%H:%M")))
                return
        except gaierror:
            print("Could not resolve {}. Marking it as \"not at home\"".format(ip))

    for g in home.groups:
        if isinstance(g, homematicip.group.SecurityZoneGroup) and g.active:
            print("{} Nobody is at home, security zones are already active -> do nothing.".format(datetime.datetime.now().strftime("%y%m%d-%H:%M")))
            return
    print("{} Nobody is at home, security zones are inactive -> activating security zones.".format(datetime.datetime.now().strftime("%y%m%d-%H:%M")))
    home.set_security_zones_activation(ACTIVATE_INTERNAL_ZONE,ACTIVATE_EXTERNAL_ZONE)

if __name__ == "__main__":
    main()