Bonjour à tous,
Je vous partage un script que j’ai fait pour rajouter à tous mes modules Shelly que j’avais dans jMQTT les commandes non créées automatiquement.
A rajouter dans un bloc code d’un scénario à lancer manuellement (j’utilise le framework SC pour les log) :
// Les équipements individuels sont à créer manuellement dans jMQTT, ils doivent se nommer : Shelly_*
// Commandes à compléter pour chaque type de module : voir Doc API https://shelly-api-docs.shelly.cloud/
$cmdsACreer = array(
"shelly1"=> array(
array("Update FW", "command", "update_fw", "action", "other"),
array("ON", "relay/0/command", "on", "action", "other"),
array("OFF", "relay/0/command", "off", "action", "other"),
array("Toggle", "relay/0/command", "toggle", "action", "other"),
),
"shelly1l"=> array(
array("Update FW", "command", "update_fw", "action", "other"),
array("ON", "relay/0/command", "on", "action", "other"),
array("OFF", "relay/0/command", "off", "action", "other"),
array("Toggle", "relay/0/command", "toggle", "action", "other"),
),
"shellydimmer2"=> array(
array("Update FW", "command", "update_fw", "action", "other"),
array("ON", "light/0/command", "on", "action", "other"),
array("OFF", "light/0/command", "off", "action", "other"),
array("Set_Lum", "light/0/set", '{"brightness": #slider#}', "action", "slider"),
array("Luminosite", "light/0/status{brightness}", "", "info", "numeric"),
)
);
// Équipements jMQTT
$eqs = eqLogic::byType('jMQTT');
//On récupère les équipements
foreach($eqs as $eq){
$name = $eq->getName();
$sc->setLog($name . ' --> '.preg_match('/Shelly_[[:alnum:]]+/',$name));
// filtre les Shelly_*
if(preg_match('/Shelly_[[:alnum:]]+/',$name)){
//décompose l'adresse pour récupérer le type de module [3] et l'adresse sans le # [1]
preg_match("/(([[:alnum:]]+)\/([[:alnum:]]+)-([[:alnum:]]+)\/)#/i",$eq->getLogicalId(),$adress);
//On récupère l'ID de l'équipement
$id = $eq->getId();
foreach($cmdsACreer[$adress[3]] as $cmdACreer){
if(!isCmdExist($id, $cmdACreer[0])){
newCmd($id, $cmdACreer[0], $adress[1].$cmdACreer[1], $cmdACreer[2], $cmdACreer[3], $cmdACreer[4]);
$sc->setLog('Création Cmd : '.$adress[3]." --> ".$cmdACreer[0]." --> ".$adress[1].$cmdACreer[1]);
}
}
}
}
// Création d'une commande action ou info
function newCmd($id, $name, $topic, $value = '', $type = 'info', $subtype = 'string'){
$cmd = new jMQTTCmd();
$cmd->setEqLogic(eqLogic::byId($id));
$cmd->setEqLogic_id($id);
$cmd->setEqType('jMQTT');
$cmd->setIsVisible(1);
$cmd->setIsHistorized(0);
$cmd->setSubType($subtype);
$cmd->setType($type);
$cmd->setTopic($topic);
$cmd->setName($name);
if($type == "action"){
$cmd->setConfiguration('request',$value);
}
$cmd->save();
return $cmd;
}
// Vérifie si la commande existe déjà
function isCmdExist($id, $name){
$cmds = cmd::byEqLogicId($id);
foreach ($cmds as $cmd) {
if($cmd->getName() == $name) {
return true;
}
}
return false;
}
La matrice peut être personnalisée en fonction de vos modules et des commandes à créer.