2019-02-13 20:21:14 +00:00
|
|
|
"""Support for monitoring a Sense energy sensor."""
|
2018-12-01 18:27:21 +00:00
|
|
|
import logging
|
2018-02-28 22:29:24 +00:00
|
|
|
|
2020-04-04 20:51:00 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_ATTRIBUTION,
|
|
|
|
DEVICE_CLASS_POWER,
|
|
|
|
ENERGY_KILO_WATT_HOUR,
|
|
|
|
POWER_WATT,
|
|
|
|
)
|
2020-02-28 05:23:47 +00:00
|
|
|
from homeassistant.core import callback
|
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2018-02-28 22:29:24 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
|
2020-02-28 05:23:47 +00:00
|
|
|
from .const import (
|
|
|
|
ACTIVE_NAME,
|
|
|
|
ACTIVE_TYPE,
|
2020-04-04 20:51:00 +00:00
|
|
|
ATTRIBUTION,
|
2020-02-28 05:23:47 +00:00
|
|
|
CONSUMPTION_ID,
|
|
|
|
CONSUMPTION_NAME,
|
|
|
|
DOMAIN,
|
|
|
|
ICON,
|
|
|
|
MDI_ICONS,
|
|
|
|
PRODUCTION_ID,
|
|
|
|
PRODUCTION_NAME,
|
|
|
|
SENSE_DATA,
|
|
|
|
SENSE_DEVICE_UPDATE,
|
|
|
|
SENSE_DEVICES_DATA,
|
|
|
|
SENSE_DISCOVERED_DEVICES_DATA,
|
2020-04-14 00:08:37 +00:00
|
|
|
SENSE_TRENDS_COORDINATOR,
|
2020-02-28 05:23:47 +00:00
|
|
|
)
|
2018-12-01 18:27:21 +00:00
|
|
|
|
2020-02-28 05:23:47 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2018-02-28 22:29:24 +00:00
|
|
|
|
|
|
|
|
2018-07-20 08:45:20 +00:00
|
|
|
class SensorConfig:
|
2018-12-01 18:27:21 +00:00
|
|
|
"""Data structure holding sensor configuration."""
|
2018-02-28 22:29:24 +00:00
|
|
|
|
|
|
|
def __init__(self, name, sensor_type):
|
|
|
|
"""Sensor name and type to pass to API."""
|
|
|
|
self.name = name
|
|
|
|
self.sensor_type = sensor_type
|
|
|
|
|
|
|
|
|
|
|
|
# Sensor types/ranges
|
2020-02-28 05:23:47 +00:00
|
|
|
ACTIVE_SENSOR_TYPE = SensorConfig(ACTIVE_NAME, ACTIVE_TYPE)
|
|
|
|
|
|
|
|
# Sensor types/ranges
|
|
|
|
TRENDS_SENSOR_TYPES = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"daily": SensorConfig("Daily", "DAY"),
|
|
|
|
"weekly": SensorConfig("Weekly", "WEEK"),
|
|
|
|
"monthly": SensorConfig("Monthly", "MONTH"),
|
|
|
|
"yearly": SensorConfig("Yearly", "YEAR"),
|
2018-12-01 18:27:21 +00:00
|
|
|
}
|
2018-02-28 22:29:24 +00:00
|
|
|
|
|
|
|
# Production/consumption variants
|
2020-02-28 05:23:47 +00:00
|
|
|
SENSOR_VARIANTS = [PRODUCTION_ID, CONSUMPTION_ID]
|
|
|
|
|
|
|
|
|
|
|
|
def sense_to_mdi(sense_icon):
|
|
|
|
"""Convert sense icon to mdi icon."""
|
|
|
|
return "mdi:{}".format(MDI_ICONS.get(sense_icon, "power-plug"))
|
2018-02-28 22:29:24 +00:00
|
|
|
|
|
|
|
|
2020-02-25 23:37:41 +00:00
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
2018-02-28 22:29:24 +00:00
|
|
|
"""Set up the Sense 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-04-14 00:08:37 +00:00
|
|
|
trends_coordinator = hass.data[DOMAIN][config_entry.entry_id][
|
|
|
|
SENSE_TRENDS_COORDINATOR
|
|
|
|
]
|
2018-02-28 22:29:24 +00:00
|
|
|
|
2020-04-14 00:08:37 +00:00
|
|
|
# Request only in case it takes longer
|
|
|
|
# than 60s
|
|
|
|
await trends_coordinator.async_request_refresh()
|
2018-02-28 22:29:24 +00:00
|
|
|
|
2020-02-25 23:37:41 +00:00
|
|
|
sense_monitor_id = data.sense_monitor_id
|
2020-02-28 05:23:47 +00:00
|
|
|
sense_devices = hass.data[DOMAIN][config_entry.entry_id][
|
|
|
|
SENSE_DISCOVERED_DEVICES_DATA
|
|
|
|
]
|
|
|
|
|
|
|
|
devices = [
|
|
|
|
SenseEnergyDevice(sense_devices_data, device, sense_monitor_id)
|
|
|
|
for device in sense_devices
|
|
|
|
if device["tags"]["DeviceListAllowed"] == "true"
|
|
|
|
]
|
|
|
|
|
|
|
|
for var in SENSOR_VARIANTS:
|
|
|
|
name = ACTIVE_SENSOR_TYPE.name
|
|
|
|
sensor_type = ACTIVE_SENSOR_TYPE.sensor_type
|
|
|
|
is_production = var == PRODUCTION_ID
|
|
|
|
|
|
|
|
unique_id = f"{sense_monitor_id}-active-{var}"
|
|
|
|
devices.append(
|
|
|
|
SenseActiveSensor(
|
|
|
|
data, name, sensor_type, is_production, sense_monitor_id, var, unique_id
|
|
|
|
)
|
|
|
|
)
|
2020-02-25 23:37:41 +00:00
|
|
|
|
2020-02-28 05:23:47 +00:00
|
|
|
for type_id in TRENDS_SENSOR_TYPES:
|
|
|
|
typ = TRENDS_SENSOR_TYPES[type_id]
|
2018-11-02 09:13:14 +00:00
|
|
|
for var in SENSOR_VARIANTS:
|
|
|
|
name = typ.name
|
|
|
|
sensor_type = typ.sensor_type
|
2020-02-28 05:23:47 +00:00
|
|
|
is_production = var == PRODUCTION_ID
|
2020-02-25 23:37:41 +00:00
|
|
|
|
2020-02-28 05:23:47 +00:00
|
|
|
unique_id = f"{sense_monitor_id}-{type_id}-{var}"
|
2020-02-25 23:37:41 +00:00
|
|
|
devices.append(
|
2020-02-28 05:23:47 +00:00
|
|
|
SenseTrendsSensor(
|
|
|
|
data,
|
|
|
|
name,
|
|
|
|
sensor_type,
|
|
|
|
is_production,
|
2020-04-14 00:08:37 +00:00
|
|
|
trends_coordinator,
|
2020-02-28 05:23:47 +00:00
|
|
|
unique_id,
|
2020-02-25 23:37:41 +00:00
|
|
|
)
|
|
|
|
)
|
2018-02-28 22:29:24 +00:00
|
|
|
|
2019-03-12 13:44:53 +00:00
|
|
|
async_add_entities(devices)
|
2018-02-28 22:29:24 +00:00
|
|
|
|
|
|
|
|
2020-02-28 05:23:47 +00:00
|
|
|
class SenseActiveSensor(Entity):
|
|
|
|
"""Implementation of a Sense energy sensor."""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
data,
|
|
|
|
name,
|
|
|
|
sensor_type,
|
|
|
|
is_production,
|
|
|
|
sense_monitor_id,
|
|
|
|
sensor_id,
|
|
|
|
unique_id,
|
|
|
|
):
|
|
|
|
"""Initialize the Sense sensor."""
|
|
|
|
name_type = PRODUCTION_NAME if is_production else CONSUMPTION_NAME
|
|
|
|
self._name = f"{name} {name_type}"
|
|
|
|
self._unique_id = unique_id
|
|
|
|
self._available = False
|
|
|
|
self._data = data
|
|
|
|
self._sense_monitor_id = sense_monitor_id
|
|
|
|
self._sensor_type = sensor_type
|
|
|
|
self._is_production = is_production
|
|
|
|
self._state = None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the sensor."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the sensor."""
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self):
|
|
|
|
"""Return the availability of the sensor."""
|
|
|
|
return self._available
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Return the unit of measurement of this entity, if any."""
|
|
|
|
return POWER_WATT
|
|
|
|
|
2020-04-04 20:51:00 +00:00
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the state attributes."""
|
|
|
|
return {ATTR_ATTRIBUTION: ATTRIBUTION}
|
|
|
|
|
2020-02-28 05:23:47 +00:00
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Icon to use in the frontend, if any."""
|
|
|
|
return ICON
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return the unique id."""
|
|
|
|
return self._unique_id
|
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
|
|
|
"""Return the device should 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,
|
|
|
|
)
|
2020-02-28 05:23:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def _async_update_from_data(self):
|
|
|
|
"""Update the sensor from the data. Must not do I/O."""
|
|
|
|
self._state = round(
|
|
|
|
self._data.active_solar_power
|
|
|
|
if self._is_production
|
|
|
|
else self._data.active_power
|
|
|
|
)
|
|
|
|
self._available = True
|
|
|
|
self.async_write_ha_state()
|
|
|
|
|
|
|
|
|
|
|
|
class SenseTrendsSensor(Entity):
|
2018-02-28 22:29:24 +00:00
|
|
|
"""Implementation of a Sense energy sensor."""
|
|
|
|
|
2020-02-25 23:37:41 +00:00
|
|
|
def __init__(
|
2020-04-14 00:08:37 +00:00
|
|
|
self, data, name, sensor_type, is_production, trends_coordinator, unique_id,
|
2020-02-25 23:37:41 +00:00
|
|
|
):
|
2018-12-01 18:27:21 +00:00
|
|
|
"""Initialize the Sense sensor."""
|
2018-02-28 22:29:24 +00:00
|
|
|
name_type = PRODUCTION_NAME if is_production else CONSUMPTION_NAME
|
2019-09-03 19:14:39 +00:00
|
|
|
self._name = f"{name} {name_type}"
|
2020-02-25 23:37:41 +00:00
|
|
|
self._unique_id = unique_id
|
|
|
|
self._available = False
|
2018-02-28 22:29:24 +00:00
|
|
|
self._data = data
|
|
|
|
self._sensor_type = sensor_type
|
2020-04-14 00:08:37 +00:00
|
|
|
self._coordinator = trends_coordinator
|
2018-02-28 22:29:24 +00:00
|
|
|
self._is_production = is_production
|
|
|
|
self._state = None
|
2020-02-28 05:23:47 +00:00
|
|
|
self._unit_of_measurement = ENERGY_KILO_WATT_HOUR
|
2020-04-14 00:08:37 +00:00
|
|
|
self._had_any_update = False
|
2018-02-28 22:29:24 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the sensor."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the sensor."""
|
2020-04-14 00:08:37 +00:00
|
|
|
return round(self._data.get_trend(self._sensor_type, self._is_production), 1)
|
2018-02-28 22:29:24 +00:00
|
|
|
|
2020-02-25 23:37:41 +00:00
|
|
|
@property
|
|
|
|
def available(self):
|
2020-04-14 00:08:37 +00:00
|
|
|
"""Return if entity is available."""
|
|
|
|
return self._had_any_update and self._coordinator.last_update_success
|
2020-02-25 23:37:41 +00:00
|
|
|
|
2018-02-28 22:29:24 +00:00
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Return the unit of measurement of this entity, if any."""
|
|
|
|
return self._unit_of_measurement
|
|
|
|
|
2020-04-07 14:45:48 +00:00
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the state attributes."""
|
|
|
|
return {ATTR_ATTRIBUTION: ATTRIBUTION}
|
|
|
|
|
2018-02-28 22:29:24 +00:00
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Icon to use in the frontend, if any."""
|
|
|
|
return ICON
|
|
|
|
|
2020-02-25 23:37:41 +00:00
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return the unique id."""
|
|
|
|
return self._unique_id
|
|
|
|
|
2020-04-14 00:08:37 +00:00
|
|
|
@property
|
|
|
|
def should_poll(self):
|
|
|
|
"""No need to poll. Coordinator notifies entity of updates."""
|
|
|
|
return False
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def _async_update(self):
|
|
|
|
"""Track if we had an update so we do not report zero data."""
|
|
|
|
self._had_any_update = True
|
|
|
|
self.async_write_ha_state()
|
|
|
|
|
2019-03-12 13:44:53 +00:00
|
|
|
async def async_update(self):
|
2020-04-14 00:08:37 +00:00
|
|
|
"""Update the entity.
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2020-04-14 00:08:37 +00:00
|
|
|
Only used by the generic entity update service.
|
|
|
|
"""
|
|
|
|
await self._coordinator.async_request_refresh()
|
2018-02-28 22:29:24 +00:00
|
|
|
|
2020-04-14 00:08:37 +00:00
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""When entity is added to hass."""
|
|
|
|
self.async_on_remove(self._coordinator.async_add_listener(self._async_update))
|
2020-02-28 05:23:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SenseEnergyDevice(Entity):
|
|
|
|
"""Implementation of a Sense energy device."""
|
|
|
|
|
|
|
|
def __init__(self, sense_devices_data, device, sense_monitor_id):
|
|
|
|
"""Initialize the Sense binary sensor."""
|
|
|
|
self._name = f"{device['name']} {CONSUMPTION_NAME}"
|
|
|
|
self._id = device["id"]
|
|
|
|
self._available = False
|
|
|
|
self._sense_monitor_id = sense_monitor_id
|
|
|
|
self._unique_id = f"{sense_monitor_id}-{self._id}-{CONSUMPTION_ID}"
|
|
|
|
self._icon = sense_to_mdi(device["icon"])
|
|
|
|
self._sense_devices_data = sense_devices_data
|
|
|
|
self._state = None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the wattage of the sensor."""
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self):
|
|
|
|
"""Return the availability of the sensor."""
|
|
|
|
return self._available
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the power sensor."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return the unique id of the power sensor."""
|
|
|
|
return self._unique_id
|
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Return the icon of the power sensor."""
|
|
|
|
return self._icon
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Return the unit of measurement of this entity."""
|
|
|
|
return POWER_WATT
|
|
|
|
|
2020-04-07 14:45:48 +00:00
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the state attributes."""
|
|
|
|
return {ATTR_ATTRIBUTION: ATTRIBUTION}
|
|
|
|
|
2020-02-28 05:23:47 +00:00
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Return the device class of the power sensor."""
|
|
|
|
return DEVICE_CLASS_POWER
|
2020-02-25 23:37:41 +00:00
|
|
|
|
2020-02-28 05:23:47 +00:00
|
|
|
@property
|
|
|
|
def should_poll(self):
|
|
|
|
"""Return the device should 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,
|
|
|
|
)
|
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."""
|
|
|
|
device_data = self._sense_devices_data.get_device_by_id(self._id)
|
|
|
|
if not device_data or "w" not in device_data:
|
|
|
|
self._state = 0
|
|
|
|
else:
|
|
|
|
self._state = int(device_data["w"])
|
2020-02-25 23:37:41 +00:00
|
|
|
self._available = True
|
2020-02-28 05:23:47 +00:00
|
|
|
self.async_write_ha_state()
|