mirror of
https://github.com/bourquep/mysa2mqtt.git
synced 2025-12-17 21:52:38 +00:00
# Fix Temperature Handling in Fahrenheit Mode
## Problem
When operating in **Fahrenheit mode**, Mysa still expects temperature
values to be provided in **Celsius**.
However, Home Assistant sends integer Fahrenheit values (e.g.,
`72.02°F`), which convert to **non-aligned Celsius values** like
`22.22°C`.
Mysa’s API only accepts temperature values that are either **whole
numbers** or **increments of 0.5°C** (for example: `21.0`, `21.5`,
`22.0`).
As a result, values such as `22.22°C` or `21.72°C` are considered
invalid and are **rejected** by Mysa’s API.
---
## Root Cause
- The original code accepted **0.1°C precision** and **0.5°C step
size**.
- When Home Assistant runs in Fahrenheit, the conversion from °F to °C
produces fractional values that are not valid (e.g., 72°F → 22.22°C).
- Because Mysa enforces strict 0.5°C increments, these fractional
setpoints caused failed updates.
---
## Solution
This update ensures valid behavior when using Fahrenheit mode **while
keeping the current behavior for Celsius**:
- Adds a new environment variable:
**`M2M_TEMP_UNIT`** — accepts either:
- `C` *(default)*
- `F` *(for Fahrenheit operation)*
- When running in Fahrenheit mode (`M2M_TEMP_UNIT=F`):
- Celsius values are **rounded and clamped to the nearest 0.5°C**.
- Temperature step size and precision are adjusted:
- Precision → `1°F`
- Step size → `1°F`
- When running in Celsius mode, existing logic remains unchanged (0.1
precision, 0.5 step).
---
## Technical Summary
| Mode | Env Variable | Precision | Step | Conversion Behavior |
|------|---------------|------------|------|----------------------|
| Celsius | `M2M_TEMP_UNIT=C` (default) | 0.1°C | 0.5°C | Direct
pass-through |
| Fahrenheit | `M2M_TEMP_UNIT=F` | 1°F | 1°F | Convert °F → °C, snap to
0.5°C |
The rounding logic ensures that when a Fahrenheit value (e.g., `72°F`)
is converted to Celsius (`21.72°C`), it is adjusted to the nearest valid
half-degree (`21.5°C` or `22.0°C`).
# Demo
https://github.com/user-attachments/assets/bbffe5fe-a3be-43cb-aed0-f63bdfacb1d4
---------
Co-authored-by: Pascal Bourque <pascal@cosmos.moi>
116 lines
3.7 KiB
JavaScript
116 lines
3.7 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
/*
|
|
mysa2mqtt
|
|
Copyright (C) 2025 Pascal Bourque
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
SOFTWARE.
|
|
*/
|
|
|
|
import { MqttSettings } from 'mqtt2ha';
|
|
import { MysaApiClient } from 'mysa-js-sdk';
|
|
import { pino } from 'pino';
|
|
import { PinoLogger } from './logger';
|
|
import { options } from './options';
|
|
import { loadSession, saveSession } from './session';
|
|
import { Thermostat } from './thermostat';
|
|
|
|
const rootLogger = pino({
|
|
name: 'mysa2mqtt',
|
|
level: options.logLevel,
|
|
transport:
|
|
options.logFormat === 'pretty'
|
|
? {
|
|
target: 'pino-pretty',
|
|
options: {
|
|
colorize: true,
|
|
singleLine: true,
|
|
ignore: 'hostname,module',
|
|
messageFormat: '\x1b[33m[{module}]\x1b[39m {msg}'
|
|
}
|
|
}
|
|
: undefined
|
|
});
|
|
|
|
/** Mysa2mqtt entry-point. */
|
|
async function main() {
|
|
rootLogger.info('Starting mysa2mqtt...');
|
|
|
|
const session = await loadSession(options.mysaSessionFile, rootLogger);
|
|
const client = new MysaApiClient(session, { logger: new PinoLogger(rootLogger.child({ module: 'mysa-js-sdk' })) });
|
|
|
|
client.emitter.on('sessionChanged', async (newSession) => {
|
|
await saveSession(newSession, options.mysaSessionFile, rootLogger);
|
|
});
|
|
|
|
if (!client.isAuthenticated) {
|
|
rootLogger.info('Logging in...');
|
|
await client.login(options.mysaUsername, options.mysaPassword);
|
|
}
|
|
|
|
rootLogger.debug('Fetching devices and firmwares...');
|
|
const [devices, firmwares] = await Promise.all([client.getDevices(), client.getDeviceFirmwares()]);
|
|
|
|
rootLogger.debug('Fetching serial numbers...');
|
|
const serialNumbers = new Map<string, string>();
|
|
for (const [deviceId] of Object.entries(devices.DevicesObj)) {
|
|
try {
|
|
const serial = await client.getDeviceSerialNumber(deviceId);
|
|
if (serial) {
|
|
serialNumbers.set(deviceId, serial);
|
|
}
|
|
} catch (error) {
|
|
rootLogger.error(error, `Failed to retrieve serial number for device ${deviceId}`);
|
|
}
|
|
}
|
|
|
|
rootLogger.debug('Initializing MQTT entities...');
|
|
|
|
const mqttSettings: MqttSettings = {
|
|
host: options.mqttHost,
|
|
port: options.mqttPort,
|
|
username: options.mqttUsername,
|
|
password: options.mqttPassword,
|
|
client_name: options.mqttClientName,
|
|
state_prefix: options.mqttTopicPrefix
|
|
};
|
|
|
|
const thermostats = Object.entries(devices.DevicesObj).map(
|
|
([, device]) =>
|
|
new Thermostat(
|
|
client,
|
|
device,
|
|
mqttSettings,
|
|
new PinoLogger(rootLogger.child({ module: 'thermostat', deviceId: device.Id })),
|
|
firmwares.Firmware[device.Id],
|
|
serialNumbers.get(device.Id),
|
|
options.temperatureUnit
|
|
)
|
|
);
|
|
|
|
for (const thermostat of thermostats) {
|
|
await thermostat.start();
|
|
}
|
|
}
|
|
|
|
main().catch((error) => {
|
|
rootLogger.fatal(error, 'Unexpected error');
|
|
process.exit(1);
|
|
});
|