2019-02-13 20:21:14 +00:00
|
|
|
"""Support for monitoring a Sense energy sensor device."""
|
2018-11-02 09:13:14 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
from homeassistant.components.binary_sensor import BinarySensorDevice
|
2019-06-01 19:15:28 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
|
|
|
from homeassistant.core import callback
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2019-06-01 19:15:28 +00:00
|
|
|
from . import SENSE_DATA, SENSE_DEVICE_UPDATE
|
2018-11-02 09:13:14 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
BIN_SENSOR_CLASS = 'power'
|
2018-12-01 18:27:21 +00:00
|
|
|
MDI_ICONS = {
|
|
|
|
'ac': 'air-conditioner',
|
|
|
|
'aquarium': 'fish',
|
|
|
|
'car': 'car-electric',
|
|
|
|
'computer': 'desktop-classic',
|
|
|
|
'cup': 'coffee',
|
|
|
|
'dehumidifier': 'water-off',
|
|
|
|
'dishes': 'dishwasher',
|
|
|
|
'drill': 'toolbox',
|
|
|
|
'fan': 'fan',
|
|
|
|
'freezer': 'fridge-top',
|
|
|
|
'fridge': 'fridge-bottom',
|
|
|
|
'game': 'gamepad-variant',
|
|
|
|
'garage': 'garage',
|
|
|
|
'grill': 'stove',
|
|
|
|
'heat': 'fire',
|
|
|
|
'heater': 'radiatior',
|
|
|
|
'humidifier': 'water',
|
|
|
|
'kettle': 'kettle',
|
|
|
|
'leafblower': 'leaf',
|
|
|
|
'lightbulb': 'lightbulb',
|
|
|
|
'media_console': 'set-top-box',
|
|
|
|
'modem': 'router-wireless',
|
|
|
|
'outlet': 'power-socket-us',
|
|
|
|
'papershredder': 'shredder',
|
|
|
|
'printer': 'printer',
|
|
|
|
'pump': 'water-pump',
|
|
|
|
'settings': 'settings',
|
|
|
|
'skillet': 'pot',
|
|
|
|
'smartcamera': 'webcam',
|
|
|
|
'socket': 'power-plug',
|
|
|
|
'sound': 'speaker',
|
|
|
|
'stove': 'stove',
|
|
|
|
'trash': 'trash-can',
|
|
|
|
'tv': 'television',
|
|
|
|
'vacuum': 'robot-vacuum',
|
|
|
|
'washer': 'washing-machine',
|
|
|
|
}
|
2018-11-02 09:13:14 +00:00
|
|
|
|
|
|
|
|
2019-03-12 13:44:53 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities,
|
|
|
|
discovery_info=None):
|
2018-12-01 18:27:21 +00:00
|
|
|
"""Set up the Sense binary sensor."""
|
2019-03-14 10:46:44 +00:00
|
|
|
if discovery_info is None:
|
|
|
|
return
|
2018-11-02 09:13:14 +00:00
|
|
|
data = hass.data[SENSE_DATA]
|
|
|
|
|
2019-03-12 13:44:53 +00:00
|
|
|
sense_devices = await data.get_discovered_device_data()
|
2018-11-13 07:34:11 +00:00
|
|
|
devices = [SenseDevice(data, device) for device in sense_devices
|
|
|
|
if device['tags']['DeviceListAllowed'] == 'true']
|
2019-03-12 13:44:53 +00:00
|
|
|
async_add_entities(devices)
|
2018-11-02 09:13:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
def sense_to_mdi(sense_icon):
|
|
|
|
"""Convert sense icon to mdi icon."""
|
2018-12-01 18:27:21 +00:00
|
|
|
return 'mdi:{}'.format(MDI_ICONS.get(sense_icon, 'power-plug'))
|
2018-11-02 09:13:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SenseDevice(BinarySensorDevice):
|
|
|
|
"""Implementation of a Sense energy device binary sensor."""
|
|
|
|
|
|
|
|
def __init__(self, data, device):
|
2018-12-01 18:27:21 +00:00
|
|
|
"""Initialize the Sense binary sensor."""
|
2018-11-02 09:13:14 +00:00
|
|
|
self._name = device['name']
|
|
|
|
self._id = device['id']
|
|
|
|
self._icon = sense_to_mdi(device['icon'])
|
|
|
|
self._data = data
|
2019-06-01 19:15:28 +00:00
|
|
|
self._undo_dispatch_subscription = None
|
2018-11-02 09:13:14 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if the binary sensor is on."""
|
2019-06-01 19:15:28 +00:00
|
|
|
return self._name in self._data.active_devices
|
2018-11-02 09:13:14 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the binary sensor."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return the id of the binary sensor."""
|
|
|
|
return self._id
|
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Return the icon of the binary sensor."""
|
|
|
|
return self._icon
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Return the device class of the binary sensor."""
|
|
|
|
return BIN_SENSOR_CLASS
|
|
|
|
|
2019-06-01 19:15:28 +00:00
|
|
|
@property
|
|
|
|
def should_poll(self):
|
|
|
|
"""Return the deviceshould not poll for updates."""
|
|
|
|
return False
|
|
|
|
|
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Register callbacks."""
|
|
|
|
@callback
|
|
|
|
def update():
|
|
|
|
"""Update the state."""
|
|
|
|
self.async_schedule_update_ha_state(True)
|
|
|
|
|
|
|
|
self._undo_dispatch_subscription = async_dispatcher_connect(
|
|
|
|
self.hass, SENSE_DEVICE_UPDATE, update)
|
|
|
|
|
|
|
|
async def async_will_remove_from_hass(self):
|
|
|
|
"""Undo subscription."""
|
|
|
|
if self._undo_dispatch_subscription:
|
|
|
|
self._undo_dispatch_subscription()
|