-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
sensor.py
94 lines (79 loc) · 2.97 KB
/
sensor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import esphome.config_validation as cv
import esphome.codegen as cg
from esphome.components import sensor, time
from esphome.const import (
CONF_ID,
CONF_TIME_ID,
CONF_POWER,
CONF_TOTAL,
DEVICE_CLASS_ENERGY,
STATE_CLASS_TOTAL_INCREASING,
UNIT_KILOWATT_HOURS,
)
CODEOWNERS = ["@dentra"]
DEPENDENCIES = ["time"]
CONF_ENERGY_TODAY = "energy_today"
CONF_ENERGY_YESTERDAY = "energy_yesterday"
CONF_ENERGY_WEEK = "energy_week"
CONF_ENERGY_MONTH = "energy_month"
energy_statistics_ns = cg.esphome_ns.namespace("energy_statistics")
EnergyStatistics = energy_statistics_ns.class_("EnergyStatistics", cg.Component)
CONFIG_SCHEMA = cv.Schema(
{
cv.GenerateID(): cv.declare_id(EnergyStatistics),
cv.GenerateID(CONF_TIME_ID): cv.use_id(time.RealTimeClock),
cv.Required(CONF_TOTAL): cv.use_id(sensor.Sensor),
cv.Optional(CONF_ENERGY_TODAY): sensor.sensor_schema(
unit_of_measurement=UNIT_KILOWATT_HOURS,
accuracy_decimals=2,
device_class=DEVICE_CLASS_ENERGY,
state_class=STATE_CLASS_TOTAL_INCREASING,
),
cv.Optional(CONF_ENERGY_YESTERDAY): sensor.sensor_schema(
unit_of_measurement=UNIT_KILOWATT_HOURS,
accuracy_decimals=2,
device_class=DEVICE_CLASS_ENERGY,
state_class=STATE_CLASS_TOTAL_INCREASING,
),
cv.Optional(CONF_ENERGY_WEEK): sensor.sensor_schema(
unit_of_measurement=UNIT_KILOWATT_HOURS,
accuracy_decimals=2,
device_class=DEVICE_CLASS_ENERGY,
state_class=STATE_CLASS_TOTAL_INCREASING,
),
cv.Optional(CONF_ENERGY_MONTH): sensor.sensor_schema(
unit_of_measurement=UNIT_KILOWATT_HOURS,
accuracy_decimals=2,
device_class=DEVICE_CLASS_ENERGY,
state_class=STATE_CLASS_TOTAL_INCREASING,
),
}
).extend(cv.COMPONENT_SCHEMA)
async def setup_sensor(config, key, setter):
"""setting up sensor"""
if key not in config:
return None
var = await sensor.new_sensor(config[key])
cg.add(setter(var))
return var
async def setup_input(config, key, setter):
"""setting up input"""
if key not in config:
return None
var = await cg.get_variable(config[key])
cg.add(setter(var))
return var
# code generation entry point
async def to_code(config):
"""Code generation entry point"""
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await setup_input(config, CONF_TIME_ID, var.set_time)
# input sensors
await setup_input(config, CONF_POWER, var.set_power)
await setup_input(config, CONF_TOTAL, var.set_total)
# exposed sensors
await setup_sensor(config, CONF_ENERGY_TODAY, var.set_energy_today)
await setup_sensor(config, CONF_ENERGY_YESTERDAY, var.set_energy_yesterday)
await setup_sensor(config, CONF_ENERGY_WEEK, var.set_energy_week)
await setup_sensor(config, CONF_ENERGY_MONTH, var.set_energy_month)