Skip to content

Commit

Permalink
Merge pull request #54 from mhzawadi/0.1.8
Browse files Browse the repository at this point in the history
PHP Error rounding #53
  • Loading branch information
mhzawadi authored Jul 14, 2024
2 parents d4aa6e6 + 8ebe904 commit 63ee41c
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 32 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/image-version.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ name: build our image for a version

on:
workflow_dispatch:
on:
release:
types: [published]
release:
types: [published]

jobs:
build:
Expand Down
1 change: 1 addition & 0 deletions src/classes/logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class logger {
/**
* Log function
* Type: 0 debug, 1 INFO, 2 WARN, 3 ERROR
* Var: Bool
**/
protected function log($text, $type = 1, $var = false){

Expand Down
10 changes: 5 additions & 5 deletions src/model/data.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function process_data($mqtt_topic, $foxess_data, $collected_data, $total_
for( $i = 0 ; $i < $options_count; $i++ ){ //for each value
$option = $collected_data[$device]['result'][0]['datas'][$i]['variable'];
$name = $collected_data[$device]['result'][0]['datas'][$i]['variable'];
$this->log($name,1,2);
$this->log('Value name: '.$name, 1);
if(strstr($option, 'Temperature') !== false || strstr($option, 'SoC') !== false
|| strstr($option, 'Volt') !== false || strstr($option, 'Current') !== false ||
strstr($option, 'Temperation') !== false
Expand All @@ -91,10 +91,6 @@ public function process_data($mqtt_topic, $foxess_data, $collected_data, $total_
$this->log('Post '.$value.' of '.$name.' to MQTT', 1);

}
}elseif(strstr($option, 'currentFault') !== false ||
strstr($option, 'currentFaultCount') !== false){ // only Faults
$this->mqtt->post_mqtt(''.$mqtt_topic.'/'.$deviceSN.'/'.$name, $data['value']);
$this->log('Post '.$data['value'].' of '.$name.' to MQTT', 1);
}elseif(strstr($option, 'runningState') !== false){ // only runningState
$data = $collected_data[$device]['result'][0]['datas'][$i];
switch($data['value']){
Expand Down Expand Up @@ -134,6 +130,10 @@ public function process_data($mqtt_topic, $foxess_data, $collected_data, $total_
}
$this->mqtt->post_mqtt(''.$mqtt_topic.'/'.$deviceSN.'/'.$name, $data['value']);
$this->log('Post '.$data['value'].' of '.$name.' to MQTT', 1);
}elseif(array_key_exists('unit', $collected_data[$device]['result'][0]['datas'][$i]) === false){ // Text values
$data = $collected_data[$device]['result'][0]['datas'][$i];
$this->mqtt->post_mqtt(''.$mqtt_topic.'/'.$deviceSN.'/'.$name, $data['value']);
$this->log('Post '.$data['value'].' of '.$name.' to MQTT', 1);
}else{ // KW/KWh
if($collected_data[$device]['result'] == 'null'){
$value_kw = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/model/device.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public function variable_list(){
}
}
if($this->redis->set('foxess_data', $foxess_data)){
$this->log('all done', 1, 3);
$this->log('all done', 1);
return true;
}else{
$this->log('Redis didnt save', 1);
Expand Down
51 changes: 28 additions & 23 deletions src/model/mqtt.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function __construct($config){
public function setup_mqtt($deviceSN, $deviceType, $option_name, $option_unit) {
$this->log('Start of MQTT setup for HA', 1);
$this->log($option_unit, 1);
$state_cla_name = '"state_class"';
$state_cla = 'measurement';
switch($option_unit){
case '°C':
Expand Down Expand Up @@ -58,7 +59,6 @@ public function setup_mqtt($deviceSN, $deviceType, $option_name, $option_unit) {
$dev_cla = 'reactive_power';
break;
default:
$dev_cla = '';
$option_unit = null;
break;
}
Expand All @@ -68,28 +68,33 @@ public function setup_mqtt($deviceSN, $deviceType, $option_name, $option_unit) {
$dev_cla = 'temperature';
$option_unit = '°C';
}

$data = '{
"name": "'.$option_name.'",
"device": {
"identifiers": "'.$deviceSN.'",
"name": "'.$this->config->mqtt_topic.'-'.$deviceSN.'",
"model": "'.$deviceType.'",
"manufacturer": "FoxEss"
},
"stat_t": "~'.$option_name.'",
"uniq_id": "'.$deviceSN.'-'.$option_name.'",
"~": "'.$this->config->mqtt_topic.'/'.$deviceSN.'/",
"unit_of_measurement": "'.$option_unit.'",
"dev_cla": "'.$dev_cla.'",
"exp_aft": 86400,
"state_class": "'.$state_cla.'"
}';
$this->log('Post to MQTT '.$deviceSN.'-'.$option_name, 1);
try {
$this->post_mqtt('homeassistant/sensor/'.$deviceSN.'-'.$option_name.'/config', $data, true);
} catch (Exception $e) {
$this->log('[WARN] MQTT not yet ready, need to sleep on first run maybe', 1);
if(is_null($option_unit)){
$this->log('[INFO] '.$option_name.' is a text object, cant import into HA', 1);
}else{
$unit_of_measurement = '"unit_of_measurement": "'.$option_unit.'",';
$device_class = '"dev_cla": "'.$dev_cla.'",';
$data = '{
"name": "'.$option_name.'",
"device": {
"identifiers": "'.$deviceSN.'",
"name": "'.$this->config->mqtt_topic.'-'.$deviceSN.'",
"model": "'.$deviceType.'",
"manufacturer": "FoxEss"
},
"stat_t": "~'.$option_name.'",
"uniq_id": "'.$deviceSN.'-'.$option_name.'",
"~": "'.$this->config->mqtt_topic.'/'.$deviceSN.'/",
"exp_aft": 86400,
'.$unit_of_measurement.'
'.$device_class.'
'.$state_cla_name.': "'.$state_cla.'"
}';
$this->log('Post to MQTT '.$deviceSN.'-'.$option_name, 1);
try {
$this->post_mqtt('homeassistant/sensor/'.$deviceSN.'-'.$option_name.'/config', $data, true);
} catch (Exception $e) {
$this->log('[WARN] MQTT not yet ready, need to sleep on first run maybe', 1);
}
}
}

Expand Down

0 comments on commit 63ee41c

Please sign in to comment.