Page 1 of 1
Xiaomi Gateway FM
Posted: Friday 06 April 2018 17:42
by hekm77
Xiaomi Gateway FM in
Domoticz. Playing your channels from MiHome app favorites, obtaining (updating) the status.
- Install php-miio
- Spoiler: show
Code: Select all
sudo apt update
sudo apt upgrade
sudo apt install php7.0
git clone https://github.com/skysilver-lab/php-miio.git php-miio
- Get the gateway token and IP:
- Spoiler: show
Code: Select all
cd /home/pi/php-miio/
php miio-cli.php --discover all
Or from miio2.db in MiHome (up to 5.0.31)
Or in MiHome, the tab is located next to developer mode activation.
- Create two dummy switches miGateway FM and miGateway FM - Source - type: Selector Switch with levels.
- Spoiler: show

- switch.png (85.99 KiB) Viewed 3211 times
- Spoiler: show

- fm.png (251.76 KiB) Viewed 3211 times
- Spoiler: show

- source.png (310.04 KiB) Viewed 3211 times
- Create a dzVents script. Change php-miio path, gateway token and IP.
- Spoiler: show
Code: Select all
return {
on = {
--timer = {'every minute'},
devices = {'miGateway FM', 'miGateway FM - Source'}
},
logging = {
level = domoticz.LOG_ERROR
},
execute = function(dz, item)
local Path = "/home/pi/php-miio/" -- php-miio path
local IP = '192.168.1.50' -- Xiaomi Gateway IP
local Token = 'x1x1x1x1x1x1x1x1x1x1x1x1x1x1x1x1' -- Xiaomi Gateway Token
local FM = dz.devices('miGateway FM')
local Source = dz.devices('miGateway FM - Source')
-- Channel ID, Channel Name
ch1ID, ch1Name = '527782024', 'Европа Плюс'
ch2ID, ch2Name = '527782027', 'Top 100 Rap'
ch3ID, ch3Name = '527782029', 'Energy FM'
ch4ID, ch4Name = '527782034', 'Sound Park Deep'
ch5ID, ch5Name = '527782069', 'Аплюс Relax BY'
chID = {}
chID[#chID+1] = {ch1ID, ch1Name}
chID[#chID+1] = {ch2ID, ch2Name}
chID[#chID+1] = {ch3ID, ch3Name}
chID[#chID+1] = {ch4ID, ch4Name}
chID[#chID+1] = {ch5ID, ch5Name}
function SendCommand()
cmd = "cd "..Path.." && php miio-cli.php --ip "..IP.." --token "..Token.." --sendcmd '{\"id\":1,\"method\":\""..method.."\",\"params\":"..param.."}'"
end
if (item.isDevice) then
if (item.name == 'miGateway FM') then
if (item.levelName == 'Get Channel ID') then
method = "get_prop_fm"
param = "[]"
SendCommand()
local h = io.popen(cmd)
local r = h:read("*a")
h:close()
_, _, num1, num2, num3, num4, channel = string.find(r, "(%d+)[^%d]+(%d+)[^%d]+(%d+)[^%d]+(%d+)[^%d]+(%d+)[^%d]")
print('Channel ID = '..channel)
item.switchOff().afterSec(1).silent()
else
method = "play_fm"
if (item.levelName == 'On') then
param = "[\"on\"]"
elseif
(item.levelName == 'Off') then
param = "[\"off\"]"
Source.switchOff().checkFirst().silent()
end
SendCommand()
dz.utils.osExecute('('..cmd..' > /dev/null)&')
end
end
if (item.name == 'miGateway FM - Source') then
if (item.state == 'Off') then
method = "play_fm"
param = "[\"off\"]"
dz.devices('miGateway FM').switchOff().silent()
else
for i in pairs(chID) do
if (item.levelName == chID[i][2]) then
method = "play_specify_fm"
param = "["..chID[i][1].."]"
if (FM.levelName ~= 'On') then
FM.switchSelector(30).silent()
end
end
end
end
SendCommand()
dz.utils.osExecute('('..cmd..' > /dev/null)&')
end
end
if (item.isTimer) then
method = "get_prop_fm"
param = "[]"
SendCommand()
local h = io.popen(cmd)
local r = h:read("*a")
h:close()
if (string.find(r, '"pause"')) then
Source.switchOff().checkFirst().silent()
FM.switchOff().checkFirst().silent()
end
if (string.find(r, '"run"')) then
_, _, num1, num2, num3, num4, channel = string.find(r, "(%d+)[^%d]+(%d+)[^%d]+(%d+)[^%d]+(%d+)[^%d]+(%d+)[^%d]")
if (channel == ch1ID and Source.levelName ~= ch1Name) then Source.switchSelector(10).silent()
elseif (channel == ch2ID and Source.levelName ~= ch2Name) then Source.switchSelector(20).silent()
elseif (channel == ch3ID and Source.levelName ~= ch3Name) then Source.switchSelector(30).silent()
elseif (channel == ch4ID and Source.levelName ~= ch4Name) then Source.switchSelector(40).silent()
elseif (channel == ch5ID and Source.levelName ~= ch5Name) then Source.switchSelector(50).silent()
elseif (channel ~= ch1ID)
and (channel ~= ch2ID)
and (channel ~= ch3ID)
and (channel ~= ch4ID)
and (channel ~= ch5ID) then
if (Source.levelName ~= 'Unknown') then
Source.switchSelector(60).silent()
end
end
if (FM.levelName ~= 'On') then
FM.switchSelector(30).silent()
end
end
end
end
}
- Play a favorite channel in the MiHome app. In the Domoticz GUI click on the Get Channel ID on the miGateway FM switch. The ID of the channel will be shown in the log.
- Spoiler: show
Code: Select all
2018-04-04 12:06:07.502 User: Admin initiated a switch command (86/miGateway FM/Set Level)
2018-04-04 12:06:07.511 (Dummy) Light/Switch (miGateway FM)
2018-04-04 12:06:08.135 dzVents: Channel ID = 527782027
- Copy, paste it and the channel name into the script. Make for all the necessary channels. In the example 5 channels.
- The channel ID can also be obtained by running in the terminal:
- Spoiler: show
Code: Select all
cd /home/pi/php-miio/ && php miio-cli.php --ip 192.168.1.50 --token xzxzzxzxzzzzzzzx --sendcmd '{"id":1,"method":"get_prop_fm","params":[]}'
- If you do not start a channel from MiHome, will be received the channel ID that was last played and now paused.
- Edit the switch miGateway FM - Source. Change the levels names to your own. The Unknown level is required!
- Uncomment the line 3 in the script (timer = {'every minute'},). To retrieve (update) playback information every minute.
- Listen your favorite radio stations from Domoticz