1 """Support for monitoring a Neurio energy sensor."""
3 from __future__
import annotations
5 from datetime
import timedelta
9 import requests.exceptions
10 import voluptuous
as vol
13 PLATFORM_SCHEMA
as SENSOR_PLATFORM_SCHEMA,
26 _LOGGER = logging.getLogger(__name__)
28 CONF_API_SECRET =
"api_secret"
29 CONF_SENSOR_ID =
"sensor_id"
31 ACTIVE_NAME =
"Energy Usage"
32 DAILY_NAME =
"Daily Energy Usage"
34 ACTIVE_TYPE =
"active"
38 MIN_TIME_BETWEEN_DAILY_UPDATES =
timedelta(seconds=150)
39 MIN_TIME_BETWEEN_ACTIVE_UPDATES =
timedelta(seconds=10)
41 PLATFORM_SCHEMA = SENSOR_PLATFORM_SCHEMA.extend(
43 vol.Required(CONF_API_KEY): cv.string,
44 vol.Required(CONF_API_SECRET): cv.string,
45 vol.Required(CONF_SENSOR_ID): cv.string,
53 add_entities: AddEntitiesCallback,
54 discovery_info: DiscoveryInfoType |
None =
None,
56 """Set up the Neurio sensor."""
57 api_key = config.get(CONF_API_KEY)
58 api_secret = config.get(CONF_API_SECRET)
59 sensor_id = config.get(CONF_SENSOR_ID)
61 data =
NeurioData(api_key, api_secret, sensor_id)
63 @Throttle(MIN_TIME_BETWEEN_DAILY_UPDATES)
65 """Update the daily power usage."""
66 data.get_daily_usage()
68 @Throttle(MIN_TIME_BETWEEN_ACTIVE_UPDATES)
70 """Update the active power usage."""
71 data.get_active_power()
83 """Stores data retrieved from Neurio sensor."""
85 def __init__(self, api_key, api_secret, sensor_id):
86 """Initialize the data."""
96 neurio_tp = neurio.TokenProvider(key=api_key, secret=api_secret)
97 self.
neurio_clientneurio_client = neurio.Client(token_provider=neurio_tp)
101 """Return latest daily usage value."""
106 """Return latest active power value."""
110 """Return current power value."""
114 except (requests.exceptions.RequestException, ValueError, KeyError):
115 _LOGGER.warning(
"Could not update current power usage")
118 """Return current daily power usage."""
120 start_time = dt_util.start_of_local_day().astimezone(dt_util.UTC).isoformat()
121 end_time = dt_util.utcnow().isoformat()
123 _LOGGER.debug(
"Start: %s, End: %s", start_time, end_time)
127 self.
sensor_idsensor_id, start_time,
"days", end_time
129 except (requests.exceptions.RequestException, ValueError, KeyError):
130 _LOGGER.warning(
"Could not update daily power usage")
133 for result
in history:
134 kwh += result[
"consumptionEnergy"] / 3600000
140 """Implementation of a Neurio energy sensor."""
142 _attr_icon =
"mdi:flash"
144 def __init__(self, data, name, sensor_type, update_call):
145 """Initialize the sensor."""
152 if sensor_type == ACTIVE_TYPE:
156 elif sensor_type == DAILY_TYPE:
163 """Return the name of the sensor."""
164 return self.
_name_name
168 """Return the state of the sensor."""
173 """Return the unit of measurement of this entity, if any."""
177 """Get the latest data, update state."""
def __init__(self, api_key, api_secret, sensor_id)
None get_active_power(self)
None get_daily_usage(self)
def __init__(self, data, name, sensor_type, update_call)
def native_unit_of_measurement(self)
def add_entities(account, async_add_entities, tracked)
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)