Skip to content

Commit

Permalink
Merge pull request #20 from jeedom/beta
Browse files Browse the repository at this point in the history
stable
  • Loading branch information
zoic21 authored May 13, 2024
2 parents bf1f086 + 8643f9a commit 129b68a
Show file tree
Hide file tree
Showing 11 changed files with 530 additions and 107 deletions.
148 changes: 146 additions & 2 deletions core/class/netatmo.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,133 @@ public static function cron10(){
log::add('netatmo','debug','Weather : '.$e->getMessage());
}
try {
netatmo_energy::refresh();
netatmo::refreshClassNetatmo();
} catch (\Exception $e) {
log::add('netatmo','debug','Energy : '.$e->getMessage());
}
}

public static function refreshClassNetatmo($homesdata = null){
if($homesdata == null) {
$homesdata = netatmo::request('/homesdata');
}
$home_ids = array();
if(isset($homesdata['homes']) && count($homesdata['homes']) > 0){
foreach ($homesdata['homes'] as $home) {
if(!isset($home['modules'])){
continue;
}
if(isset($home['modules']) && count($home['modules']) > 0){
foreach ($home['modules'] as $module) {
$moduleid = $module['id'];
$ArrayAssocModuleIDtoRoom[$moduleid]= $module['room_id'];
}
}
$home_ids[] = $home['id'];
if(!isset($home['therm_mode'])){
continue;
}
$eqLogic = eqLogic::byLogicalId($home['id'], 'netatmo');
if(!is_object($eqLogic)){
continue;
}
if($home['therm_mode'] != 'schedule'){
$eqLogic->checkAndUpdateCmd('mode',$home['therm_mode']);
continue;
}
if(isset($home['schedules']) && count($home['schedules']) > 0){
$mode = '';
foreach ($home['schedules'] as $schedule) {
if(!$schedule['selected']){
continue;
}
$mode .= $schedule['name'].',';
}
$eqLogic->checkAndUpdateCmd('mode',trim($mode,','));
}
}
}
if(count($home_ids) == 0){
return;
}
foreach ($home_ids as $home_id) {
$homestatus = netatmo::request('/homestatus',array('home_id' => $home_id));
if(isset($homestatus['errors']) && count($homestatus['errors']) > 0){
$error_desc[1] = "Unknown error";
$error_desc[2] = "Internal error";
$error_desc[3] = "Parser error";
$error_desc[4] = "Command unknown node module error";
$error_desc[5] = "Command invalid params";
$error_desc[6] = "Unreachable";
$ModulesError = array();
foreach ($homestatus['errors'] as $deviceerror) {
$ModulesError[$deviceerror['id']] = $deviceerror['code'];
$eqLogic = eqLogic::byLogicalId($deviceerror['id'], 'netatmo');
if(!is_object($eqLogic)){
continue;
}
$Cmderrorinfo = $eqLogic->getCmd('info','error_status');
if(isset($Cmderrorinfo)){
log::add('netatmo','debug','[netatmo cloud] Erreur '.$deviceerror['code'].' détectée sur l\'équipement '.$deviceerror['id']);
$eqLogic->checkAndUpdateCmd("error_status",$deviceerror['code'].' '.$error_desc[$deviceerror['code']]);
}
}
}
if(isset($homestatus['home']) && isset($homestatus['home']['modules']) && count($homestatus['home']['modules']) > 0){
foreach ($homestatus['home']['modules'] as $module) {
if ($module['type']=="OTM" || $module['type']=="NATherm1") {
$eqLogic = eqLogic::byLogicalId($ArrayAssocModuleIDtoRoom[$module['id']], 'netatmo');
if(is_object($eqLogic)){
foreach ($eqLogic->getCmd('info') as $cmd) {
$logicalId = $cmd->getLogicalId();
if(isset($module[$logicalId]) && $cmd->getLogicalID()!="reachable"){
$eqLogic->checkAndUpdateCmd($cmd,$module[$logicalId]);
}
}
}
}
$eqLogic = eqLogic::byLogicalId($module['id'], 'netatmo');
if(!is_object($eqLogic)){
continue;
}
foreach ($eqLogic->getCmd('info') as $cmd) {
$logicalId = $cmd->getLogicalId();
if($logicalId == 'state'){
$logicalId = 'status';
}
if($logicalId == 'error_status'){
if($logicalId == 'error_status' && !isset($ModulesError[$module['id']])) {
$eqLogic->checkAndUpdateCmd($cmd,'0 No error');
}
}
if(!isset($module[$logicalId])){
continue;
}
$eqLogic->checkAndUpdateCmd($cmd,$module[$logicalId]);
}
}
}
if(isset($homestatus['home']) && isset($homestatus['home']['rooms']) && count($homestatus['home']['rooms']) > 0){
foreach ($homestatus['home']['rooms'] as $room) {
$eqLogic = eqLogic::byLogicalId($room['id'], 'netatmo');
if(!is_object($eqLogic)){
continue;
}
foreach ($eqLogic->getCmd('info') as $cmd) {
if(!isset($room[$cmd->getLogicalId()])){
continue;
}
if($cmd->getLogicalId() == 'therm_setpoint_mode' && $room[$cmd->getLogicalId()] != 'schedule' && isset($room['therm_setpoint_end_time'])){
$eqLogic->checkAndUpdateCmd($cmd,$room[$cmd->getLogicalId()].' ('.__('fini à',__FILE__).' '.date('H:i',$room['therm_setpoint_end_time']).')');
continue;
}
$eqLogic->checkAndUpdateCmd($cmd,$room[$cmd->getLogicalId()]);
}
}
}
}
}

