Skip to content

Commit

Permalink
fix(permit_join): update to new permit join API
Browse files Browse the repository at this point in the history
  • Loading branch information
Koenkk committed Dec 8, 2024
1 parent ed5a73b commit 7ca8026
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
21 changes: 18 additions & 3 deletions src/components/navbar/StartStopJoinButton.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { RefObject, useState } from 'react';
import React, { RefObject, useState, useEffect } from 'react';
import Button from '../button';
import cx from 'classnames';
import useComponentVisible from '../../hooks/useComponentVisible';
Expand All @@ -14,8 +14,8 @@ export function StartStopJoinButton({ devices, setPermitJoin, bridgeInfo }: Star
const { t } = useTranslation(['navbar']);
const { ref, isComponentVisible, setIsComponentVisible } = useComponentVisible(false);
const [selectedRouter, setSelectedRouter] = useState<Device>({} as Device);
const { permit_join_timeout: permitJoinTimeout } = bridgeInfo;
const permitJoin = permitJoinTimeout > 0;
const [permitJoinTimeout, setPermitJoinTimeout] = useState<number | undefined>(undefined);
const { permit_join: permitJoin, permit_join_end: permitJoinEnd } = bridgeInfo;

const selectAndHide = (device: Device) => {
setSelectedRouter(device);
Expand All @@ -39,6 +39,21 @@ export function StartStopJoinButton({ devices, setPermitJoin, bridgeInfo }: Star
const onBtnClick = () => {
setPermitJoin(permitJoin ? 0 : 254, selectedRouter);
};
const updatePermitJoinTimeout = () => {
setPermitJoinTimeout(permitJoinEnd ? Math.round((permitJoinEnd - Date.now()) / 1000) : undefined);
};

useEffect(() => {
let interval;
updatePermitJoinTimeout();

if (permitJoinEnd) {
interval = setInterval(() => updatePermitJoinTimeout(), 1000);
}

return () => clearInterval(interval);
}, [permitJoinEnd]);

const permitJoinTimer = (
<>
{permitJoinTimeout ? (
Expand Down
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ export type BridgeState = 'online' | 'offline';
export interface BridgeInfo {
config: Z2MConfig;
config_schema: JSONSchema7;
permit_join_timeout: number;
permit_join: boolean;
permit_join_end: number | undefined;
commit?: string;
version?: string;
zigbee_herdsman_converters: { version: string };
Expand Down

0 comments on commit 7ca8026

Please sign in to comment.