forked from GladysAssistant/Gladys
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sonos Integration (GladysAssistant#1949)
- Loading branch information
1 parent
aeddefc
commit bfbf817
Showing
40 changed files
with
1,959 additions
and
2 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { Component } from 'preact'; | ||
import { Text } from 'preact-i18n'; | ||
import Select from 'react-select'; | ||
import { connect } from 'unistore/preact'; | ||
|
||
import BaseEditBox from '../baseEditBox'; | ||
import { DEVICE_FEATURE_CATEGORIES } from '../../../../../server/utils/constants'; | ||
|
||
class EditMusicBoxComponent extends Component { | ||
updateDevice = option => { | ||
this.props.updateBoxConfig(this.props.x, this.props.y, { | ||
device: option ? option.value : null | ||
}); | ||
}; | ||
|
||
getDevices = async () => { | ||
try { | ||
await this.setState({ | ||
error: false | ||
}); | ||
const musicDevices = await this.props.httpClient.get('/api/v1/device', { | ||
device_feature_category: DEVICE_FEATURE_CATEGORIES.MUSIC | ||
}); | ||
const musicDevicesOptions = musicDevices.map(d => ({ | ||
label: d.name, | ||
value: d.selector | ||
})); | ||
this.setState({ | ||
musicDevicesOptions | ||
}); | ||
} catch (e) { | ||
console.error(e); | ||
this.setState({ | ||
error: true | ||
}); | ||
} | ||
}; | ||
|
||
componentDidMount() { | ||
this.getDevices(); | ||
} | ||
|
||
render(props, { musicDevicesOptions }) { | ||
let optionSelected = null; | ||
if (musicDevicesOptions && props.box.device) { | ||
optionSelected = musicDevicesOptions.find(o => o.value === props.box.device); | ||
} | ||
return ( | ||
<BaseEditBox {...props} titleKey="dashboard.boxTitle.music"> | ||
<div class="form-group"> | ||
<label> | ||
<Text id="dashboard.boxes.music.selectDeviceLabel" /> | ||
</label> | ||
<Select | ||
defaultValue={null} | ||
value={optionSelected} | ||
onChange={this.updateDevice} | ||
options={musicDevicesOptions} | ||
/> | ||
</div> | ||
</BaseEditBox> | ||
); | ||
} | ||
} | ||
|
||
export default connect('httpClient', {})(EditMusicBoxComponent); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
import { Component } from 'preact'; | ||
import { connect } from 'unistore/preact'; | ||
|
||
import { | ||
WEBSOCKET_MESSAGE_TYPES, | ||
DEVICE_FEATURE_TYPES, | ||
MUSIC_PLAYBACK_STATE | ||
} from '../../../../../server/utils/constants'; | ||
|
||
class MusicComponent extends Component { | ||
state = { | ||
isPlaying: null | ||
}; | ||
getDevice = async () => { | ||
try { | ||
await this.setState({ | ||
error: false | ||
}); | ||
const musicDevice = await this.props.httpClient.get(`/api/v1/device/${this.props.box.device}`, {}); | ||
const playFeature = musicDevice.features.find(f => f.type === DEVICE_FEATURE_TYPES.MUSIC.PLAY); | ||
const pauseFeature = musicDevice.features.find(f => f.type === DEVICE_FEATURE_TYPES.MUSIC.PAUSE); | ||
const previousFeature = musicDevice.features.find(f => f.type === DEVICE_FEATURE_TYPES.MUSIC.PREVIOUS); | ||
const nextFeature = musicDevice.features.find(f => f.type === DEVICE_FEATURE_TYPES.MUSIC.NEXT); | ||
const volumeFeature = musicDevice.features.find(f => f.type === DEVICE_FEATURE_TYPES.MUSIC.VOLUME); | ||
const playBackStateFeature = musicDevice.features.find(f => f.type === DEVICE_FEATURE_TYPES.MUSIC.PLAYBACK_STATE); | ||
const isPlaying = playBackStateFeature.last_value === MUSIC_PLAYBACK_STATE.PLAYING; | ||
this.setState({ | ||
musicDevice, | ||
playFeature, | ||
pauseFeature, | ||
previousFeature, | ||
nextFeature, | ||
volumeFeature, | ||
playBackStateFeature, | ||
isPlaying | ||
}); | ||
} catch (e) { | ||
console.error(e); | ||
this.setState({ | ||
error: true | ||
}); | ||
} | ||
}; | ||
|
||
setValueDevice = async (deviceFeature, value) => { | ||
try { | ||
await this.setState({ error: false }); | ||
await this.props.httpClient.post(`/api/v1/device_feature/${deviceFeature.selector}/value`, { | ||
value | ||
}); | ||
} catch (e) { | ||
console.error(e); | ||
this.setState({ error: true }); | ||
} | ||
}; | ||
|
||
play = async () => { | ||
await this.setState({ isPlaying: true }); | ||
await this.setValueDevice(this.state.playFeature, 1); | ||
}; | ||
pause = async () => { | ||
await this.setState({ isPlaying: false }); | ||
await this.setValueDevice(this.state.pauseFeature, 1); | ||
}; | ||
next = async () => { | ||
await this.setValueDevice(this.state.nextFeature, 1); | ||
}; | ||
previous = async () => { | ||
await this.setValueDevice(this.state.previousFeature, 1); | ||
}; | ||
changeVolume = async e => { | ||
const volume = parseInt(e.target.value, 10); | ||
const newVolumeFeature = { ...this.state.volumeFeature, last_value: volume }; | ||
await this.setState({ volumeFeature: newVolumeFeature }); | ||
await this.setValueDevice(this.state.volumeFeature, volume, 10); | ||
}; | ||
|
||
updateDeviceStateWebsocket = payload => { | ||
if (payload.device_feature_selector === this.state.playBackStateFeature.selector) { | ||
const isPlaying = payload.last_value === MUSIC_PLAYBACK_STATE.PLAYING; | ||
this.setState({ isPlaying }); | ||
} | ||
if (payload.device_feature_selector === this.state.volumeFeature.selector) { | ||
const newVolumeFeature = { ...this.state.volumeFeature, last_value: payload.last_value }; | ||
this.setState({ volumeFeature: newVolumeFeature }); | ||
} | ||
}; | ||
|
||
componentDidMount() { | ||
this.getDevice(); | ||
this.props.session.dispatcher.addListener( | ||
WEBSOCKET_MESSAGE_TYPES.DEVICE.NEW_STATE, | ||
this.updateDeviceStateWebsocket | ||
); | ||
} | ||
|
||
componentWillUnmount() { | ||
this.props.session.dispatcher.removeListener( | ||
WEBSOCKET_MESSAGE_TYPES.DEVICE.NEW_STATE, | ||
this.updateDeviceStateWebsocket | ||
); | ||
} | ||
|
||
render(props, { isPlaying, musicDevice, previousFeature, nextFeature, volumeFeature }) { | ||
return ( | ||
<div class="card"> | ||
<div class="card-header"> | ||
<h3 class="card-title">{musicDevice && musicDevice.name}</h3> | ||
</div> | ||
<div class="card-body"> | ||
<div class="row"> | ||
<div class="col"> | ||
{previousFeature && ( | ||
<button class="btn btn-block btn-secondary" onClick={this.previous}> | ||
<i class="fe fe-skip-back" /> | ||
</button> | ||
)} | ||
</div> | ||
<div class="col"> | ||
{!isPlaying && ( | ||
<button class="btn btn-block btn-secondary" onClick={this.play}> | ||
<i class="fe fe-play" /> | ||
</button> | ||
)} | ||
{isPlaying && ( | ||
<button class="btn btn-block btn-secondary" onClick={this.pause}> | ||
<i class="fe fe-pause" /> | ||
</button> | ||
)} | ||
</div> | ||
<div class="col"> | ||
{nextFeature && ( | ||
<button class="btn btn-block btn-secondary" onClick={this.next}> | ||
<i class="fe fe-skip-forward" /> | ||
</button> | ||
)} | ||
</div> | ||
</div> | ||
{volumeFeature && ( | ||
<div class="row mt-4"> | ||
<div class="col"> | ||
<input | ||
type="range" | ||
value={volumeFeature.last_value} | ||
onChange={this.changeVolume} | ||
class="form-control" | ||
step="1" | ||
min="0" | ||
max="100" | ||
/> | ||
</div> | ||
</div> | ||
)} | ||
</div> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default connect('httpClient,session', {})(MusicComponent); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.