Add usage sensors for each device sense detects that show powe… (#32206)

* init

* const

* update docs string

* update docs string

* restore binary sensors

* Restore binary sensors

* match name

* pylint

* Fix bug in conf migration

* Fix refactoring error

* Address review items

Imporve performance

* Fix devices never turning back off
This commit is contained in:
J. Nick Koston 2020-02-27 19:23:47 -10:00 committed by GitHub
parent 3b17e570df
commit 0364cd8db5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 357 additions and 111 deletions

View file

@ -2,69 +2,36 @@
import logging
from homeassistant.components.binary_sensor import BinarySensorDevice
from homeassistant.const import DEVICE_CLASS_POWER
from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_registry import async_get_registry
from .const import DOMAIN, SENSE_DATA, SENSE_DEVICE_UPDATE
from .const import (
DOMAIN,
MDI_ICONS,
SENSE_DATA,
SENSE_DEVICE_UPDATE,
SENSE_DEVICES_DATA,
SENSE_DISCOVERED_DEVICES_DATA,
)
_LOGGER = logging.getLogger(__name__)
ATTR_WATTS = "watts"
DEVICE_ID_SOLAR = "solar"
BIN_SENSOR_CLASS = "power"
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",
"solar_alt": "solar-power",
"sound": "speaker",
"stove": "stove",
"trash": "trash-can",
"tv": "television",
"vacuum": "robot-vacuum",
"washer": "washing-machine",
}
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up the Sense binary sensor."""
data = hass.data[DOMAIN][config_entry.entry_id][SENSE_DATA]
sense_devices_data = hass.data[DOMAIN][config_entry.entry_id][SENSE_DEVICES_DATA]
sense_monitor_id = data.sense_monitor_id
sense_devices = await data.get_discovered_device_data()
sense_devices = hass.data[DOMAIN][config_entry.entry_id][
SENSE_DISCOVERED_DEVICES_DATA
]
devices = [
SenseDevice(data, device, sense_monitor_id)
SenseDevice(sense_devices_data, device, sense_monitor_id)
for device in sense_devices
if device["id"] == DEVICE_ID_SOLAR
or device["tags"]["DeviceListAllowed"] == "true"
if device["tags"]["DeviceListAllowed"] == "true"
]
await _migrate_old_unique_ids(hass, devices)
@ -96,20 +63,27 @@ def sense_to_mdi(sense_icon):
class SenseDevice(BinarySensorDevice):
"""Implementation of a Sense energy device binary sensor."""
def __init__(self, data, device, sense_monitor_id):
def __init__(self, sense_devices_data, device, sense_monitor_id):
"""Initialize the Sense binary sensor."""
self._name = device["name"]
self._id = device["id"]
self._sense_monitor_id = sense_monitor_id
self._unique_id = f"{sense_monitor_id}-{self._id}"
self._icon = sense_to_mdi(device["icon"])
self._data = data
self._sense_devices_data = sense_devices_data
self._undo_dispatch_subscription = None
self._state = None
self._available = False
@property
def is_on(self):
"""Return true if the binary sensor is on."""
return self._name in self._data.active_devices
return self._state
@property
def available(self):
"""Return the availability of the binary sensor."""
return self._available
@property
def name(self):
@ -134,7 +108,7 @@ class SenseDevice(BinarySensorDevice):
@property
def device_class(self):
"""Return the device class of the binary sensor."""
return BIN_SENSOR_CLASS
return DEVICE_CLASS_POWER
@property
def should_poll(self):
@ -143,17 +117,20 @@ class SenseDevice(BinarySensorDevice):
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, f"{SENSE_DEVICE_UPDATE}-{self._sense_monitor_id}", update
self.hass,
f"{SENSE_DEVICE_UPDATE}-{self._sense_monitor_id}",
self._async_update_from_data,
)
async def async_will_remove_from_hass(self):
"""Undo subscription."""
if self._undo_dispatch_subscription:
self._undo_dispatch_subscription()
@callback
def _async_update_from_data(self):
"""Get the latest data, update state. Must not do I/O."""
self._available = True
self._state = bool(self._sense_devices_data.get_device_by_id(self._id))
self.async_write_ha_state()