Skip to content

Commit

Permalink
Fix the way units are handled #886 (#1028)
Browse files Browse the repository at this point in the history
  • Loading branch information
atrovato authored Dec 22, 2020
1 parent da1d864 commit 48d25c6
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import relativeTime from 'dayjs/plugin/relativeTime';

dayjs.extend(relativeTime);

import { DEVICE_FEATURE_UNITS, DEVICE_FEATURE_CATEGORIES } from '../../../../../../server/utils/constants';
import { DEVICE_FEATURE_CATEGORIES } from '../../../../../../server/utils/constants';

const SPECIAL_SENSORS = [
DEVICE_FEATURE_CATEGORIES.OPENING_SENSOR,
Expand Down Expand Up @@ -35,17 +35,7 @@ const SensorDeviceType = ({ children, ...props }) => (
{props.deviceFeature.last_value !== null && (
<span>
{' '}
{props.deviceFeature.unit === DEVICE_FEATURE_UNITS.PERCENT && '%'}
{props.deviceFeature.unit === DEVICE_FEATURE_UNITS.CELSIUS && '°C'}
{props.deviceFeature.unit === DEVICE_FEATURE_UNITS.FAHRENHEIT && '°F'}
{props.deviceFeature.unit === DEVICE_FEATURE_UNITS.WATT && 'W'}
{props.deviceFeature.unit === DEVICE_FEATURE_UNITS.KILOWATT && 'kW'}
{props.deviceFeature.unit === DEVICE_FEATURE_UNITS.KILOWATT_HOUR && 'kW/h'}
{props.deviceFeature.unit === DEVICE_FEATURE_UNITS.LUX && 'Lx'}
{props.deviceFeature.unit === DEVICE_FEATURE_UNITS.PASCAL && 'Pa'}
{props.deviceFeature.unit === DEVICE_FEATURE_UNITS.AMPERE && 'A'}
{props.deviceFeature.unit === DEVICE_FEATURE_UNITS.VOLT && 'V'}
{props.deviceFeature.unit === DEVICE_FEATURE_UNITS.PPM && 'ppm'}
<Text id={`deviceFeatureUnitShort.${props.deviceFeature.unit}`} />
</span>
)}
</td>
Expand Down
56 changes: 25 additions & 31 deletions front/src/components/device/UpdateDeviceFeature.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Component } from 'preact';
import { Text, Localizer } from 'preact-i18n';
import { DEVICE_FEATURE_CATEGORIES, DEVICE_FEATURE_UNITS } from '../../../../server/utils/constants';
import { DEVICE_FEATURE_UNITS_BY_CATEGORY } from '../../../../server/utils/constants';
import { DeviceFeatureCategoriesIcon } from '../../utils/consts';
import get from 'get-value';

Expand All @@ -12,29 +12,24 @@ class UpdateDeviceFeature extends Component {
updateUnit = e => this.props.updateFeatureProperty(this.props.featureIndex, 'unit', e.target.value);
deleteFeature = e => this.props.deleteFeature(this.props.featureIndex);

render(props, {}) {
render({ feature, featureIndex, ...props }) {
return (
<div class="col-md-4">
<div class="card">
<div class="card-header">
<i
class={`mr-2 fe fe-${get(
DeviceFeatureCategoriesIcon,
`${props.feature.category}.${props.feature.type}`
)}`}
/>
<Text id={`deviceFeatureCategory.${props.feature.category}.${props.feature.type}`} />
<i class={`mr-2 fe fe-${get(DeviceFeatureCategoriesIcon, `${feature.category}.${feature.type}`)}`} />
<Text id={`deviceFeatureCategory.${feature.category}.${feature.type}`} />
</div>
<div class="card-body">
<div class="form-group form-label" for={`featureName_${props.featureIndex}`}>
<div class="form-group form-label" for={`featureName_${featureIndex}`}>
<label>
<Text id="editDeviceForm.nameLabel" />
</label>
<Localizer>
<input
id={`featureName_${props.featureIndex}`}
id={`featureName_${featureIndex}`}
type="text"
value={props.feature.name}
value={feature.name}
onInput={this.updateName}
class="form-control"
placeholder={<Text id="editDeviceForm.namePlaceholder" />}
Expand All @@ -43,57 +38,56 @@ class UpdateDeviceFeature extends Component {
</div>
{props.allowModifyFeatures && (
<div class="form-group">
<label class="form-label" for={`externalid_${props.featureIndex}`}>
<label class="form-label" for={`externalid_${featureIndex}`}>
<Text id="editDeviceForm.externalIdLabel" />
</label>
<Localizer>
<input
id={`externalid_${props.featureIndex}`}
id={`externalid_${featureIndex}`}
type="text"
value={props.feature.external_id}
value={feature.external_id}
onInput={this.updateExternalId}
class="form-control"
placeholder={<Text id="editDeviceForm.externalIdPlaceholder" />}
/>
</Localizer>
</div>
)}
{props.feature.category === DEVICE_FEATURE_CATEGORIES.TEMPERATURE_SENSOR && (
{DEVICE_FEATURE_UNITS_BY_CATEGORY[feature.category] && (
<div class="form-group">
<label class="form-label" for={`externalid_${props.featureIndex}`}>
<label class="form-label" for={`externalid_${featureIndex}`}>
<Text id="editDeviceForm.unitLabel" />
</label>
<Localizer>
<select
id={`unit_${props.featureIndex}`}
id={`unit_${featureIndex}`}
type="text"
value={props.feature.unit}
value={feature.unit}
onChange={this.updateUnit}
class="form-control"
>
<option value="">
<Text id="global.emptySelectOption" />
</option>
<option value={DEVICE_FEATURE_UNITS.CELSIUS}>
<Text id="deviceFeatureUnit.celsius" />
</option>
<option value={DEVICE_FEATURE_UNITS.FAHRENHEIT}>
<Text id="deviceFeatureUnit.fahrenheit" />
</option>
{DEVICE_FEATURE_UNITS_BY_CATEGORY[feature.category].map(unit => (
<option value={unit}>
<Text id={`deviceFeatureUnit.${unit}`}>{unit}</Text>
</option>
))}
</select>
</Localizer>
</div>
)}
{props.allowModifyFeatures && (
<div class="form-group">
<label class="form-label" for={`min_${props.featureIndex}`}>
<label class="form-label" for={`min_${featureIndex}`}>
<Text id="editDeviceForm.minLabel" />
</label>
<Localizer>
<input
id={`min_${props.featureIndex}`}
id={`min_${featureIndex}`}
type="number"
value={props.feature.min}
value={feature.min}
onInput={this.updateMin}
class="form-control"
placeholder={<Text id="editDeviceForm.minPlaceholder" />}
Expand All @@ -103,14 +97,14 @@ class UpdateDeviceFeature extends Component {
)}
{props.allowModifyFeatures && (
<div class="form-group">
<label class="form-label" for={`max_${props.featureIndex}`}>
<label class="form-label" for={`max_${featureIndex}`}>
<Text id="editDeviceForm.maxLabel" />
</label>
<Localizer>
<input
id={`max_${props.featureIndex}`}
id={`max_${featureIndex}`}
type="number"
value={props.feature.max}
value={feature.max}
onInput={this.updateMax}
class="form-control"
placeholder={<Text id="editDeviceForm.maxPlaceholder" />}
Expand Down
19 changes: 16 additions & 3 deletions front/src/config/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,6 @@
"externalIdLabel": "Feature external ID",
"externalIdMessage": "The external ID is an unique ID which helps you identify this device outside of Gladys when sending a MQTT message. It musts start with \"mqtt:\". Read the MQTT API documentation <a href=\"https://gladysassistant.com/en/docs/api/mqtt-api\">here</a>.",
"externalIdPlaceholder": "Feature MQTT message key",
"unitLabel": "Unit",
"minLabel": "Minimum value",
"minPlaceholder": "Enter feature minimum value",
"maxLabel": "Maximum value",
Expand Down Expand Up @@ -1199,13 +1198,27 @@
"deviceFeatureUnit": {
"celsius": "Celsius (°C)",
"fahrenheit": "Fahrenheit (°F)",
"percent": "%",
"ppm": "ppm"
"percent": "Percent (%)",
"pascal": "Pascal (Pa)",
"lux": "Lux",
"watt": "Watt (W)",
"kilowatt": "Kilowatt (kW)",
"kilowatt-hour": "Kilowatt per hour (kW/h)",
"ampere": "Ampere (A)",
"volt": "Volt (V)",
"ppm": "Parts per million (ppm)"
},
"deviceFeatureUnitShort": {
"celsius": "°C",
"fahrenheit": "°F",
"percent": "%",
"pascal": "Pa",
"lux": "lx",
"watt": "W",
"kilowatt": "KW",
"kilowatt-hour": "kW/h",
"ampere": "A",
"volt": "V",
"ppm": "ppm"
},
"deviceFeatureCategory": {
Expand Down
19 changes: 16 additions & 3 deletions front/src/config/i18n/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,6 @@
"externalIdLabel": "ID externe de la fonctionnalité",
"externalIdMessage": "L'ID externe d'une fonctionnalité est un ID unique qui vous aide à identifier cette fonctionnalité en dehors de Gladys lors de l'envoi d'un message MQTT. Il doit commencer par \"mqtt:\". Pour en savoir plus, rendez-vous sur la documentation de l'API MQTT de Gladys <a href=\"https://gladysassistant.com/fr/docs/api/mqtt-api\"> ici </a>.",
"externalIdPlaceholder": "Message clé de la fonctionnalité MQTT",
"unitLabel": "Unité",
"minLabel": "Valeur minimum",
"minPlaceholder": "Entrez la valeur minimum",
"maxLabel": "Valeur maximum",
Expand Down Expand Up @@ -1199,13 +1198,27 @@
"deviceFeatureUnit": {
"celsius": "Celsius (°C)",
"fahrenheit": "Fahrenheit (°F)",
"percent": "%",
"ppm": "ppm"
"percent": "Pourcent (%)",
"pascal": "Pascal (Pa)",
"lux": "Lux",
"watt": "Watt",
"kilowatt": "Kilowatt",
"kilowatt-hour": "Kilowatt heure (kW/h)",
"ampere": "Ampère (A)",
"volt": "Volt (V)",
"ppm": "Partie par million (ppm)"
},
"deviceFeatureUnitShort": {
"celsius": "°C",
"fahrenheit": "°F",
"percent": "%",
"pascal": "Pa",
"lux": "lx",
"watt": "W",
"kilowatt": "KW",
"kilowatt-hour": "kW/h",
"ampere": "A",
"volt": "V",
"ppm": "ppm"
},
"deviceFeatureCategory": {
Expand Down
Loading

0 comments on commit 48d25c6

Please sign in to comment.