Skip to content

Commit

Permalink
Fetch history with no_attributes for entities that do not need them (
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco committed Mar 21, 2022
1 parent 9c1d1cb commit ddf1cc0
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 12 deletions.
19 changes: 17 additions & 2 deletions src/data/cached-history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
HistoryResult,
LineChartUnit,
TimelineEntity,
entityIdHistoryNeedsAttributes,
} from "./history";

export interface CacheConfig {
Expand Down Expand Up @@ -53,7 +54,17 @@ export const getRecent = (
return cache.data;
}

const prom = fetchRecent(hass, entityId, startTime, endTime).then(
const noAttributes = !entityIdHistoryNeedsAttributes(hass, entityId);
const prom = fetchRecent(
hass,
entityId,
startTime,
endTime,
false,
undefined,
true,
noAttributes
).then(
(stateHistory) => computeHistory(hass, stateHistory, localize),
(err) => {
delete RECENT_CACHE[entityId];
Expand Down Expand Up @@ -120,6 +131,7 @@ export const getRecentWithCache = (
}

const curCacheProm = cache.prom;
const noAttributes = !entityIdHistoryNeedsAttributes(hass, entityId);

const genProm = async () => {
let fetchedHistory: HassEntity[][];
Expand All @@ -132,7 +144,10 @@ export const getRecentWithCache = (
entityId,
toFetchStartTime,
endTime,
appendingToCache
appendingToCache,
undefined,
true,
noAttributes
),
]);
fetchedHistory = results[1];
Expand Down
48 changes: 38 additions & 10 deletions src/data/history.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { HassEntity } from "home-assistant-js-websocket";
import { computeDomain } from "../common/entity/compute_domain";
import { computeStateDisplay } from "../common/entity/compute_state_display";
import { computeStateDomain } from "../common/entity/compute_state_domain";
import { computeStateName } from "../common/entity/compute_state_name";
Expand All @@ -7,6 +8,13 @@ import { HomeAssistant } from "../types";
import { FrontendLocaleData } from "./translation";

const DOMAINS_USE_LAST_UPDATED = ["climate", "humidifier", "water_heater"];
const NEED_ATTRIBUTE_DOMAINS = [
"climate",
"humidifier",
"input_datetime",
"thermostat",
"water_heater",
];
const LINE_ATTRIBUTES_TO_KEEP = [
"temperature",
"current_temperature",
Expand Down Expand Up @@ -131,14 +139,22 @@ export interface StatisticsValidationResults {
[statisticId: string]: StatisticsValidationResult[];
}

export const entityIdHistoryNeedsAttributes = (
hass: HomeAssistant,
entityId: string
) =>
!hass.states[entityId] ||
NEED_ATTRIBUTE_DOMAINS.includes(computeDomain(entityId));

export const fetchRecent = (
hass: HomeAssistant,
entityId: string,
startTime: Date,
endTime: Date,
skipInitialState = false,
significantChangesOnly?: boolean,
minimalResponse = true
minimalResponse = true,
noAttributes?: boolean
): Promise<HassEntity[][]> => {
let url = "history/period";
if (startTime) {
Expand All @@ -157,7 +173,9 @@ export const fetchRecent = (
if (minimalResponse) {
url += "&minimal_response";
}

if (noAttributes) {
url += "&no_attributes";
}
return hass.callApi("GET", url);
};

Expand All @@ -171,6 +189,10 @@ export const fetchDate = (
"GET",
`history/period/${startTime.toISOString()}?end_time=${endTime.toISOString()}&minimal_response${
entityId ? `&filter_entity_id=${entityId}` : ``
}${
entityId && !entityIdHistoryNeedsAttributes(hass, entityId)
? `&no_attributes`
: ``
}`
);

Expand Down Expand Up @@ -278,6 +300,10 @@ const processLineChartEntities = (
};
};

const stateUsesUnits = (state: HassEntity) =>
"unit_of_measurement" in state.attributes ||
"state_class" in state.attributes;

export const computeHistory = (
hass: HomeAssistant,
stateHistory: HassEntity[][],
Expand All @@ -294,16 +320,18 @@ export const computeHistory = (
return;
}

const stateWithUnitorStateClass = stateInfo.find(
(state) =>
state.attributes &&
("unit_of_measurement" in state.attributes ||
"state_class" in state.attributes)
);
const entityId = stateInfo[0].entity_id;
const currentState =
entityId in hass.states ? hass.states[entityId] : undefined;
const stateWithUnitorStateClass =
!currentState &&
stateInfo.find((state) => state.attributes && stateUsesUnits(state));

let unit: string | undefined;

if (stateWithUnitorStateClass) {
if (currentState && stateUsesUnits(currentState)) {
unit = currentState.attributes.unit_of_measurement || " ";
} else if (stateWithUnitorStateClass) {
unit = stateWithUnitorStateClass.attributes.unit_of_measurement || " ";
} else {
unit = {
Expand All @@ -313,7 +341,7 @@ export const computeHistory = (
input_number: "#",
number: "#",
water_heater: hass.config.unit_system.temperature,
}[computeStateDomain(stateInfo[0])];
}[computeDomain(entityId)];
}

if (!unit) {
Expand Down

0 comments on commit ddf1cc0

Please sign in to comment.