mysa2mqtt 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/dist/main.js +2 -2
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# mysa2mqtt
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com/package/mysa2mqtt)
|
|
4
|
+
[](https://hub.docker.com/r/bourquep/mysa2mqtt)
|
|
4
5
|
[](https://github.com/bourquep/mysa2mqtt/actions/workflows/github-code-scanning/codeql)
|
|
5
6
|
[](https://github.com/bourquep/mysa2mqtt/actions/workflows/ci.yml)
|
|
6
7
|
|
package/dist/main.js
CHANGED
|
@@ -22,9 +22,9 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
22
22
|
SOFTWARE.
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
|
-
import{MysaApiClient as
|
|
25
|
+
import{MysaApiClient as x}from"mysa-js-sdk";import{pino as k}from"pino";var p=class{constructor(t){this.logger=t}debug(t,...e){let i=e.at(0);i?this.logger.debug(i,t,...e):this.logger.debug(t,...e)}info(t,...e){let i=e.at(0);i?this.logger.info(i,t,...e):this.logger.info(t,...e)}warn(t,...e){let i=e.at(0);i?this.logger.warn(i,t,...e):this.logger.warn(t,...e)}error(t,...e){let i=e.at(0);i?this.logger.error(i,t,...e):this.logger.error(t,...e)}};import{Command as _,InvalidArgumentError as q,Option as s}from"commander";import{configDotenv as v}from"dotenv";import{readFileSync as M}from"fs";import{dirname as w,join as S}from"path";import{fileURLToPath as C}from"url";v({path:[".env",".env.local"],override:!0});function T(){try{let r=C(import.meta.url),t=w(r),e=S(t,"..","package.json");return JSON.parse(M(e,"utf-8")).version||"unknown"}catch{return"unknown"}}function b(r){let t=parseInt(r,10);if(isNaN(t))throw new q("Must be a number.");return t}var h=T(),O=`
|
|
26
26
|
Copyright (c) 2025 Pascal Bourque
|
|
27
27
|
Licensed under the MIT License
|
|
28
28
|
|
|
29
29
|
Source code and documentation available at: https://github.com/bourquep/mysa2mqtt
|
|
30
|
-
`,n=new
|
|
30
|
+
`,n=new _("mysa2mqtt").version(h).description("Expose Mysa smart thermostats to home automation platforms via MQTT.").addHelpText("afterAll",O).addOption(new s("-l, --log-level <logLevel>","log level").choices(["silent","fatal","error","warn","info","debug","trace"]).env("M2M_LOG_LEVEL").default("info").helpGroup("Configuration")).addOption(new s("-f, --log-format <logFormat>","log format").choices(["pretty","json"]).env("M2M_LOG_FORMAT").default("pretty").helpGroup("Configuration")).addOption(new s("-H, --mqtt-host <mqttHost>","hostname of the MQTT broker").env("M2M_MQTT_HOST").makeOptionMandatory().helpGroup("MQTT")).addOption(new s("-P, --mqtt-port <mqttPort>","port of the MQTT broker").env("M2M_MQTT_PORT").argParser(b).default(1883).helpGroup("MQTT")).addOption(new s("-U, --mqtt-username <mqttUsername>","username of the MQTT broker").env("M2M_MQTT_USERNAME").helpGroup("MQTT")).addOption(new s("-B, --mqtt-password <mqttPassword>","password of the MQTT broker").env("M2M_MQTT_PASSWORD").helpGroup("MQTT")).addOption(new s("-u, --mysa-username <mysaUsername>","Mysa account username").env("M2M_MYSA_USERNAME").makeOptionMandatory().helpGroup("Mysa")).addOption(new s("-p, --mysa-password <mysaPassword>","Mysa account password").env("M2M_MYSA_PASSWORD").makeOptionMandatory().helpGroup("Mysa")).addOption(new s("-s, --mysa-session-file <mysaSessionFile>","Mysa session file").env("M2M_MYSA_SESSION_FILE").default("session.json").helpGroup("Configuration")).addOption(new s("-N, --mqtt-client-name <mqttClientName>","name of the MQTT client").env("M2M_MQTT_CLIENT_NAME").default("mysa2mqtt").helpGroup("MQTT")).addOption(new s("-T, --mqtt-topic-prefix <mqttTopicPrefix>","prefix of the MQTT topic").env("M2M_MQTT_TOPIC_PREFIX").default("mysa2mqtt").helpGroup("MQTT")).parse().opts();import{readFile as A,rm as I,writeFile as P}from"fs/promises";async function g(r,t){try{t.info("Loading Mysa session...");let e=await A(r,"utf8");return JSON.parse(e)}catch{t.info("No valid Mysa session file found.")}}async function y(r,t,e){if(r)e.info("Saving Mysa session..."),await P(t,JSON.stringify(r));else try{e.debug("Removing Mysa session file..."),await I(t)}catch{}}import{Climate as D,Sensor as f}from"mqtt2ha";var c=class{constructor(t,e,i,u,d,l){this.mysaApiClient=t;this.mysaDevice=e;this.mqttSettings=i;this.logger=u;this.mysaDeviceFirmware=d;this.mysaDeviceSerialNumber=l;this.mqttDevice={identifiers:e.Id,name:e.Name,manufacturer:"Mysa",model:e.Model,sw_version:d==null?void 0:d.InstalledVersion,serial_number:l},this.mqttOrigin={name:"mysa2mqtt",sw_version:h,support_url:"https://github.com/bourquep/mysa2mqtt"},this.mqttClimate=new D({mqtt:this.mqttSettings,logger:this.logger,component:{component:"climate",device:this.mqttDevice,origin:this.mqttOrigin,unique_id:`mysa_${e.Id}_climate`,name:"Thermostat",min_temp:e.MinSetpoint,max_temp:e.MaxSetpoint,modes:["off","heat"],precision:.1,temp_step:.5,temperature_unit:"C",optimistic:!0}},["action_topic","current_humidity_topic","current_temperature_topic","mode_state_topic","temperature_state_topic"],async()=>{},["mode_command_topic","power_command_topic","temperature_command_topic"],async(a,o)=>{switch(a){case"mode_command_topic":this.mysaApiClient.setDeviceState(this.mysaDevice.Id,void 0,o==="off"?"off":o==="heat"?"heat":void 0);break;case"power_command_topic":this.mysaApiClient.setDeviceState(this.mysaDevice.Id,void 0,o==="OFF"?"off":o==="ON"?"heat":void 0);break;case"temperature_command_topic":o===""?this.mysaApiClient.setDeviceState(this.mysaDevice.Id,void 0,void 0):this.mysaApiClient.setDeviceState(this.mysaDevice.Id,parseFloat(o),void 0);break}}),this.mqttTemperature=new f({mqtt:this.mqttSettings,logger:this.logger,component:{component:"sensor",device:this.mqttDevice,origin:this.mqttOrigin,unique_id:`mysa_${e.Id}_temperature`,name:"Current temperature",device_class:"temperature",state_class:"measurement",unit_of_measurement:"\xB0C",suggested_display_precision:1,force_update:!0}}),this.mqttHumidity=new f({mqtt:this.mqttSettings,logger:this.logger,component:{component:"sensor",device:this.mqttDevice,origin:this.mqttOrigin,unique_id:`mysa_${e.Id}_humidity`,name:"Current humidity",device_class:"humidity",state_class:"measurement",unit_of_measurement:"%",suggested_display_precision:0,force_update:!0}}),this.mqttPower=new f({mqtt:this.mqttSettings,logger:this.logger,component:{component:"sensor",device:this.mqttDevice,origin:this.mqttOrigin,unique_id:`mysa_${e.Id}_power`,name:"Current power",device_class:"power",state_class:"measurement",unit_of_measurement:"W",suggested_display_precision:0,force_update:!0}})}isStarted=!1;mqttDevice;mqttOrigin;mqttClimate;mqttTemperature;mqttHumidity;mqttPower;mysaStatusUpdateHandler=this.handleMysaStatusUpdate.bind(this);mysaStateChangeHandler=this.handleMysaStateChange.bind(this);async start(){if(!this.isStarted){this.isStarted=!0;try{let e=(await this.mysaApiClient.getDeviceStates()).DeviceStatesObj[this.mysaDevice.Id];this.mqttClimate.currentTemperature=e.CorrectedTemp.v,this.mqttClimate.currentHumidity=e.Humidity.v,this.mqttClimate.currentMode=e.TstatMode.v===1?"off":e.TstatMode.v===3?"heat":void 0,this.mqttClimate.currentAction=this.computeCurrentAction(void 0,e.Duty.v),this.mqttClimate.targetTemperature=this.mqttClimate.currentMode!=="off"?e.SetPoint.v:void 0,await this.mqttClimate.writeConfig(),await this.mqttTemperature.setState("state_topic",e.CorrectedTemp.v.toFixed(2)),await this.mqttTemperature.writeConfig(),await this.mqttHumidity.setState("state_topic",e.Humidity.v.toFixed(2)),await this.mqttHumidity.writeConfig(),await this.mqttPower.setState("state_topic","None"),await this.mqttPower.writeConfig(),this.mysaApiClient.emitter.on("statusChanged",this.mysaStatusUpdateHandler),this.mysaApiClient.emitter.on("stateChanged",this.mysaStateChangeHandler),await this.mysaApiClient.startRealtimeUpdates(this.mysaDevice.Id)}catch(t){throw this.isStarted=!1,t}}}async stop(){this.isStarted&&(this.isStarted=!1,await this.mysaApiClient.stopRealtimeUpdates(this.mysaDevice.Id),this.mysaApiClient.emitter.off("statusChanged",this.mysaStatusUpdateHandler),this.mysaApiClient.emitter.off("stateChanged",this.mysaStateChangeHandler),await this.mqttPower.setState("state_topic","None"),await this.mqttTemperature.setState("state_topic","None"),await this.mqttHumidity.setState("state_topic","None"))}async handleMysaStatusUpdate(t){if(!(!this.isStarted||t.deviceId!==this.mysaDevice.Id)){if(this.mqttClimate.currentAction=this.computeCurrentAction(t.current,t.dutyCycle),this.mqttClimate.currentTemperature=t.temperature,this.mqttClimate.currentHumidity=t.humidity,this.mqttClimate.targetTemperature=this.mqttClimate.currentMode!=="off"?t.setPoint:void 0,t.current!=null){let e=this.mysaDevice.Voltage*t.current;await this.mqttPower.setState("state_topic",e.toFixed(2))}else await this.mqttPower.setState("state_topic","None");await this.mqttTemperature.setState("state_topic",t.temperature.toFixed(2)),await this.mqttHumidity.setState("state_topic",t.humidity.toFixed(2))}}async handleMysaStateChange(t){if(!(!this.isStarted||t.deviceId!==this.mysaDevice.Id))switch(t.mode){case"off":this.mqttClimate.currentMode="off",this.mqttClimate.currentAction="off",this.mqttClimate.targetTemperature=void 0;break;case"heat":this.mqttClimate.currentMode="heat",this.mqttClimate.targetTemperature=t.setPoint;break}}computeCurrentAction(t,e){switch(this.mqttClimate.currentMode==="heat"?"heat":this.mqttClimate.currentMode==="off"?"off":void 0){case"off":return"off";case"heat":return t!=null?t>0?"heating":"idle":(e??0)>0?"heating":"idle";default:return"idle"}}};var m=k({name:"mysa2mqtt",level:n.logLevel,transport:n.logFormat==="pretty"?{target:"pino-pretty",options:{colorize:!0,singleLine:!0,ignore:"hostname,module",messageFormat:"\x1B[33m[{module}]\x1B[39m {msg}"}}:void 0});async function F(){m.info("Starting mysa2mqtt...");let r=await g(n.mysaSessionFile,m),t=new x(r,{logger:new p(m.child({module:"mysa-js-sdk"}))});t.emitter.on("sessionChanged",async a=>{await y(a,n.mysaSessionFile,m)}),t.isAuthenticated||(m.info("Logging in..."),await t.login(n.mysaUsername,n.mysaPassword)),m.debug("Fetching devices and firmwares...");let[e,i]=await Promise.all([t.getDevices(),t.getDeviceFirmwares()]);m.debug("Fetching serial numbers...");let u=new Map;for(let[a]of Object.entries(e.DevicesObj))try{let o=await t.getDeviceSerialNumber(a);o&&u.set(a,o)}catch(o){m.error(`Failed to retrieve serial number for device ${a}`,o)}m.debug("Initializing MQTT entities...");let d={host:n.mqttHost,port:n.mqttPort,username:n.mqttUsername,password:n.mqttPassword,client_name:n.mqttClientName,state_prefix:n.mqttTopicPrefix},l=Object.entries(e.DevicesObj).map(([,a])=>new c(t,a,d,new p(m.child({module:"thermostat",deviceId:a.Id})),i.Firmware[a.Id],u.get(a.Id)));for(let a of l)await a.start()}F().catch(r=>{m.fatal(r,"Unexpected error"),process.exit(1)});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mysa2mqtt",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"private": false,
|
|
6
6
|
"description": "Expose Mysa smart thermostats to home automation platforms via MQTT.",
|
|
@@ -48,8 +48,8 @@
|
|
|
48
48
|
"dependencies": {
|
|
49
49
|
"commander": "14.0.0",
|
|
50
50
|
"dotenv": "16.5.0",
|
|
51
|
-
"mqtt2ha": "4.
|
|
52
|
-
"mysa-js-sdk": "1.
|
|
51
|
+
"mqtt2ha": "4.1.0",
|
|
52
|
+
"mysa-js-sdk": "1.2.0",
|
|
53
53
|
"pino": "9.7.0",
|
|
54
54
|
"pino-pretty": "13.0.0"
|
|
55
55
|
},
|