|
17 | 17 | ) |
18 | 18 | from homeassistant.helpers import config_validation as cv |
19 | 19 | from homeassistant.helpers.entityfilter import generate_filter |
| 20 | +from homeassistant.helpers.recorder import DATA_INSTANCE |
20 | 21 | from homeassistant.helpers.service import ( |
21 | 22 | async_extract_entity_ids, |
22 | 23 | async_register_admin_service, |
|
25 | 26 | from homeassistant.util.json import JsonArrayType, JsonObjectType |
26 | 27 |
|
27 | 28 | from .const import ATTR_APPLY_FILTER, ATTR_KEEP_DAYS, ATTR_REPACK, DOMAIN |
28 | | -from .core import Recorder |
29 | 29 | from .statistics import statistics_during_period |
30 | 30 | from .tasks import PurgeEntitiesTask, PurgeTask |
31 | 31 |
|
|
87 | 87 | ) |
88 | 88 |
|
89 | 89 |
|
90 | | -@callback |
91 | | -def _async_register_purge_service(hass: HomeAssistant, instance: Recorder) -> None: |
92 | | - async def async_handle_purge_service(service: ServiceCall) -> None: |
93 | | - """Handle calls to the purge service.""" |
94 | | - kwargs = service.data |
95 | | - keep_days = kwargs.get(ATTR_KEEP_DAYS, instance.keep_days) |
96 | | - repack = cast(bool, kwargs[ATTR_REPACK]) |
97 | | - apply_filter = cast(bool, kwargs[ATTR_APPLY_FILTER]) |
98 | | - purge_before = dt_util.utcnow() - timedelta(days=keep_days) |
99 | | - instance.queue_task(PurgeTask(purge_before, repack, apply_filter)) |
| 90 | +async def _async_handle_purge_service(service: ServiceCall) -> None: |
| 91 | + """Handle calls to the purge service.""" |
| 92 | + hass = service.hass |
| 93 | + instance = hass.data[DATA_INSTANCE] |
| 94 | + kwargs = service.data |
| 95 | + keep_days = kwargs.get(ATTR_KEEP_DAYS, instance.keep_days) |
| 96 | + repack = cast(bool, kwargs[ATTR_REPACK]) |
| 97 | + apply_filter = cast(bool, kwargs[ATTR_APPLY_FILTER]) |
| 98 | + purge_before = dt_util.utcnow() - timedelta(days=keep_days) |
| 99 | + instance.queue_task(PurgeTask(purge_before, repack, apply_filter)) |
| 100 | + |
| 101 | + |
| 102 | +async def _async_handle_purge_entities_service(service: ServiceCall) -> None: |
| 103 | + """Handle calls to the purge entities service.""" |
| 104 | + hass = service.hass |
| 105 | + entity_ids = await async_extract_entity_ids(hass, service) |
| 106 | + domains = service.data.get(ATTR_DOMAINS, []) |
| 107 | + keep_days = service.data.get(ATTR_KEEP_DAYS, 0) |
| 108 | + entity_globs = service.data.get(ATTR_ENTITY_GLOBS, []) |
| 109 | + entity_filter = generate_filter(domains, list(entity_ids), [], [], entity_globs) |
| 110 | + purge_before = dt_util.utcnow() - timedelta(days=keep_days) |
| 111 | + hass.data[DATA_INSTANCE].queue_task(PurgeEntitiesTask(entity_filter, purge_before)) |
| 112 | + |
| 113 | + |
| 114 | +async def _async_handle_enable_service(service: ServiceCall) -> None: |
| 115 | + service.hass.data[DATA_INSTANCE].set_enable(True) |
100 | 116 |
|
| 117 | + |
| 118 | +async def _async_handle_disable_service(service: ServiceCall) -> None: |
| 119 | + service.hass.data[DATA_INSTANCE].set_enable(False) |
| 120 | + |
| 121 | + |
| 122 | +async def _async_handle_get_statistics_service( |
| 123 | + service: ServiceCall, |
| 124 | +) -> ServiceResponse: |
| 125 | + """Handle calls to the get_statistics service.""" |
| 126 | + hass = service.hass |
| 127 | + start_time = dt_util.as_utc(service.data["start_time"]) |
| 128 | + end_time = ( |
| 129 | + dt_util.as_utc(service.data["end_time"]) if "end_time" in service.data else None |
| 130 | + ) |
| 131 | + |
| 132 | + statistic_ids = service.data["statistic_ids"] |
| 133 | + types = service.data["types"] |
| 134 | + period = service.data["period"] |
| 135 | + units = service.data.get("units") |
| 136 | + |
| 137 | + result = await hass.data[DATA_INSTANCE].async_add_executor_job( |
| 138 | + statistics_during_period, |
| 139 | + hass, |
| 140 | + start_time, |
| 141 | + end_time, |
| 142 | + statistic_ids, |
| 143 | + period, |
| 144 | + units, |
| 145 | + types, |
| 146 | + ) |
| 147 | + |
| 148 | + formatted_result: JsonObjectType = {} |
| 149 | + for statistic_id, statistic_rows in result.items(): |
| 150 | + formatted_statistic_rows: JsonArrayType = [] |
| 151 | + |
| 152 | + for row in statistic_rows: |
| 153 | + formatted_row: JsonObjectType = { |
| 154 | + "start": dt_util.utc_from_timestamp(row["start"]).isoformat(), |
| 155 | + "end": dt_util.utc_from_timestamp(row["end"]).isoformat(), |
| 156 | + } |
| 157 | + if (last_reset := row.get("last_reset")) is not None: |
| 158 | + formatted_row["last_reset"] = dt_util.utc_from_timestamp( |
| 159 | + last_reset |
| 160 | + ).isoformat() |
| 161 | + if (state := row.get("state")) is not None: |
| 162 | + formatted_row["state"] = state |
| 163 | + if (sum_value := row.get("sum")) is not None: |
| 164 | + formatted_row["sum"] = sum_value |
| 165 | + if (min_value := row.get("min")) is not None: |
| 166 | + formatted_row["min"] = min_value |
| 167 | + if (max_value := row.get("max")) is not None: |
| 168 | + formatted_row["max"] = max_value |
| 169 | + if (mean := row.get("mean")) is not None: |
| 170 | + formatted_row["mean"] = mean |
| 171 | + if (change := row.get("change")) is not None: |
| 172 | + formatted_row["change"] = change |
| 173 | + |
| 174 | + formatted_statistic_rows.append(formatted_row) |
| 175 | + |
| 176 | + formatted_result[statistic_id] = formatted_statistic_rows |
| 177 | + |
| 178 | + return {"statistics": formatted_result} |
| 179 | + |
| 180 | + |
| 181 | +@callback |
| 182 | +def async_setup_services(hass: HomeAssistant) -> None: |
| 183 | + """Register recorder services.""" |
101 | 184 | async_register_admin_service( |
102 | 185 | hass, |
103 | 186 | DOMAIN, |
104 | 187 | SERVICE_PURGE, |
105 | | - async_handle_purge_service, |
| 188 | + _async_handle_purge_service, |
106 | 189 | schema=SERVICE_PURGE_SCHEMA, |
107 | 190 | ) |
108 | 191 |
|
109 | | - |
110 | | -@callback |
111 | | -def _async_register_purge_entities_service( |
112 | | - hass: HomeAssistant, instance: Recorder |
113 | | -) -> None: |
114 | | - async def async_handle_purge_entities_service(service: ServiceCall) -> None: |
115 | | - """Handle calls to the purge entities service.""" |
116 | | - entity_ids = await async_extract_entity_ids(hass, service) |
117 | | - domains = service.data.get(ATTR_DOMAINS, []) |
118 | | - keep_days = service.data.get(ATTR_KEEP_DAYS, 0) |
119 | | - entity_globs = service.data.get(ATTR_ENTITY_GLOBS, []) |
120 | | - entity_filter = generate_filter(domains, list(entity_ids), [], [], entity_globs) |
121 | | - purge_before = dt_util.utcnow() - timedelta(days=keep_days) |
122 | | - instance.queue_task(PurgeEntitiesTask(entity_filter, purge_before)) |
123 | | - |
124 | 192 | async_register_admin_service( |
125 | 193 | hass, |
126 | 194 | DOMAIN, |
127 | 195 | SERVICE_PURGE_ENTITIES, |
128 | | - async_handle_purge_entities_service, |
| 196 | + _async_handle_purge_entities_service, |
129 | 197 | schema=SERVICE_PURGE_ENTITIES_SCHEMA, |
130 | 198 | ) |
131 | 199 |
|
132 | | - |
133 | | -@callback |
134 | | -def _async_register_enable_service(hass: HomeAssistant, instance: Recorder) -> None: |
135 | | - async def async_handle_enable_service(service: ServiceCall) -> None: |
136 | | - instance.set_enable(True) |
137 | | - |
138 | 200 | async_register_admin_service( |
139 | 201 | hass, |
140 | 202 | DOMAIN, |
141 | 203 | SERVICE_ENABLE, |
142 | | - async_handle_enable_service, |
| 204 | + _async_handle_enable_service, |
143 | 205 | schema=SERVICE_ENABLE_SCHEMA, |
144 | 206 | ) |
145 | 207 |
|
146 | | - |
147 | | -@callback |
148 | | -def _async_register_disable_service(hass: HomeAssistant, instance: Recorder) -> None: |
149 | | - async def async_handle_disable_service(service: ServiceCall) -> None: |
150 | | - instance.set_enable(False) |
151 | | - |
152 | 208 | async_register_admin_service( |
153 | 209 | hass, |
154 | 210 | DOMAIN, |
155 | 211 | SERVICE_DISABLE, |
156 | | - async_handle_disable_service, |
| 212 | + _async_handle_disable_service, |
157 | 213 | schema=SERVICE_DISABLE_SCHEMA, |
158 | 214 | ) |
159 | 215 |
|
160 | | - |
161 | | -@callback |
162 | | -def _async_register_get_statistics_service( |
163 | | - hass: HomeAssistant, instance: Recorder |
164 | | -) -> None: |
165 | | - async def async_handle_get_statistics_service( |
166 | | - service: ServiceCall, |
167 | | - ) -> ServiceResponse: |
168 | | - """Handle calls to the get_statistics service.""" |
169 | | - start_time = dt_util.as_utc(service.data["start_time"]) |
170 | | - end_time = ( |
171 | | - dt_util.as_utc(service.data["end_time"]) |
172 | | - if "end_time" in service.data |
173 | | - else None |
174 | | - ) |
175 | | - |
176 | | - statistic_ids = service.data["statistic_ids"] |
177 | | - types = service.data["types"] |
178 | | - period = service.data["period"] |
179 | | - units = service.data.get("units") |
180 | | - |
181 | | - result = await instance.async_add_executor_job( |
182 | | - statistics_during_period, |
183 | | - hass, |
184 | | - start_time, |
185 | | - end_time, |
186 | | - statistic_ids, |
187 | | - period, |
188 | | - units, |
189 | | - types, |
190 | | - ) |
191 | | - |
192 | | - formatted_result: JsonObjectType = {} |
193 | | - for statistic_id, statistic_rows in result.items(): |
194 | | - formatted_statistic_rows: JsonArrayType = [] |
195 | | - |
196 | | - for row in statistic_rows: |
197 | | - formatted_row: JsonObjectType = { |
198 | | - "start": dt_util.utc_from_timestamp(row["start"]).isoformat(), |
199 | | - "end": dt_util.utc_from_timestamp(row["end"]).isoformat(), |
200 | | - } |
201 | | - if (last_reset := row.get("last_reset")) is not None: |
202 | | - formatted_row["last_reset"] = dt_util.utc_from_timestamp( |
203 | | - last_reset |
204 | | - ).isoformat() |
205 | | - if (state := row.get("state")) is not None: |
206 | | - formatted_row["state"] = state |
207 | | - if (sum_value := row.get("sum")) is not None: |
208 | | - formatted_row["sum"] = sum_value |
209 | | - if (min_value := row.get("min")) is not None: |
210 | | - formatted_row["min"] = min_value |
211 | | - if (max_value := row.get("max")) is not None: |
212 | | - formatted_row["max"] = max_value |
213 | | - if (mean := row.get("mean")) is not None: |
214 | | - formatted_row["mean"] = mean |
215 | | - if (change := row.get("change")) is not None: |
216 | | - formatted_row["change"] = change |
217 | | - |
218 | | - formatted_statistic_rows.append(formatted_row) |
219 | | - |
220 | | - formatted_result[statistic_id] = formatted_statistic_rows |
221 | | - |
222 | | - return {"statistics": formatted_result} |
223 | | - |
224 | 216 | async_register_admin_service( |
225 | 217 | hass, |
226 | 218 | DOMAIN, |
227 | 219 | SERVICE_GET_STATISTICS, |
228 | | - async_handle_get_statistics_service, |
| 220 | + _async_handle_get_statistics_service, |
229 | 221 | schema=SERVICE_GET_STATISTICS_SCHEMA, |
230 | 222 | supports_response=SupportsResponse.ONLY, |
231 | 223 | ) |
232 | | - |
233 | | - |
234 | | -@callback |
235 | | -def async_register_services(hass: HomeAssistant, instance: Recorder) -> None: |
236 | | - """Register recorder services.""" |
237 | | - _async_register_purge_service(hass, instance) |
238 | | - _async_register_purge_entities_service(hass, instance) |
239 | | - _async_register_enable_service(hass, instance) |
240 | | - _async_register_disable_service(hass, instance) |
241 | | - _async_register_get_statistics_service(hass, instance) |
0 commit comments