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

Commit

Permalink
Merge pull request #44 from AndreyShpilevoy/master
Browse files Browse the repository at this point in the history
Fix for Remaining/elapsed resetting
  • Loading branch information
Daniel Greco authored Nov 25, 2021
2 parents ac9c050 + 2667e75 commit 5818018
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/Components/Stats/TimeStat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,25 @@ const renderTime = (
}
}

const getTotalSeconds = (
timeEntity: HassEntity,
config: ThreedyConfig
) => {
let result;
if(!config.use_mqtt){
result = parseInt(timeEntity.state) || 0;
} else {
if(timeEntity.state){
const [hours, minutes, seconds] = timeEntity.state.split(':');
result = (+hours) * 60 * 60 + (+minutes) * 60 + (+seconds);

} else {
result = 0;
}
}
return result;
}


type TimeStatProps = {
timeEntity: HassEntity,
Expand All @@ -54,8 +73,8 @@ type TimeStatProps = {
}

const TimeStat: React.FC<TimeStatProps> = ({timeEntity, condition, config, direction}) => {

const [ time, setTime ] = useState<number>( parseInt(timeEntity.state) || 0);
const totalSeconds = getTotalSeconds(timeEntity, config);
const [ time, setTime ] = useState<number>(totalSeconds);
const [ lastIntervalId, setLastIntervalId ] = useState<number>(-1);

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

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

setTime(timeEntity.state || 0);
setTime(getTotalSeconds(timeEntity, config));

const id = setInterval(
incTime,
Expand Down

0 comments on commit 5818018

Please sign in to comment.