hass-core/homeassistant/components/sense/sensor.py

157 lines
4.4 KiB
Python
Raw Normal View History

"""Support for monitoring a Sense energy sensor."""
from datetime import timedelta
2018-12-01 19:27:21 +01:00
import logging
2019-12-01 06:35:45 +01:00
from sense_energy import SenseAPITimeoutException
from homeassistant.const import ENERGY_KILO_WATT_HOUR, POWER_WATT
from homeassistant.helpers.entity import Entity
from homeassistant.util import Throttle
from .const import DOMAIN, SENSE_DATA
_LOGGER = logging.getLogger(__name__)
2019-07-31 12:25:30 -07:00
ACTIVE_NAME = "Energy"
ACTIVE_TYPE = "active"
2018-12-01 19:27:21 +01:00
2019-07-31 12:25:30 -07:00
CONSUMPTION_NAME = "Usage"
2019-07-31 12:25:30 -07:00
ICON = "mdi:flash"
2018-12-01 19:27:21 +01:00
MIN_TIME_BETWEEN_DAILY_UPDATES = timedelta(seconds=300)
2019-07-31 12:25:30 -07:00
PRODUCTION_NAME = "Production"
class SensorConfig:
2018-12-01 19:27:21 +01:00
"""Data structure holding sensor configuration."""
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
2018-12-01 19:27:21 +01:00
SENSOR_TYPES = {
2019-07-31 12:25:30 -07:00
"active": SensorConfig(ACTIVE_NAME, ACTIVE_TYPE),
"daily": SensorConfig("Daily", "DAY"),
"weekly": SensorConfig("Weekly", "WEEK"),
"monthly": SensorConfig("Monthly", "MONTH"),
"yearly": SensorConfig("Yearly", "YEAR"),
2018-12-01 19:27:21 +01:00
}
# Production/consumption variants
SENSOR_VARIANTS = [PRODUCTION_NAME.lower(), CONSUMPTION_NAME.lower()]
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up the Sense sensor."""
data = hass.data[DOMAIN][config_entry.entry_id][SENSE_DATA]
@Throttle(MIN_TIME_BETWEEN_DAILY_UPDATES)
async def update_trends():
"""Update the daily power usage."""
await data.update_trend_data()
async def update_active():
"""Update the active power usage."""
await data.update_realtime()
sense_monitor_id = data.sense_monitor_id
devices = []
for type_id in SENSOR_TYPES:
typ = SENSOR_TYPES[type_id]
for var in SENSOR_VARIANTS:
name = typ.name
sensor_type = typ.sensor_type
is_production = var == PRODUCTION_NAME.lower()
if sensor_type == ACTIVE_TYPE:
update_call = update_active
else:
update_call = update_trends
unique_id = f"{sense_monitor_id}-{type_id}-{var}".lower()
devices.append(
Sense(
data, name, sensor_type, is_production, update_call, var, unique_id
)
)
async_add_entities(devices)
class Sense(Entity):
"""Implementation of a Sense energy sensor."""
def __init__(
self, data, name, sensor_type, is_production, update_call, sensor_id, unique_id
):
2018-12-01 19:27:21 +01:00
"""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._sensor_type = sensor_type
self.update_sensor = update_call
self._is_production = is_production
self._state = None
if sensor_type == ACTIVE_TYPE:
self._unit_of_measurement = POWER_WATT
else:
self._unit_of_measurement = ENERGY_KILO_WATT_HOUR
@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 self._unit_of_measurement
@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
async def async_update(self):
"""Get the latest data, update state."""
2019-07-31 12:25:30 -07:00
try:
await self.update_sensor()
except SenseAPITimeoutException:
_LOGGER.error("Timeout retrieving data")
return
if self._sensor_type == ACTIVE_TYPE:
if self._is_production:
self._state = round(self._data.active_solar_power)
else:
self._state = round(self._data.active_power)
else:
2019-07-31 12:25:30 -07:00
state = self._data.get_trend(self._sensor_type, self._is_production)
self._state = round(state, 1)
self._available = True