Whistle / Pettracker.com integration

Here’s a hack to make it work. I couldn’t find any way to determine if the pet was outside the zone, so I’m checking the notifications. Make sure you have location notifications turned on at Whistle.

Also, modify PASSWORD, EMAIL, and PETNAME appropriately.

In /etc/hass/scripts/pet_is_home

#!/usr/bin/php
<?php
// API reference from:
// http://jared.wuntu.org/whistle-dog-activity-monitor-undocumented-api/

$data = array("password" => "PASSWORD", "email" => "EMAIL", "app_id" => "com.whistle.WhistleApp");                                                                    
$data_string = json_encode($data);                                                                                   
$ch = curl_init('https://app.whistle.com/api/tokens.json');                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string))                                                                       
);                                                                                                                                                                                                                                        
$result = json_decode(curl_exec($ch));
$token = $result->token;
$ch = curl_init('https://app.whistle.com/api/notifications');                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");                                                                     
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'X-Whistle-AuthToken:' . $token)
);                                                                                                                   
$notifications = json_decode(curl_exec($ch));
foreach ($notifications as $notification) {
   foreach($notification as $item) {
      if (preg_match('/PETNAME has left/i', $item->message)) {
         echo 'not_home';
         exit;
      } else if (preg_match('/PETNAME is back/i', $item->message)) {
         echo 'home';
         exit;
      } else if (preg_match('/PETNAME is now back/i', $item->message)) {
         echo 'home';
         exit;
      }
   }
}
?>

In sensors.yaml:

##################################################################
## Pet is home
##################################################################
- platform: command_line
  scan_interval: 3600
  name: "Pet is Home"
  command: "/etc/hass/scripts/pet_is_home"
  value_template: '{{ value }}'