Skip to content

Commit

Permalink
Fix GladysAssistant#542 : Filter Xiaomi discovered devices
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Gilles committed Oct 29, 2019
1 parent a9f1ee9 commit a409b97
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
4 changes: 3 additions & 1 deletion front/src/config/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,9 @@
"setup": {
"title": "Xiaomi devices not in Gladys",
"description": "Devices detected by the Xiaomi Gateway will be added here when they send data. Your Xiaomi Gateway need to be in developer mode. Learn more <a href=\"https://documentation.gladysassistant.com/en/configuration#xiaomi-configuration\">here</a>.",
"createDeviceInGladys": "Connect in Gladys"
"createDeviceInGladys": "Connect in Gladys",
"createDeviceError": "There was an error creating this device in Gladys. ",
"deviceCreatedSuccess": "The device was added with success."
},
"models": {
"motion": "Motion Sensor",
Expand Down
1 change: 1 addition & 0 deletions front/src/routes/integration/all/xiaomi/DevicePanel.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const DevicePanel = ({ children, ...props }) => (
type="text"
class="form-control w-10"
placeholder={<Text id="integration.xiaomi.device.searchPlaceholder" />}
value={props.xiaomiDeviceSearch}
onInput={props.debouncedSearch}
/>
</Localizer>
Expand Down
17 changes: 16 additions & 1 deletion front/src/routes/integration/all/xiaomi/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,16 @@ function createActions(store) {
});
try {
const xiaomiSensors = await state.httpClient.get('/api/v1/service/xiaomi/sensor');

// remove sensors which are already added
const xiaomiSensorsFiltered = xiaomiSensors.filter(sensor => {
if (!state.xiaomiDevicesMap) {
return true;
}
return !state.xiaomiDevicesMap.has(sensor.external_id);
});
store.setState({
xiaomiSensors,
xiaomiSensors: xiaomiSensorsFiltered,
getXiaomiSensorsStatus: RequestStatus.Success
});
} catch (e) {
Expand All @@ -39,10 +47,17 @@ function createActions(store) {
options.search = state.xiaomiDeviceSearch;
}
const xiaomiDevices = await state.httpClient.get('/api/v1/service/xiaomi/device', options);

const xiaomiDevicesMap = new Map();
xiaomiDevices.forEach(xiaomiDevice => {
xiaomiDevicesMap.set(xiaomiDevice.external_id, xiaomiDevice);
});
store.setState({
xiaomiDevices,
xiaomiDevicesMap,
getXiaomiDevicesStatus: RequestStatus.Success
});
actions.getXiaomiSensors(store.getState());
} catch (e) {
store.setState({
getXiaomiDevicesStatus: RequestStatus.Error
Expand Down
4 changes: 2 additions & 2 deletions front/src/routes/integration/all/xiaomi/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import DevicePanel from './DevicePanel';
import { WEBSOCKET_MESSAGE_TYPES } from '../../../../../../server/utils/constants';

@connect(
'user,session,xiaomiSensors,xiaomiDevices,houses,getXiaomiDevicesStatus',
'user,session,xiaomiSensors,xiaomiDevices,houses,getXiaomiDevicesStatus,xiaomiDeviceSearch',
actions
)
class XiaomiPage extends Component {
Expand All @@ -23,7 +23,7 @@ class XiaomiPage extends Component {
render(props, {}) {
return (
<XiaomiLayout>
<DevicePanel {...props} />
{props.xiaomiDevices && props.xiaomiDevices.length ? <DevicePanel {...props} /> : <div />}
<SetupPanel {...props} />
</XiaomiLayout>
);
Expand Down

0 comments on commit a409b97

Please sign in to comment.