Welcome to Smart Home Forum by FIBARO
Dear Guest,
as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.
As a member you can:
- Start new topics and reply to others
- Follow topics and users to get email updates
- Get your own profile page and make new friends
- Send personal messages
- ... and learn a lot about our system!
Regards,
Smart Home Forum by FIBARO Team
Search the Community
Showing results for tags 'library'.
-
Here is a code lib to control Sonos - it's under development. The goal is to have a code base that's easy to integrate in other projects or build custom QAs. https://github.com/jangabrielsson/fibemu/blob/main/lib/SonosLib.lua It's based on the local LAN API and using websockets listening for Sonos status changes - no polling(!) Sonos commands sonos:play(playerName) -- Start playing group that player belong to sonos:pause(playerName) -- Pause group that player belong to sonos:volume(playerName,volume) -- Set volume to group that player belong to sonos:relativeVolume(playerName,delta) -- Set relative volume to group that player belong to sonos:mute(playerName,state) -- Mute group that player belong to sonos:togglePlayPause(playerName) -- Toggle play/pause group that player belong to sonos:skipToNextTrack(playerName) -- Skip to next track in group that player belong to sonos:skipToPreviousTrack(playerName) -- Skip to previous track in group that player belong to sonos:playFavorite(playerName,favorite,action,modes) -- Play favorite on group that player belong to sonos:playPlaylist(playerName,playlist,action,modes) -- Play playlist on group that player belong to sonos:playerVolume(playerName,volume) -- Set volume of player sonos:playerRelativeVolume(playerName,delta) -- Set relative volume of player sonos:playerMute(playerName,state) -- Mute player sonos:clip(playerName,url,volume) -- Play audio clip on player sonos:say(playerName,text,volume,lang) -- Play TTS on player sonos:playerGroup(playerName) -- group that player belong to sonos:playersInGroup(playerName) -- players in group that player belong to sonos:createGroup(playerNames,...) -- group players. sonos:removeGroup(groupName) -- remove group. Ex. sonos:removeGroup(sonos:playerGroup(playerName)) sonos:getPlayer(playerName) -- Get player object. Ex. p = sonos:getPlayer(playerName); p:pause() sonos:cb(cb) -- wrap call in callback. Ex. sonos:cb(function(h,data) print(json.encode(h)) end):pause(playerName) Example of use: local function delay(args) local t=0 for i=1,#args,4 do local d,cond,f,doc=args[i],args[i+1],args[i+2],args[i+3] local function f0() print(">"..doc) f() end if cond then t=t+d setTimeout(f0,1000*t) end end end function QuickApp:onInit() self:debug("onInit",self.name,self.id) local clip = "https://github.com/joepv/fibaro/raw/refs/heads/master/sonos-tts-example-eng.mp3" Sonos("192.168.1.225",function(sonos) self:debug("Sonos Ready") function sonos:eventHandler(event) print(event) -- Just print out events, could be used to ex. update UI end print("Players:",sonos.playerNames) print("Groups:",sonos.groupNames) local playerA = sonos.playerNames[1] local playerB = sonos.playerNames[2] local PA = sonos._player[playerA] print("PlayerA:",PA.name,PA.id) local PB = sonos._player[playerB] print("PlayerB:",PB.name,PB.id) local favorite1 = (sonos.favorites[1] or {}).name local playlist1 = (sonos.playlists[1] or {}).name print(("PlayerA='%s', PlayerB='%s'"):format(playerA,playerB)) print(("Favorite1='%s', Playlist1='%s'"):format(favorite1,playlist1)) local function callback(headers,data) print("Callback",headers,data) end delay{ 1,playerA,function() sonos:say(playerA,"Hello world",25) end, "TTS clip to player", 2,playerB,function() sonos:say(playerB,"Hello world again",25) end, "TTS clip to player", 2,playerA,function() sonos:clip(playerA,clip,25) end, "Audio clip to player with volume", 2,playerA,function() sonos:play(playerA) end, "Play group that player belongs to", 2,playerA,function() sonos:pause(playerA) end, "Pause group that player belongs to", 2,playerB,function() sonos:play(playerB) end, "Play group that player belongs to", 2,playerB,function() sonos:cb(callback):pause(playerB) end, "Pause group that player belongs to", 2,playerB and favorite1,function() sonos:playFavorite(playerB,favorite1) end, "Play favorite in group that player belongs to", 4,playerB,function() sonos:pause(playerB) end, "Pause group that player belongs to", 4,playerB and playlist1,function() sonos:playPlaylist(playerB,playlist1) end, "Play favorite in group that player belongs to", 4,playerB,function() sonos:pause(playerB) end, "Pause group that player belongs to", 5,playerA and playerB,function() sonos:createGroup(playerB,playerA) end, "Create group with players", 10,playerA,function() sonos:play(playerA) end, "Play group that playerA belongs to (both players)", 4,playerA,function() sonos:removeGroup(sonos:playerGroup(playerA)) end, "Destroy group playerA belongs to", 4,playerA,function() sonos:play(playerA) end, "Play group that playerA belongs to (only playerA)", } sonos:volume(playerA,40) -- set volume to group that player belongs to sonos:playerVolume(playerA,30) -- set player volume local pl = sonos:getPlayer(playerA) pl:pause() local group = sonos:playerGroup(playerA) -- get group that player belongs to local players = sonos:playersInGroup(sonos:playerGroup(playerA)) -- get players in group end,{socket=true}) -- debug flags end end The sonos object when created does some initialization and when ready call a callback function, in this case the main of the example, logging and testing functionality. More to come... Here is an example of a QA that uses the library. The QA is very reactive on changes in the system. Because the current HC3 UI model is a mess this QA is just a proof-of-concept. The QA's type is com.fibaro.player which doesn't render in the mobile app withe the new UI... However, it works in the web UI. Switching to the old UI model makes the interface a bit ugly as drop-downs miss labels etc... In the future it will hopefully be possible to create more beautiful app compatible QAs... It's also hardcoded to the IP of my sonos, so it requires some coding to get running on your system 😉 SonosPlayer.fqa ---@diagnostic disable: undefined-global -- Player type should handle actions: play, pause, stop, next, prev, setVolume, setMute --%%name=SonosPlayer --%%type=com.fibaro.player local player,sonos -- UI buttons - send command to Sonos function QuickApp:play() sonos:play(player) end function QuickApp:pause() sonos:pause(player) end function QuickApp:stop() sonos:pause(player) end function QuickApp:next() sonos:skipToNextTrack(player) end function QuickApp:prev() sonos:skipToPreviousTrack(player) end function QuickApp:setVolume(volume) sonos:volume(player,volume) end function QuickApp:setMute(mute) sonos:mute(player,mute~=0) end function QuickApp:selectPlayer(ev) player = ev.values[1] self:updatePlayer() end local function mode(m) local group = sonos._group[sonos._player[player].groupId] return (group.playModes or {})[m]==true end function QuickApp:doShuffle() sonos:setModes(player,{shuffle=not mode('shuffle')}) end function QuickApp:doRepeat() sonos:setModes(player,{['repeat']=not mode('repeat')}) end function QuickApp:doRepeat1() sonos:setModes(player,{repeatOne=not mode('repeatOne')}) end function QuickApp:doCrossFade() sonos:setModes(player,{crossfade=not mode('crossfade')}) end function QuickApp:speakTime() sonos:say(player,"Time is "..os.date("%H:%M")) end function QuickApp:favoriteSelected(ev) sonos:playFavorite(player,tostring(ev.values[1])) end function QuickApp:playlistSelected(ev) sonos:playPlaylist(player,tostring(ev.values[1])) end local groupsSelected = {} function QuickApp:groupSelected(ev) groupsSelected=ev.values[1] end function QuickApp:applyGrouping(ev) print("SEL:",json.encode(groupsSelected)) -- sonos.createGroup(groupsSelected) end function QuickApp:setLabel(name,str) --self:updateView(name,"text",string.format("<font size='2'><b>%s</b></font>",str)) self:updateView(name,"text",str) end function QuickApp:updatePlayer() local group = sonos._group[sonos._player[player].groupId] self:updateView('playerLabel','text',"Player: "..(player or "")) self:updateView("statusLabel","text","Status: "..(group.status or "")) self:updateView("artistLabel","text","Artist: "..(group.currentArtist or "")) self:updateView("trackLabel","text","Track: "..(group.currentTrack or "")) self:updateProperty("state",group.status or "") self:updateProperty("volume",group.volume or 0) self:updateProperty("mute",group.muted or false) local modes = group.playModes or {} local m = {} for k,v in pairs(modes) do if v then m[#m+1]=k end end self:updateView("modesLabel","text","Modes: "..table.concat(m,",")) end function QuickApp:setOptions(name,list,fun) local options = {} for _,item in ipairs(list) do local text,value = fun(item) options[#options+1] = {text=text,type='option',value=value} end self:updateView(name,"options",options) end -- Events from the Sonos player local EVENT = {} function EVENT.groupVolume(event) quickApp:updatePlayer() end function EVENT.playerVolume(event) end function EVENT.playbackStatus(event) quickApp:updatePlayer() end function EVENT.metadata(event) quickApp:updatePlayer() end function EVENT.favoritesUpdated() local function fun(i) return i.name,i.id end quickApp:setOptions("favoriteSelector",sonos.favorites,fun) end function EVENT.playlistsUpdated() local function fun(i) return i.name,i.id end quickApp:setOptions("playlistSelector",sonos.playlists,fun) end function QuickApp:onInit() self:debug("Player") quickApp = self Sonos("192.168.1.6",function(_sonos) self:debug("Sonos Ready") sonos = _sonos function sonos:eventHandler(event) if EVENT[event.type] then EVENT[event.type](event) else print("Unhandled event:",json.encode(event)) end end local function fun(i) return i,i end self:setOptions("playerSelector",sonos.playerNames,fun) self:setOptions("groupSelector",sonos.playerNames,fun) player = sonos.playerNames[1] self:updateView("playerSelector","selectedItem", player) self:updateView("playerLabel","text","Player:"..player) end,{socket=true}) -- debug flags end Support will be added for grouping of players. The logic is there but not connected to the UI. The family is using the Sonos player at the moment so I'm not allowed to play with them to verify the code... ;.-)
-
Hi, When building LUA scenes it would be great to have my own utility functions library at hand - not to replicate code, but be able to reuse the code. Like having a global function that does something to strings, or other type conversions. Any idea how to put this (rather obvious for programmers) idea to work on HC2? P.
-
hi all i have created a basic website that is basically a database of LUA code. Some is simple stuff and others a bit more complex. All of it i have tried to put into alphabetical order. At the moment it contains mostly the code i have used and there is a contact me page so that other members can send me code and i can add to this library. Also on there is a section for Virtual Devices, with download links and links back to the thread on this forum. im hoping that this should make finding LUA code easier and also the VD downloads a bit more accessible. thanks the link is: http://www.yorkshireautomation.co.uk/lua