diff --git a/smart-hut/src/components/dashboard/devices/Curtain.js b/smart-hut/src/components/dashboard/devices/Curtain.js index e055b34..45850f6 100644 --- a/smart-hut/src/components/dashboard/devices/Curtain.js +++ b/smart-hut/src/components/dashboard/devices/Curtain.js @@ -101,6 +101,7 @@ class Curtain extends Component { {Math.round(this.props.stateOrDevice.intensity)}% { const on = !this.turnedOn; - if (this.props.tab === "Devices") { + if (this.props.tab !== "Scenes") { this.props - .saveDevice({ ...this.props.stateOrDevice, on }) + .saveDevice( + { ...this.props.stateOrDevice, on }, + this.props.tab === "Hosts" ? this.props.activeHost : null + ) .catch((err) => console.error("regular light update error", err)); } else { if (this.props.device.kind === "regularLight") { @@ -115,10 +118,13 @@ class Light extends Component { saveIntensity = () => { const intensity = Math.round(this.state.intensity); - if (this.props.tab === "Devices") { + if (this.props.tab !== "Scenes") { this.props - .saveDevice({ ...this.props.stateOrDevice, intensity }) - .catch((err) => console.error("regular light update error", err)); + .saveDevice( + { ...this.props.stateOrDevice, intensity }, + this.props.tab === "Hosts" ? this.props.activeHost : null + ) + .catch((err) => console.error("dimmable light update error", err)); } else { this.props .updateState( @@ -137,8 +143,8 @@ class Light extends Component { + {} : this.onClickDevice}> Smart Plug diff --git a/smart-hut/src/components/dashboard/devices/Switch.js b/smart-hut/src/components/dashboard/devices/Switch.js index 62e9c84..8c4e4d9 100644 --- a/smart-hut/src/components/dashboard/devices/Switch.js +++ b/smart-hut/src/components/dashboard/devices/Switch.js @@ -38,7 +38,7 @@ class Switch extends Component { render() { return ( - + {} : this.onClickDevice}> Switch diff --git a/smart-hut/src/components/dashboard/devices/Thermostats.js b/smart-hut/src/components/dashboard/devices/Thermostats.js index 4eeb868..8fa9df7 100644 --- a/smart-hut/src/components/dashboard/devices/Thermostats.js +++ b/smart-hut/src/components/dashboard/devices/Thermostats.js @@ -99,6 +99,7 @@ class Thermostats extends Component {

Thermostat {this.props.tab === "Devices" ? ( } promise that resolves to the saved device and rejects * with user-fiendly errors as a RemoteError */ - saveDevice: (data) => { + saveDevice: (data, hostId = null) => { return (dispatch) => { let url = "/device"; if ((data.id && data.flowType === "OUTPUT") || !data.id) { url = "/" + data.kind; } - return Endpoint[data.id ? "put" : "post"](url, {}, data) + return Endpoint[data.id ? "put" : "post"]( + url, + hostId ? { hostId } : {}, + data + ) .then((res) => { - dispatch(actions.deviceSave(res.data)); + dispatch( + hostId + ? actions.hostDeviceSave(hostId, res.data) + : actions.deviceSave(res.data) + ); return res.data; }) .catch((err) => { diff --git a/smart-hut/src/store.js b/smart-hut/src/store.js index 2050fd5..e68060f 100644 --- a/smart-hut/src/store.js +++ b/smart-hut/src/store.js @@ -3,6 +3,7 @@ import thunk from "redux-thunk"; import update from "immutability-helper"; import reduxWebSocket, { connect } from "@giantmachines/redux-websocket"; import { socketURL } from "./endpoint"; +import actions from "./storeActions"; function reducer(previousState, action) { let newState, change; @@ -333,6 +334,18 @@ function reducer(previousState, action) { } newState = update(previousState, change); break; + case "HOST_DEVICE_SAVE": + change = { + hostDevices: { + [action.hostId]: { + [action.device.id]: { + $set: action.device, + }, + }, + }, + }; + newState = update(previousState, change); + break; case "AUTOMATION_SAVE": change = { @@ -491,7 +504,6 @@ function reducer(previousState, action) { a[hostId].push(e); return a; }, {}); - newState = reducer(previousState, { type: "DEVICES_UPDATE", partial: true, diff --git a/smart-hut/src/storeActions.js b/smart-hut/src/storeActions.js index bfce32b..64c2a52 100644 --- a/smart-hut/src/storeActions.js +++ b/smart-hut/src/storeActions.js @@ -25,6 +25,11 @@ const actions = { type: "DEVICE_SAVE", device, }), + hostDeviceSave: (hostId, device) => ({ + type: "HOST_DEVICE_SAVE", + hostId, + device, + }), triggerSave: (automation) => ({ type: "TRIGGER_SAVE", automation,