public static function cronHourly(){
if(config::byKey('mode', 'netatmo') != 'internal'){
sleep(rand(0,120));
Expand All @@ -109,9 +230,16 @@ public static function request($_path,$_data = null,$_type='GET'){
'Autorization: '.sha512(mb_strtolower(config::byKey('market::username')).':'.config::byKey('market::password'))
));
if($_type == 'POST'){
log::add('netatmo','debug','[netatmo cloud] request : '.$_path);
log::add('netatmo','debug','[netatmo cloud] request (POST json): '.json_encode($_data));
$request_http->setPost(json_encode($_data));
}
else {
if($_data !== null) log::add('netatmo','debug','[netatmo cloud] request : '.$_path.'?options='.json_encode($_data));
else log::add('netatmo','debug','[netatmo cloud] request : '.$_path);
}
$return = json_decode($request_http->exec(30,1),true);
log::add('netatmo','debug','[netatmo cloud] response : '.json_encode($return));
$return = is_json($return,$return);
if(isset($return['state']) && $return['state'] != 'ok'){
throw new \Exception(__('Erreur lors de la requete à Netatmo : ',__FILE__).json_encode($return));
Expand All @@ -120,6 +248,22 @@ public static function request($_path,$_data = null,$_type='GET'){
throw new \Exception(__('Erreur lors de la requete à Netatmo : ',__FILE__).json_encode($return));
}
if(isset($return['body'])){
$return_temp = $return['body'];
if(isset($return_temp['errors'])){
foreach ($return_temp['errors'] as $error) {
$eqLogicError = eqLogic::byLogicalId($error[id], 'netatmo');
if(!is_object($eqLogicError)){
continue;
}
$error_desc[1] = "Unknown error";
$error_desc[2] = "Internal error";
$error_desc[3] = "Parser error";
$error_desc[4] = "Command unknown node module error";
$error_desc[5] = "Command invalid params";
$error_desc[6] = "Unreachable";
message::add('netatmo','L\'équipement '.$eqLogicError->getName().' est en erreur : '.$error_desc[$error[code]].'('.$error[code].')');
}
}
return $return['body'];
}
return $return;
Expand Down Expand Up @@ -228,7 +372,7 @@ public function execute($_options = array()) {
netatmo_security::refresh();
}
if($eqLogic->getConfiguration('type') == 'energy'){
netatmo_energy::refresh();
netatmo::refreshClassNetatmo();
}
return;
}
Expand Down
154 changes: 67 additions & 87 deletions core/class/netatmo_energy.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public static function getRoomEnergyDevices($_modules,$_module_ids){

public static function sync(){
$homesdata = netatmo::request('/homesdata');
log::add('netatmo','debug','[netatmo energy] homesdata : '.json_encode($homesdata));
if(isset($homesdata['homes']) && count($homesdata['homes']) > 0){
foreach ($homesdata['homes'] as $home) {
if(!isset($home['rooms']) || count($home['rooms']) == 0 || !isset($home['modules']) || count($home['modules']) == 0 || !isset($home['schedules'])){
Expand Down Expand Up @@ -119,86 +118,7 @@ public static function sync(){
}

public static function refresh($homesdata = null){
if($homesdata == null) {
$homesdata = netatmo::request('/homesdata');
log::add('netatmo','debug','[netatmo energy] homesdata : '.json_encode($homestatus));
}
$home_ids = array();
if(isset($homesdata['homes']) && count($homesdata['homes']) > 0){
foreach ($homesdata['homes'] as $home) {
if(!isset($home['rooms'])){
continue;
}
$home_ids[] = $home['id'];
if(!isset($home['therm_mode'])){
continue;
}
$eqLogic = eqLogic::byLogicalId($home['id'], 'netatmo');
if(!is_object($eqLogic)){
continue;
}
if($home['therm_mode'] != 'schedule'){
$eqLogic->checkAndUpdateCmd('mode',$home['therm_mode']);
continue;
}
if(isset($home['schedules']) && count($home['schedules']) > 0){
$mode = '';
foreach ($home['schedules'] as $schedule) {
if(!$schedule['selected']){
continue;
}
$mode .= $schedule['name'].',';
}
$eqLogic->checkAndUpdateCmd('mode',trim($mode,','));
}
}
}
if(count($home_ids) == 0){
return;
}
foreach ($home_ids as $home_id) {
$homestatus = netatmo::request('/homestatus',array('home_id' => $home_id));
log::add('netatmo','debug','[netatmo energy] homestatus : '.json_encode($homestatus));
if(isset($homestatus['home']) && isset($homestatus['home']['modules']) && count($homestatus['home']['modules']) > 0){
foreach ($homestatus['home']['modules'] as $module) {
$eqLogic = eqLogic::byLogicalId($module['id'], 'netatmo');
if(!is_object($eqLogic)){
continue;
}
foreach ($eqLogic->getCmd('info') as $cmd) {
$logicalId = $cmd->getLogicalId();
if($logicalId == 'state'){
$logicalId = 'status';
}
if(!isset($module[$logicalId])){
continue;
}
$eqLogic->checkAndUpdateCmd($cmd,$module[$logicalId]);
}
}
}
if(isset($homestatus['home']) && isset($homestatus['home']['rooms']) && count($homestatus['home']['rooms']) > 0){
foreach ($homestatus['home']['rooms'] as $room) {
$eqLogic = eqLogic::byLogicalId($room['id'], 'netatmo');
if(!is_object($eqLogic)){
continue;
}
foreach ($eqLogic->getCmd('info') as $cmd) {
if(!isset($room[$cmd->getLogicalId()])){
continue;
}
if($cmd->getLogicalId() == 'therm_setpoint_mode' && $room[$cmd->getLogicalId()] != 'schedule' && isset($room['therm_setpoint_end_time'])){
$eqLogic->checkAndUpdateCmd($cmd,$room[$cmd->getLogicalId()].' ('.__('fini à',__FILE__).' '.date('H:i',$room['therm_setpoint_end_time']).')');
continue;
}
$eqLogic->checkAndUpdateCmd($cmd,$room[$cmd->getLogicalId()]);
}
}
}



}
netatmo::refreshClassNetatmo();
}

public static function execCmd($_cmd,$_options = array()){
Expand All @@ -212,7 +132,7 @@ public static function execCmd($_cmd,$_options = array()){
array(
'id' => $eqLogic->getLogicalId(),
'therm_setpoint_mode' => 'manual',
'therm_setpoint_temperature' => intval($_options['slider']),
'therm_setpoint_temperature' => floatval($_options['slider']),
)
)
)
Expand Down Expand Up @@ -245,17 +165,74 @@ public static function execCmd($_cmd,$_options = array()){
'mode' => 'home',
),'POST');
}
}else if($_cmd->getLogicalId() == 'home_mode_away'){
}else if($_cmd->getLogicalId() == 'mode_hg'){
netatmo::request('/setstate',array(
'home' => array(
'id' => $eqLogic->getConfiguration('home_id'),
'rooms' => array(
array(
'id' => $eqLogic->getLogicalId(),
'therm_setpoint_mode' => 'hg'
)
)
)
),'POST');
}else if($_cmd->getLogicalId() == 'mode_hg_endtime'){
log::add('netatmo','debug','[netatmo energy] Mode HG');
netatmo::request('/setstate',array(
'home' => array(
'id' => $eqLogic->getConfiguration('home_id'),
'rooms' => array(
array(
'id' => $eqLogic->getLogicalId(),
'therm_setpoint_mode' => 'hg',
'therm_setpoint_end_time' => strtotime('now +'.$_options['slider'].' hours')
)
)
)
),'POST');
}else if($_cmd->getLogicalId() == 'mode_off'){
log::add('netatmo','debug','[netatmo energy] Mode OFF');
netatmo::request('/setroomthermpoint',array(
'home_id' => $eqLogic->getConfiguration('home_id'),
'room_id' => $eqLogic->getLogicalId(),
'mode' => 'off',
),'POST');
}else if($_cmd->getLogicalId() == 'mode_off_endtime'){
log::add('netatmo','debug','[netatmo energy] Mode OFF (heures)');
netatmo::request('/setroomthermpoint',array(
'home_id' => $eqLogic->getConfiguration('home_id'),
'room_id' => $eqLogic->getLogicalId(),
'mode' => 'off',
'endtime' => strtotime('now +'.$_options['slider'].' hours')
),'POST');

}else if($_cmd->getLogicalId() == 'home_mode_away_endtime'){
netatmo::request('/setthermmode',array(
'home_id' => $eqLogic->getConfiguration('home_id'),
'mode' => 'away',
'endtime' => strotime('now +'.$_options['slider'].' hours')
'endtime' => strtotime('now +'.$_options['slider'].' hours')
),'POST');
}else if($_cmd->getLogicalId() == 'home_mode_hg'){
}else if($_cmd->getLogicalId() == 'home_mode_hg_endtime'){
netatmo::request('/setthermmode',array(
'home_id' => $eqLogic->getConfiguration('home_id'),
'mode' => 'hg',
'endtime' => strotime('now +'.$_options['slider'].' hours')
'endtime' => strtotime('now +'.$_options['slider'].' hours')
),'POST');
}else if($_cmd->getLogicalId() == 'home_mode_schedule'){
netatmo::request('/setthermmode',array(
'home_id' => $eqLogic->getConfiguration('home_id'),
'mode' => 'schedule'
),'POST');
}else if($_cmd->getLogicalId() == 'home_mode_away'){
netatmo::request('/setthermmode',array(
'home_id' => $eqLogic->getConfiguration('home_id'),
'mode' => 'away'
),'POST');
}else if($_cmd->getLogicalId() == 'home_mode_hg'){
netatmo::request('/setthermmode',array(
'home_id' => $eqLogic->getConfiguration('home_id'),
'mode' => 'hg'
),'POST');
}else if(strpos($_cmd->getLogicalId(),'schedule') !== false){
netatmo::request('/setthermmode',array(
Expand All @@ -264,7 +241,10 @@ public static function execCmd($_cmd,$_options = array()){
'schedule_id' => str_replace('schedule','',$_cmd->getLogicalId())
),'POST');
}
else {
throw new \Exception('Erreur lors de l éxécution de la commande (commande '.$_cmd->getLogicalId().' inconnue)');
}
sleep(10);
self::refresh();
netatmo::refreshClassNetatmo();
}
}
Loading

0 comments on commit 129b68a

Please sign in to comment.