Continuing the discussion from Integration for Home Automation:
I went ahead and built out my example of using Sense’s on/off detection to generate a voice announcement via Home Assistant. I wrote a simple custom component for Home Assistant (leveraging this Pyhton project), and it works!
For those who are familiar with Home Assistant, here’s the required config excerpt:
binary_sensor:
- platform: sense
email: !secret email
password: !secret sense_password
This will generate a binary (on/off) sensor for each device in your device list (i.e. merged devices are a single sensor). You can then use the state transitions of these binary sensors to trigger automations, including calling a text-to-speech (TTS) service with whatever message you would like.
For those who are interested, here’s the code for the custom component:
"""
Support for the Sense power monitor.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/binary_sensor.sense/
"""
import datetime
import logging
import requests
import voluptuous as vol
from sense_energy import Senseable
from homeassistant.components.binary_sensor import PLATFORM_SCHEMA
from homeassistant.const import (CONF_NAME, CONF_EMAIL, CONF_PASSWORD,
STATE_ON, STATE_OFF)
from homeassistant.helpers.entity import Entity
from homeassistant.util import Throttle
import homeassistant.helpers.config_validation as cv
REQUIREMENTS = ['sense_energy==0.3.1']
_LOGGER = logging.getLogger(__name__)
MIN_TIME_BETWEEN_UPDATES = datetime.timedelta(seconds=60)
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Optional(CONF_NAME): cv.string,
vol.Required(CONF_EMAIL): cv.string,
vol.Required(CONF_PASSWORD): cv.string
})
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up Sense device binary sensors."""
sense_object = Senseable(config.get(CONF_EMAIL),
config.get(CONF_PASSWORD))
sense_object.get_realtime()
device_details = sense_object.get_discovered_device_data()
device_list = []
for d in device_details:
if d['tags']['DeviceListAllowed'] == 'true':
device_list.append(d['name'])
add_devices([SenseBinarySensor(sense_object, d)
for d in device_list])
class SenseBinarySensor(Entity):
"""Implementation of a Sense binary sensor."""
def __init__(self, sense_object, device_name):
"""Initialize the sensor."""
self.device_name = device_name
self.sense_object = sense_object
@property
def name(self):
"""Return the name of the sensor."""
return self.device_name
@property
def state(self):
"""Return the state of the sensor."""
if self.device_name in self.sense_object.active_devices:
return STATE_ON
else:
return STATE_OFF
@Throttle(MIN_TIME_BETWEEN_UPDATES)
def update(self):
"""Update sensor data."""
self.sense_object.get_realtime()
I’ll submit a pull request to have this merged into Home Assistant as a core component, but in the meantime please let me know if you find it useful.