Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Commit 5818018

Browse files
author
Daniel Greco
authored
Merge pull request #44 from AndreyShpilevoy/master
Fix for Remaining/elapsed resetting
2 parents ac9c050 + 2667e75 commit 5818018

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

src/Components/Stats/TimeStat.tsx

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,25 @@ const renderTime = (
4545
}
4646
}
4747

48+
const getTotalSeconds = (
49+
timeEntity: HassEntity,
50+
config: ThreedyConfig
51+
) => {
52+
let result;
53+
if(!config.use_mqtt){
54+
result = parseInt(timeEntity.state) || 0;
55+
} else {
56+
if(timeEntity.state){
57+
const [hours, minutes, seconds] = timeEntity.state.split(':');
58+
result = (+hours) * 60 * 60 + (+minutes) * 60 + (+seconds);
59+
60+
} else {
61+
result = 0;
62+
}
63+
}
64+
return result;
65+
}
66+
4867

4968
type TimeStatProps = {
5069
timeEntity: HassEntity,
@@ -54,8 +73,8 @@ type TimeStatProps = {
5473
}
5574

5675
const TimeStat: React.FC<TimeStatProps> = ({timeEntity, condition, config, direction}) => {
57-
58-
const [ time, setTime ] = useState<number>( parseInt(timeEntity.state) || 0);
76+
const totalSeconds = getTotalSeconds(timeEntity, config);
77+
const [ time, setTime ] = useState<number>(totalSeconds);
5978
const [ lastIntervalId, setLastIntervalId ] = useState<number>(-1);
6079

6180
const incTime = () => setTime( time => (parseInt(time) + parseInt(direction)) );
@@ -64,7 +83,7 @@ const TimeStat: React.FC<TimeStatProps> = ({timeEntity, condition, config, direc
6483

6584
if (lastIntervalId !== -1) clearInterval(lastIntervalId);
6685

67-
setTime(timeEntity.state || 0);
86+
setTime(getTotalSeconds(timeEntity, config));
6887

6988
const id = setInterval(
7089
incTime,

0 commit comments

Comments
 (0)