2019-04-03 15:40:03 +00:00
|
|
|
"""Support for Efergy sensors."""
|
2015-07-09 16:05:19 +00:00
|
|
|
import logging
|
2016-02-19 05:27:50 +00:00
|
|
|
|
2018-07-09 12:39:28 +00:00
|
|
|
import requests
|
|
|
|
import voluptuous as vol
|
2015-07-09 16:05:19 +00:00
|
|
|
|
2021-03-22 11:52:29 +00:00
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
|
2021-02-08 08:45:05 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_CURRENCY,
|
|
|
|
CONF_MONITORED_VARIABLES,
|
|
|
|
CONF_TYPE,
|
|
|
|
ENERGY_KILO_WATT_HOUR,
|
|
|
|
POWER_WATT,
|
|
|
|
)
|
2018-07-09 12:39:28 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2015-07-09 16:05:19 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2019-07-31 19:25:30 +00:00
|
|
|
_RESOURCE = "https://engage.efergy.com/mobile_proxy/"
|
2016-08-23 03:51:17 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_APPTOKEN = "app_token"
|
|
|
|
CONF_UTC_OFFSET = "utc_offset"
|
2016-08-23 03:51:17 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_PERIOD = "period"
|
2016-08-23 03:51:17 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_INSTANT = "instant_readings"
|
|
|
|
CONF_AMOUNT = "amount"
|
|
|
|
CONF_BUDGET = "budget"
|
|
|
|
CONF_COST = "cost"
|
|
|
|
CONF_CURRENT_VALUES = "current_values"
|
2016-08-23 03:51:17 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_PERIOD = "year"
|
|
|
|
DEFAULT_UTC_OFFSET = "0"
|
2017-05-02 16:18:47 +00:00
|
|
|
|
2015-07-09 16:05:19 +00:00
|
|
|
SENSOR_TYPES = {
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_INSTANT: ["Energy Usage", POWER_WATT],
|
|
|
|
CONF_AMOUNT: ["Energy Consumed", ENERGY_KILO_WATT_HOUR],
|
|
|
|
CONF_BUDGET: ["Energy Budget", None],
|
|
|
|
CONF_COST: ["Energy Cost", None],
|
|
|
|
CONF_CURRENT_VALUES: ["Per-Device Usage", POWER_WATT],
|
2015-07-09 16:05:19 +00:00
|
|
|
}
|
|
|
|
|
2016-12-02 06:00:17 +00:00
|
|
|
TYPES_SCHEMA = vol.In(SENSOR_TYPES)
|
2016-08-23 03:51:17 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
SENSORS_SCHEMA = vol.Schema(
|
|
|
|
{
|
2021-02-08 08:45:05 +00:00
|
|
|
vol.Required(CONF_TYPE): TYPES_SCHEMA,
|
2019-07-31 19:25:30 +00:00
|
|
|
vol.Optional(CONF_CURRENCY, default=""): cv.string,
|
|
|
|
vol.Optional(CONF_PERIOD, default=DEFAULT_PERIOD): cv.string,
|
|
|
|
}
|
|
|
|
)
|
2016-08-23 03:51:17 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_APPTOKEN): cv.string,
|
|
|
|
vol.Optional(CONF_UTC_OFFSET, default=DEFAULT_UTC_OFFSET): cv.string,
|
|
|
|
vol.Required(CONF_MONITORED_VARIABLES): [SENSORS_SCHEMA],
|
|
|
|
}
|
|
|
|
)
|
2016-08-23 03:51:17 +00:00
|
|
|
|
2015-07-09 16:05:19 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up the Efergy sensor."""
|
2016-08-23 03:51:17 +00:00
|
|
|
app_token = config.get(CONF_APPTOKEN)
|
|
|
|
utc_offset = str(config.get(CONF_UTC_OFFSET))
|
2018-07-09 12:39:28 +00:00
|
|
|
|
2015-07-09 16:05:19 +00:00
|
|
|
dev = []
|
2016-08-23 03:51:17 +00:00
|
|
|
for variable in config[CONF_MONITORED_VARIABLES]:
|
2021-02-08 08:45:05 +00:00
|
|
|
if variable[CONF_TYPE] == CONF_CURRENT_VALUES:
|
2020-02-24 16:47:52 +00:00
|
|
|
url_string = f"{_RESOURCE}getCurrentValuesSummary?token={app_token}"
|
2018-07-09 12:39:28 +00:00
|
|
|
response = requests.get(url_string, timeout=10)
|
2017-03-17 06:22:10 +00:00
|
|
|
for sensor in response.json():
|
2019-07-31 19:25:30 +00:00
|
|
|
sid = sensor["sid"]
|
|
|
|
dev.append(
|
|
|
|
EfergySensor(
|
2021-02-08 08:45:05 +00:00
|
|
|
variable[CONF_TYPE],
|
2019-07-31 19:25:30 +00:00
|
|
|
app_token,
|
|
|
|
utc_offset,
|
|
|
|
variable[CONF_PERIOD],
|
|
|
|
variable[CONF_CURRENCY],
|
|
|
|
sid,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
dev.append(
|
|
|
|
EfergySensor(
|
2021-02-08 08:45:05 +00:00
|
|
|
variable[CONF_TYPE],
|
2019-07-31 19:25:30 +00:00
|
|
|
app_token,
|
|
|
|
utc_offset,
|
|
|
|
variable[CONF_PERIOD],
|
|
|
|
variable[CONF_CURRENCY],
|
|
|
|
)
|
|
|
|
)
|
2015-07-09 16:05:19 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities(dev, True)
|
2015-07-09 16:05:19 +00:00
|
|
|
|
|
|
|
|
2021-03-22 11:52:29 +00:00
|
|
|
class EfergySensor(SensorEntity):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Implementation of an Efergy sensor."""
|
2015-07-09 16:05:19 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
def __init__(self, sensor_type, app_token, utc_offset, period, currency, sid=None):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Initialize the sensor."""
|
2017-03-17 06:22:10 +00:00
|
|
|
self.sid = sid
|
|
|
|
if sid:
|
2019-09-03 15:10:56 +00:00
|
|
|
self._name = f"efergy_{sid}"
|
2017-03-17 06:22:10 +00:00
|
|
|
else:
|
|
|
|
self._name = SENSOR_TYPES[sensor_type][0]
|
2015-07-09 16:05:19 +00:00
|
|
|
self.type = sensor_type
|
|
|
|
self.app_token = app_token
|
|
|
|
self.utc_offset = utc_offset
|
|
|
|
self._state = None
|
|
|
|
self.period = period
|
|
|
|
self.currency = currency
|
2019-07-31 19:25:30 +00:00
|
|
|
if self.type == "cost":
|
2019-09-03 15:10:56 +00:00
|
|
|
self._unit_of_measurement = f"{self.currency}/{self.period}"
|
2015-07-09 16:05:19 +00:00
|
|
|
else:
|
|
|
|
self._unit_of_measurement = SENSOR_TYPES[sensor_type][1]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the name of the sensor."""
|
2015-07-09 16:05:19 +00:00
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the state of the sensor."""
|
2015-07-09 16:05:19 +00:00
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the unit of measurement of this entity, if any."""
|
2015-07-09 16:05:19 +00:00
|
|
|
return self._unit_of_measurement
|
|
|
|
|
|
|
|
def update(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Get the Efergy monitor data from the web service."""
|
2015-10-28 10:57:10 +00:00
|
|
|
try:
|
2019-07-31 19:25:30 +00:00
|
|
|
if self.type == "instant_readings":
|
2019-09-03 15:10:56 +00:00
|
|
|
url_string = f"{_RESOURCE}getInstant?token={self.app_token}"
|
2018-07-09 12:39:28 +00:00
|
|
|
response = requests.get(url_string, timeout=10)
|
2019-07-31 19:25:30 +00:00
|
|
|
self._state = response.json()["reading"]
|
|
|
|
elif self.type == "amount":
|
2020-02-24 16:47:52 +00:00
|
|
|
url_string = f"{_RESOURCE}getEnergy?token={self.app_token}&offset={self.utc_offset}&period={self.period}"
|
2018-07-09 12:39:28 +00:00
|
|
|
response = requests.get(url_string, timeout=10)
|
2019-07-31 19:25:30 +00:00
|
|
|
self._state = response.json()["sum"]
|
|
|
|
elif self.type == "budget":
|
2019-09-03 15:10:56 +00:00
|
|
|
url_string = f"{_RESOURCE}getBudget?token={self.app_token}"
|
2018-07-09 12:39:28 +00:00
|
|
|
response = requests.get(url_string, timeout=10)
|
2019-07-31 19:25:30 +00:00
|
|
|
self._state = response.json()["status"]
|
|
|
|
elif self.type == "cost":
|
2020-02-24 16:47:52 +00:00
|
|
|
url_string = f"{_RESOURCE}getCost?token={self.app_token}&offset={self.utc_offset}&period={self.period}"
|
2018-07-09 12:39:28 +00:00
|
|
|
response = requests.get(url_string, timeout=10)
|
2019-07-31 19:25:30 +00:00
|
|
|
self._state = response.json()["sum"]
|
|
|
|
elif self.type == "current_values":
|
2020-02-24 16:47:52 +00:00
|
|
|
url_string = (
|
|
|
|
f"{_RESOURCE}getCurrentValuesSummary?token={self.app_token}"
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-07-09 12:39:28 +00:00
|
|
|
response = requests.get(url_string, timeout=10)
|
2017-03-17 06:22:10 +00:00
|
|
|
for sensor in response.json():
|
2019-07-31 19:25:30 +00:00
|
|
|
if self.sid == sensor["sid"]:
|
|
|
|
measurement = next(iter(sensor["data"][0].values()))
|
2017-08-01 03:41:45 +00:00
|
|
|
self._state = measurement
|
2015-10-28 10:57:10 +00:00
|
|
|
else:
|
2018-07-09 12:39:28 +00:00
|
|
|
self._state = None
|
|
|
|
except (requests.RequestException, ValueError, KeyError):
|
2017-05-02 16:18:47 +00:00
|
|
|
_LOGGER.warning("Could not update status for %s", self.name)
|