Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if (unit != '°C' && unit != 'C') {
if (unit === '°F' || unit === 'F')
value = Units.transformToBaseUnit(value, 'F');
else if (unit === 'K')
value = Units.transformToBaseUnit(value, 'K');
else
throw new Error(`Unrecognized unit ${unit}`)
}
} else if (this.device_class === 'pressure') {
if (unit != 'Pa') {
if (unit === 'hPa' || unit === 'hpa')
value *= 100;
else if (unit === 'mbar')
value = Units.transformToBaseUnit(value * 0.001, 'bar');
else if (['bar', 'psi', 'mmHg', 'inHg', 'atm'].includes(unit))
value = Units.transformToBaseUnit(value * 0.001, unit);
else
throw new Error(`Unrecognized unit ${unit}`)
}
}
return [{state: undefined, value: value}];
} else if (this.domain === 'binary_sensor') {
let state = this.deviceStateMapping[this.state.state];
if (['gas', 'smoke'].includes(this.device_class)) {
state = state === 'detecting' ? this.device_class : 'nothing';
}
return [{state: state, value: undefined}];
}
}
// note: subscribe_ must NOT be async, or an ImplementationError will occur at runtime
let value = parseFloat(this.state.state);
if (this.device_class === 'temperature') {
if (unit != '°C' && unit != 'C') {
if (unit === '°F' || unit === 'F')
value = Units.transformToBaseUnit(value, 'F');
else if (unit === 'K')
value = Units.transformToBaseUnit(value, 'K');
else
throw new Error(`Unrecognized unit ${unit}`)
}
} else if (this.device_class === 'pressure') {
if (unit != 'Pa') {
if (unit === 'hPa' || unit === 'hpa')
value *= 100;
else if (unit === 'mbar')
value = Units.transformToBaseUnit(value * 0.001, 'bar');
else if (['bar', 'psi', 'mmHg', 'inHg', 'atm'].includes(unit))
value = Units.transformToBaseUnit(value * 0.001, unit);
else
throw new Error(`Unrecognized unit ${unit}`)
}
}
return [{state: undefined, value: value}];
} else if (this.domain === 'binary_sensor') {
let state = this.deviceStateMapping[this.state.state];
if (['gas', 'smoke'].includes(this.device_class)) {
state = state === 'detecting' ? this.device_class : 'nothing';
}
return [{state: state, value: undefined}];
}
}
// note: subscribe_ must NOT be async, or an ImplementationError will occur at runtime
subscribe_state() {
if (this.domain === 'sensor') {
let unit = this.state.attributes.unit_of_measurement;
let value = parseFloat(this.state.state);
if (this.device_class === 'temperature') {
if (unit != '°C' && unit != 'C') {
if (unit === '°F' || unit === 'F')
value = Units.transformToBaseUnit(value, 'F');
else if (unit === 'K')
value = Units.transformToBaseUnit(value, 'K');
else
throw new Error(`Unrecognized unit ${unit}`)
}
} else if (this.device_class === 'pressure') {
if (unit != 'Pa') {
if (unit === 'hPa' || unit === 'hpa')
value *= 100;
else if (unit === 'mbar')
value = Units.transformToBaseUnit(value * 0.001, 'bar');
else if (['bar', 'psi', 'mmHg', 'inHg', 'atm'].includes(unit))
value = Units.transformToBaseUnit(value * 0.001, unit);
else
throw new Error(`Unrecognized unit ${unit}`)
}
if (unit != '°C' && unit != 'C') {
if (unit === '°F' || unit === 'F')
value = Units.transformToBaseUnit(value, 'F');
else if (unit === 'K')
value = Units.transformToBaseUnit(value, 'K');
else
throw new Error(`Unrecognized unit ${unit}`)
}
} else if (this.device_class === 'pressure') {
if (unit != 'Pa') {
if (unit === 'hPa' || unit === 'hpa')
value *= 100;
else if (unit === 'mbar')
value = Units.transformToBaseUnit(value * 0.001, 'bar');
else if (['bar', 'psi', 'mmHg', 'inHg', 'atm'].includes(unit))
value = Units.transformToBaseUnit(value * 0.001, unit);
else
throw new Error(`Unrecognized unit ${unit}`)
}
}
return this._subscribeState(() => {
return {state: undefined, value: value};
});
} else if (this.domain === 'binary_sensor') {
let state = this.deviceStateMapping[this.state.state];
if (['gas', 'smoke'].includes(this.device_class)) {
state = state === 'detecting' ? this.device_class : 'nothing';
}
return this._subscribeState(() => {
return {state: state, value: undefined};
});
}
async get_state() {
if (this.domain === 'sensor') {
let value = parseFloat(this.state.state);
if (this.device_class === 'temperature') {
if (unit != '°C' && unit != 'C') {
if (unit === '°F' || unit === 'F')
value = Units.transformToBaseUnit(value, 'F');
else if (unit === 'K')
value = Units.transformToBaseUnit(value, 'K');
else
throw new Error(`Unrecognized unit ${unit}`)
}
} else if (this.device_class === 'pressure') {
if (unit != 'Pa') {
if (unit === 'hPa' || unit === 'hpa')
value *= 100;
else if (unit === 'mbar')
value = Units.transformToBaseUnit(value * 0.001, 'bar');
else if (['bar', 'psi', 'mmHg', 'inHg', 'atm'].includes(unit))
value = Units.transformToBaseUnit(value * 0.001, unit);
else
throw new Error(`Unrecognized unit ${unit}`)
}
subscribe_state() {
if (this.domain === 'sensor') {
let unit = this.state.attributes.unit_of_measurement;
let value = parseFloat(this.state.state);
if (this.device_class === 'temperature') {
if (unit != '°C' && unit != 'C') {
if (unit === '°F' || unit === 'F')
value = Units.transformToBaseUnit(value, 'F');
else if (unit === 'K')
value = Units.transformToBaseUnit(value, 'K');
else
throw new Error(`Unrecognized unit ${unit}`)
}
} else if (this.device_class === 'pressure') {
if (unit != 'Pa') {
if (unit === 'hPa' || unit === 'hpa')
value *= 100;
else if (unit === 'mbar')
value = Units.transformToBaseUnit(value * 0.001, 'bar');
else if (['bar', 'psi', 'mmHg', 'inHg', 'atm'].includes(unit))
value = Units.transformToBaseUnit(value * 0.001, unit);
else
throw new Error(`Unrecognized unit ${unit}`)
}
}
return this._subscribeState(() => {
let value = parseFloat(this.state.state);
if (this.device_class === 'temperature') {
if (unit != '°C' && unit != 'C') {
if (unit === '°F' || unit === 'F')
value = Units.transformToBaseUnit(value, 'F');
else if (unit === 'K')
value = Units.transformToBaseUnit(value, 'K');
else
throw new Error(`Unrecognized unit ${unit}`)
}
} else if (this.device_class === 'pressure') {
if (unit != 'Pa') {
if (unit === 'hPa' || unit === 'hpa')
value *= 100;
else if (unit === 'mbar')
value = Units.transformToBaseUnit(value * 0.001, 'bar');
else if (['bar', 'psi', 'mmHg', 'inHg', 'atm'].includes(unit))
value = Units.transformToBaseUnit(value * 0.001, unit);
else
throw new Error(`Unrecognized unit ${unit}`)
}
}
return this._subscribeState(() => {
return {state: undefined, value: value};
});
} else if (this.domain === 'binary_sensor') {
let state = this.deviceStateMapping[this.state.state];
if (['gas', 'smoke'].includes(this.device_class)) {
state = state === 'detecting' ? this.device_class : 'nothing';
}
return this._subscribeState(() => {
return {state: state, value: undefined};
async get_state() {
if (this.domain === 'sensor') {
let value = parseFloat(this.state.state);
if (this.device_class === 'temperature') {
if (unit != '°C' && unit != 'C') {
if (unit === '°F' || unit === 'F')
value = Units.transformToBaseUnit(value, 'F');
else if (unit === 'K')
value = Units.transformToBaseUnit(value, 'K');
else
throw new Error(`Unrecognized unit ${unit}`)
}
} else if (this.device_class === 'pressure') {
if (unit != 'Pa') {
if (unit === 'hPa' || unit === 'hpa')
value *= 100;
else if (unit === 'mbar')
value = Units.transformToBaseUnit(value * 0.001, 'bar');
else if (['bar', 'psi', 'mmHg', 'inHg', 'atm'].includes(unit))
value = Units.transformToBaseUnit(value * 0.001, unit);
else
throw new Error(`Unrecognized unit ${unit}`)
}
}
return [{state: undefined, value: value}];