diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..b43bf86 --- /dev/null +++ b/.prettierignore @@ -0,0 +1 @@ +README.md diff --git a/README.md b/README.md index a571d19..59e2074 100644 --- a/README.md +++ b/README.md @@ -124,6 +124,11 @@ For development or custom modifications: The application can be configured using either command-line arguments or environment variables. Environment variables take precedence over command-line defaults. +> [!IMPORTANT] +> The `M2M_TEMPERATURE_UNIT` option must match Home Assistant's unit system (Settings → General → Unit System) +> so setpoints and readings are interpreted correctly. If mismatched, climate entities will show incorrect values (e.g. +> 21°C treated as 21°F) and commands may result in unexpected temperatures. + ### Required Configuration | CLI Option | Environment Variable | Description | @@ -151,6 +156,7 @@ take precedence over command-line defaults. | `-l, --log-level` | `M2M_LOG_LEVEL` | `info` | Log level: `silent`, `fatal`, `error`, `warn`, `info`, `debug`, `trace` | | `-f, --log-format` | `M2M_LOG_FORMAT` | `pretty` | Log format: `pretty`, `json` | | `-s, --mysa-session-file` | `M2M_MYSA_SESSION_FILE` | `session.json` | Path to Mysa session file | +| `-t, --temperature-unit` | `M2M_TEMPERATURE_UNIT` | `C` | Temperature unit (`C` = Celsius, `F` = Fahrenheit) | ## Usage Examples diff --git a/src/main.ts b/src/main.ts index 6e48a23..7ce5d35 100644 --- a/src/main.ts +++ b/src/main.ts @@ -99,7 +99,8 @@ async function main() { mqttSettings, new PinoLogger(rootLogger.child({ module: 'thermostat', deviceId: device.Id })), firmwares.Firmware[device.Id], - serialNumbers.get(device.Id) + serialNumbers.get(device.Id), + options.temperatureUnit ) ); diff --git a/src/options.ts b/src/options.ts index af99ec0..4d2439f 100644 --- a/src/options.ts +++ b/src/options.ts @@ -143,5 +143,12 @@ export const options = new Command('mysa2mqtt') .default('mysa2mqtt') .helpGroup('MQTT') ) + .addOption( + new Option('--temperature-unit ', 'temperature unit (C or F)') + .env('M2M_TEMPERATURE_UNIT') + .choices(['C', 'F']) + .default('C') + .helpGroup('Configuration') + ) .parse() .opts(); diff --git a/src/thermostat.ts b/src/thermostat.ts index 496df2d..229ebe2 100644 --- a/src/thermostat.ts +++ b/src/thermostat.ts @@ -51,8 +51,11 @@ export class Thermostat { private readonly mqttSettings: MqttSettings, private readonly logger: Logger, public readonly mysaDeviceFirmware?: FirmwareDevice, - public readonly mysaDeviceSerialNumber?: string + public readonly mysaDeviceSerialNumber?: string, + public readonly temperatureUnit?: 'C' | 'F' ) { + const is_celsius = (temperatureUnit ?? 'C') === 'C'; + this.mqttDevice = { identifiers: mysaDevice.Id, name: mysaDevice.Name, @@ -81,9 +84,9 @@ export class Thermostat { min_temp: mysaDevice.MinSetpoint, max_temp: mysaDevice.MaxSetpoint, modes: ['off', 'heat'], // TODO: AC - precision: 0.1, - temp_step: 0.5, - temperature_unit: 'C', // TODO: Confirm that Mysa always works in C + precision: is_celsius ? 0.1 : 1.0, + temp_step: is_celsius ? 0.5 : 1.0, + temperature_unit: 'C', optimistic: true } }, @@ -118,7 +121,17 @@ export class Thermostat { if (message === '') { this.mysaApiClient.setDeviceState(this.mysaDevice.Id, undefined, undefined); } else { - this.mysaApiClient.setDeviceState(this.mysaDevice.Id, parseFloat(message), undefined); + let temperature = parseFloat(message); + + if (!is_celsius) { + const snapHalfC = (c: number) => Math.round(c * 2) / 2; + const clamp = (v: number, min: number, max: number) => Math.min(max, Math.max(min, v)); + // Snap to 0.5 °C and clamp to device limits + const setC = snapHalfC(temperature); + temperature = clamp(setC, this.mysaDevice.MinSetpoint ?? 0, this.mysaDevice.MaxSetpoint ?? 100); + } + + this.mysaApiClient.setDeviceState(this.mysaDevice.Id, temperature, undefined); } break; } @@ -137,7 +150,7 @@ export class Thermostat { device_class: 'temperature', state_class: 'measurement', unit_of_measurement: '°C', - suggested_display_precision: 1, + suggested_display_precision: is_celsius ? 0.1 : 0.0, force_update: true } });