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
|
|
|
|
|
2020-04-23 19:57:07 +00:00
|
|
|
from homeassistant.components.binary_sensor import BinarySensorEntity
|
2020-04-04 20:51:00 +00:00
|
|
|
from homeassistant.const import ATTR_ATTRIBUTION, DEVICE_CLASS_POWER
|
2019-06-01 19:15:28 +00:00
|
|
|
from homeassistant.core import callback
|
2019-12-01 05:35:45 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2020-02-25 23:37:41 +00:00
|
|
|
from homeassistant.helpers.entity_registry import async_get_registry
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2020-02-28 05:23:47 +00:00
|
|
|
from .const import (
|
2020-04-04 20:51:00 +00:00
|
|
|
ATTRIBUTION,
|
2020-02-28 05:23:47 +00:00
|
|
|
DOMAIN,
|
|
|
|
MDI_ICONS,
|
|
|
|
SENSE_DATA,
|
|
|
|
SENSE_DEVICE_UPDATE,
|
|
|
|
SENSE_DEVICES_DATA,
|
|
|
|
SENSE_DISCOVERED_DEVICES_DATA,
|
|
|
|
)
|
2018-11-02 09:13:14 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2020-02-25 23:37:41 +00:00
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
2018-12-01 18:27:21 +00:00
|
|
|
"""Set up the Sense binary sensor."""
|
2020-02-25 23:37:41 +00:00
|
|
|
data = hass.data[DOMAIN][config_entry.entry_id][SENSE_DATA]
|
2020-02-28 05:23:47 +00:00
|
|
|
sense_devices_data = hass.data[DOMAIN][config_entry.entry_id][SENSE_DEVICES_DATA]
|
2020-02-25 23:37:41 +00:00
|
|
|
sense_monitor_id = data.sense_monitor_id
|
2018-11-02 09:13:14 +00:00
|
|
|
|
2020-02-28 05:23:47 +00:00
|
|
|
sense_devices = hass.data[DOMAIN][config_entry.entry_id][
|
|
|
|
SENSE_DISCOVERED_DEVICES_DATA
|
|
|
|
]
|
2019-07-31 19:25:30 +00:00
|
|
|
devices = [
|
2020-02-28 05:23:47 +00:00
|
|
|
SenseDevice(sense_devices_data, device, sense_monitor_id)
|
2019-07-31 19:25:30 +00:00
|
|
|
for device in sense_devices
|
2020-02-28 05:23:47 +00:00
|
|
|
if device["tags"]["DeviceListAllowed"] == "true"
|
2019-07-31 19:25:30 +00:00
|
|
|
]
|
2020-02-25 23:37:41 +00:00
|
|
|
|
|
|
|
await _migrate_old_unique_ids(hass, devices)
|
|
|
|
|
2019-03-12 13:44:53 +00:00
|
|
|
async_add_entities(devices)
|
2018-11-02 09:13:14 +00:00
|
|
|
|
|
|
|
|
2020-02-25 23:37:41 +00:00
|
|
|
async def _migrate_old_unique_ids(hass, devices):
|
|
|
|
registry = await async_get_registry(hass)
|
|
|
|
for device in devices:
|
|
|
|
# Migration of old not so unique ids
|
|
|
|
old_entity_id = registry.async_get_entity_id(
|
|
|
|
"binary_sensor", DOMAIN, device.old_unique_id
|
|
|
|
)
|
|
|
|
if old_entity_id is not None:
|
|
|
|
_LOGGER.debug(
|
|
|
|
"Migrating unique_id from [%s] to [%s]",
|
|
|
|
device.old_unique_id,
|
|
|
|
device.unique_id,
|
|
|
|
)
|
|
|
|
registry.async_update_entity(old_entity_id, new_unique_id=device.unique_id)
|
|
|
|
|
|
|
|
|
2018-11-02 09:13:14 +00:00
|
|
|
def sense_to_mdi(sense_icon):
|
|
|
|
"""Convert sense icon to mdi icon."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return "mdi:{}".format(MDI_ICONS.get(sense_icon, "power-plug"))
|
2018-11-02 09:13:14 +00:00
|
|
|
|
|
|
|
|
2020-04-23 19:57:07 +00:00
|
|
|
class SenseDevice(BinarySensorEntity):
|
2018-11-02 09:13:14 +00:00
|
|
|
"""Implementation of a Sense energy device binary sensor."""
|
|
|
|
|
2020-02-28 05:23:47 +00:00
|
|
|
def __init__(self, sense_devices_data, device, sense_monitor_id):
|
2018-12-01 18:27:21 +00:00
|
|
|
"""Initialize the Sense binary sensor."""
|
2019-07-31 19:25:30 +00:00
|
|
|
self._name = device["name"]
|
|
|
|
self._id = device["id"]
|
2020-02-25 23:37:41 +00:00
|
|
|
self._sense_monitor_id = sense_monitor_id
|
|
|
|
self._unique_id = f"{sense_monitor_id}-{self._id}"
|
2019-07-31 19:25:30 +00:00
|
|
|
self._icon = sense_to_mdi(device["icon"])
|
2020-02-28 05:23:47 +00:00
|
|
|
self._sense_devices_data = sense_devices_data
|
|
|
|
self._state = None
|
|
|
|
self._available = False
|
2018-11-02 09:13:14 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if the binary sensor is on."""
|
2020-02-28 05:23:47 +00:00
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self):
|
|
|
|
"""Return the availability of the binary sensor."""
|
|
|
|
return self._available
|
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):
|
2020-02-25 23:37:41 +00:00
|
|
|
"""Return the unique id of the binary sensor."""
|
|
|
|
return self._unique_id
|
|
|
|
|
|
|
|
@property
|
|
|
|
def old_unique_id(self):
|
|
|
|
"""Return the old not so unique id of the binary sensor."""
|
2018-11-02 09:13:14 +00:00
|
|
|
return self._id
|
|
|
|
|
2020-04-04 20:51:00 +00:00
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the state attributes."""
|
|
|
|
return {ATTR_ATTRIBUTION: ATTRIBUTION}
|
|
|
|
|
2018-11-02 09:13:14 +00:00
|
|
|
@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."""
|
2020-02-28 05:23:47 +00:00
|
|
|
return DEVICE_CLASS_POWER
|
2018-11-02 09:13:14 +00:00
|
|
|
|
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."""
|
2020-04-14 00:08:37 +00:00
|
|
|
self.async_on_remove(
|
|
|
|
async_dispatcher_connect(
|
|
|
|
self.hass,
|
|
|
|
f"{SENSE_DEVICE_UPDATE}-{self._sense_monitor_id}",
|
|
|
|
self._async_update_from_data,
|
|
|
|
)
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-06-01 19:15:28 +00:00
|
|
|
|
2020-02-28 05:23:47 +00:00
|
|
|
@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()
|