Skip to content

Commit bbe03dc

Browse files
authored
Add missing Opower tests (home-assistant#147934)
1 parent f77e6cc commit bbe03dc

File tree

5 files changed

+668
-0
lines changed

5 files changed

+668
-0
lines changed

tests/components/opower/conftest.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
"""Fixtures for the Opower integration tests."""
22

3+
from collections.abc import Generator
4+
from datetime import date
5+
from unittest.mock import AsyncMock, Mock, patch
6+
7+
from opower import Account, Forecast, MeterType, ReadResolution, UnitOfMeasure
8+
from opower.utilities.pge import PGE
39
import pytest
410

511
from homeassistant.components.opower.const import DOMAIN
@@ -22,3 +28,76 @@ def mock_config_entry(hass: HomeAssistant) -> MockConfigEntry:
2228
)
2329
config_entry.add_to_hass(hass)
2430
return config_entry
31+
32+
33+
@pytest.fixture
34+
def mock_opower_api() -> Generator[AsyncMock]:
35+
"""Mock Opower API."""
36+
with patch(
37+
"homeassistant.components.opower.coordinator.Opower", autospec=True
38+
) as mock_api:
39+
api = mock_api.return_value
40+
api.utility = PGE
41+
42+
api.async_get_accounts.return_value = [
43+
Account(
44+
customer=Mock(),
45+
uuid="111111-uuid",
46+
utility_account_id="111111",
47+
id="111111",
48+
meter_type=MeterType.ELEC,
49+
read_resolution=ReadResolution.HOUR,
50+
),
51+
Account(
52+
customer=Mock(),
53+
uuid="222222-uuid",
54+
utility_account_id="222222",
55+
id="222222",
56+
meter_type=MeterType.GAS,
57+
read_resolution=ReadResolution.DAY,
58+
),
59+
]
60+
api.async_get_forecast.return_value = [
61+
Forecast(
62+
account=Account(
63+
customer=Mock(),
64+
uuid="111111-uuid",
65+
utility_account_id="111111",
66+
id="111111",
67+
meter_type=MeterType.ELEC,
68+
read_resolution=ReadResolution.HOUR,
69+
),
70+
usage_to_date=100,
71+
cost_to_date=20.0,
72+
forecasted_usage=200,
73+
forecasted_cost=40.0,
74+
typical_usage=180,
75+
typical_cost=36.0,
76+
unit_of_measure=UnitOfMeasure.KWH,
77+
start_date=date(2023, 1, 1),
78+
end_date=date(2023, 1, 31),
79+
current_date=date(2023, 1, 15),
80+
),
81+
Forecast(
82+
account=Account(
83+
customer=Mock(),
84+
uuid="222222-uuid",
85+
utility_account_id="222222",
86+
id="222222",
87+
meter_type=MeterType.GAS,
88+
read_resolution=ReadResolution.DAY,
89+
),
90+
usage_to_date=50,
91+
cost_to_date=15.0,
92+
forecasted_usage=100,
93+
forecasted_cost=30.0,
94+
typical_usage=90,
95+
typical_cost=27.0,
96+
unit_of_measure=UnitOfMeasure.CCF,
97+
start_date=date(2023, 1, 1),
98+
end_date=date(2023, 1, 31),
99+
current_date=date(2023, 1, 15),
100+
),
101+
]
102+
api.async_get_cost_reads.return_value = []
103+
yield api
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
# serializer version: 1
2+
# name: test_coordinator_first_run
3+
defaultdict({
4+
'opower:pge_elec_111111_energy_compensation': list([
5+
dict({
6+
'end': 1672592400.0,
7+
'start': 1672588800.0,
8+
'state': 0.0,
9+
'sum': 0.0,
10+
}),
11+
dict({
12+
'end': 1672596000.0,
13+
'start': 1672592400.0,
14+
'state': 0.1,
15+
'sum': 0.1,
16+
}),
17+
]),
18+
'opower:pge_elec_111111_energy_consumption': list([
19+
dict({
20+
'end': 1672592400.0,
21+
'start': 1672588800.0,
22+
'state': 1.5,
23+
'sum': 1.5,
24+
}),
25+
dict({
26+
'end': 1672596000.0,
27+
'start': 1672592400.0,
28+
'state': 0.0,
29+
'sum': 1.5,
30+
}),
31+
]),
32+
'opower:pge_elec_111111_energy_cost': list([
33+
dict({
34+
'end': 1672592400.0,
35+
'start': 1672588800.0,
36+
'state': 0.5,
37+
'sum': 0.5,
38+
}),
39+
dict({
40+
'end': 1672596000.0,
41+
'start': 1672592400.0,
42+
'state': 0.0,
43+
'sum': 0.5,
44+
}),
45+
]),
46+
'opower:pge_elec_111111_energy_return': list([
47+
dict({
48+
'end': 1672592400.0,
49+
'start': 1672588800.0,
50+
'state': 0.0,
51+
'sum': 0.0,
52+
}),
53+
dict({
54+
'end': 1672596000.0,
55+
'start': 1672592400.0,
56+
'state': 0.5,
57+
'sum': 0.5,
58+
}),
59+
]),
60+
})
61+
# ---
62+
# name: test_coordinator_migration
63+
defaultdict({
64+
'opower:pge_elec_111111_energy_consumption': list([
65+
dict({
66+
'end': 1672592400.0,
67+
'start': 1672588800.0,
68+
'state': 1.5,
69+
'sum': 1.5,
70+
}),
71+
dict({
72+
'end': 1672596000.0,
73+
'start': 1672592400.0,
74+
'state': 0.0,
75+
'sum': 1.5,
76+
}),
77+
]),
78+
'opower:pge_elec_111111_energy_return': list([
79+
dict({
80+
'end': 1672592400.0,
81+
'start': 1672588800.0,
82+
'state': 0.0,
83+
'sum': 0.0,
84+
}),
85+
dict({
86+
'end': 1672596000.0,
87+
'start': 1672592400.0,
88+
'state': 0.5,
89+
'sum': 0.5,
90+
}),
91+
]),
92+
})
93+
# ---
94+
# name: test_coordinator_subsequent_run
95+
defaultdict({
96+
'opower:pge_elec_111111_energy_compensation': list([
97+
dict({
98+
'end': 1672592400.0,
99+
'start': 1672588800.0,
100+
'state': 0.0,
101+
'sum': 0.0,
102+
}),
103+
dict({
104+
'end': 1672596000.0,
105+
'start': 1672592400.0,
106+
'state': 0.1,
107+
'sum': 0.1,
108+
}),
109+
dict({
110+
'end': 1672599600.0,
111+
'start': 1672596000.0,
112+
'state': 0.0,
113+
'sum': 0.1,
114+
}),
115+
]),
116+
'opower:pge_elec_111111_energy_consumption': list([
117+
dict({
118+
'end': 1672592400.0,
119+
'start': 1672588800.0,
120+
'state': 1.5,
121+
'sum': 1.5,
122+
}),
123+
dict({
124+
'end': 1672596000.0,
125+
'start': 1672592400.0,
126+
'state': 0.0,
127+
'sum': 1.5,
128+
}),
129+
dict({
130+
'end': 1672599600.0,
131+
'start': 1672596000.0,
132+
'state': 2.0,
133+
'sum': 3.5,
134+
}),
135+
]),
136+
'opower:pge_elec_111111_energy_cost': list([
137+
dict({
138+
'end': 1672592400.0,
139+
'start': 1672588800.0,
140+
'state': 0.5,
141+
'sum': 0.5,
142+
}),
143+
dict({
144+
'end': 1672596000.0,
145+
'start': 1672592400.0,
146+
'state': 0.0,
147+
'sum': 0.5,
148+
}),
149+
dict({
150+
'end': 1672599600.0,
151+
'start': 1672596000.0,
152+
'state': 0.7,
153+
'sum': 1.2,
154+
}),
155+
]),
156+
'opower:pge_elec_111111_energy_return': list([
157+
dict({
158+
'end': 1672592400.0,
159+
'start': 1672588800.0,
160+
'state': 0.0,
161+
'sum': 0.0,
162+
}),
163+
dict({
164+
'end': 1672596000.0,
165+
'start': 1672592400.0,
166+
'state': 0.5,
167+
'sum': 0.5,
168+
}),
169+
dict({
170+
'end': 1672599600.0,
171+
'start': 1672596000.0,
172+
'state': 0.0,
173+
'sum': 0.5,
174+
}),
175+
]),
176+
})
177+
# ---

0 commit comments

Comments
 (0)