Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dashboard MQTT display the feature name Fixes #787 #852

Merged
merged 2 commits into from
Aug 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getDeviceName } from './utils';

const BinaryDeviceType = ({ children, ...props }) => {
function updateValue() {
props.updateValue(
Expand All @@ -16,7 +18,7 @@ const BinaryDeviceType = ({ children, ...props }) => {
<td>
<i class="fe fe-toggle-right" />
</td>
<td>{props.device.name}</td>
<td>{getDeviceName(props.device, props.deviceFeature)}</td>
<td class="text-right">
<label class="custom-switch">
<input
Expand Down
12 changes: 12 additions & 0 deletions front/src/components/boxs/device-in-room/device-features/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import get from 'get-value';

const DISPLAY_FEATURE_NAME_SERVICES = ['mqtt'];

export const getDeviceName = (device, feature) => {
const service = get(device, 'service.name');
if (service && DISPLAY_FEATURE_NAME_SERVICES.includes(service)) {
return feature.name;
}

return device.name;
};
40 changes: 37 additions & 3 deletions front/src/config/demo.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@
},
{
"type": "devices-in-room",
"room": "living-room"
"room": "living-room",
"device_features": ["main-lamp-binary", "tv-lamp-binary", "mqtt-living-room-switch", "mqtt-living-room-temp"]
}
],
[
Expand Down Expand Up @@ -224,8 +225,8 @@
"selector": "tv-lamp",
"features": [
{
"name": "TV Lamp",
"selector": "main-lamp-binary",
"name": "TV Lamp feature",
"selector": "tv-lamp-binary",
"category": "light",
"type": "binary",
"min": 0,
Expand Down Expand Up @@ -253,6 +254,39 @@
"last_value_changed": "2019-02-12 07:49:07.556 +00:00"
}
]
},
{
"id": "81d637d2-b7f5-4cc3-a39e-2270fd069ee2",
"selector": "mqtt-living-room",
"name": "MQTT device",
"service": {
"name": "mqtt"
},
"features": [
{
"name": "Main Plug",
"selector": "mqtt-living-room-switch",
"category": "switch",
"type": "binary",
"min": 0,
"max": 1,
"read_only": false,
"last_value": 1,
"last_value_changed": "2019-02-12 07:49:07.556 +00:00"
},
{
"name": "Window Temp",
"selector": "mqtt-living-room-temp",
"category": "temperature-sensor",
"type": "decimal",
"unit": "celsius",
"min": -200,
"max": 200,
"read_only": true,
"last_value": 27,
"last_value_changed": "2019-02-12 07:49:07.556 +00:00"
}
]
}
]
},
Expand Down
18 changes: 13 additions & 5 deletions server/lib/room/room.getBySelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const DEVICE_FEATURES_ATTRIBUTES = [
'last_value',
'last_value_changed',
];
const SERVICE_ATTRIBUTES = ['name'];

/**
* @description Get a room by selector
Expand All @@ -34,11 +35,18 @@ async function getBySelector(selector, options) {
model: db.Device,
as: 'devices',
attributes: DEVICE_ATTRIBUTES,
include: {
model: db.DeviceFeature,
as: 'features',
attributes: DEVICE_FEATURES_ATTRIBUTES,
},
include: [
{
model: db.DeviceFeature,
as: 'features',
attributes: DEVICE_FEATURES_ATTRIBUTES,
},
{
model: db.Service,
as: 'service',
attributes: SERVICE_ATTRIBUTES,
},
],
});
}
const room = await db.Room.findOne({
Expand Down
12 changes: 12 additions & 0 deletions server/test/controllers/room/room.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ describe('GET /api/v1/room/:room_selector', () => {
});
});
});
it('should get a room by selector with expanded devices', async () => {
await authenticatedRequest
.get('/api/v1/room/test-room?expand=devices')
.expect('Content-Type', /json/)
.expect(200)
.then((res) => {
expect(res.body).to.have.property('devices');
res.body.devices.forEach((device) => {
expect(device).to.have.property('service');
});
});
});
});

describe('GET /api/v1/room', () => {
Expand Down