I have a widget that is just a basic toggle that returns a true (2) or false (1) value based on the position of the toggle. I am really not sure why, but the widget never turns off even when I try and toggle it (i.e always stays active).
Checking console log, I can see the value it is trying to send is a 1, but the widget does not seem to update its position or value to the attribute.
This is using javascript and angular.
let settings;
let attributeService;
let utils;
let translate;
let $scope;
let map;
self.onInit = function() {
self.ctx.ngZone.run(function() {
init();
self.ctx.detectChanges(true);
});
};
function init() {
$scope = self.ctx.$scope;
attributeService = $scope.$injector.get(self.ctx.servicesMap.get('attributeService'));
utils = $scope.$injector.get(self.ctx.servicesMap.get('utils'));
translate = $scope.$injector.get(self.ctx.servicesMap.get('translate'));
$scope.toastTargetId = 'input-widget' + utils.guid();
settings = utils.deepClone(self.ctx.settings) || {};
settings.showResultMessage = utils.defaultValue(settings.showResultMessage, true);
$scope.isValidParameter = true;
$scope.dataKeyDetected = false;
$scope.message = translate.instant('widgets.input-widgets.no-entity-selected');
settings.trueValue = utils.defaultValue(utils.customTranslation(settings.trueValue, settings.trueValue), true);
settings.falseValue = utils.defaultValue(utils.customTranslation(settings.falseValue, settings.falseValue), false);
map = {
true: settings.trueValue,
false: settings.falseValue
};
$scope.slideToggleValue = false;
$scope.currentValue = map[$scope.slideToggleValue];
$scope.attributeUpdateFormGroup = $scope.fb.group({slideToggleValue: [$scope.slideToggleValue]});
$scope.changed = function() {
$scope.slideToggleValue = $scope.attributeUpdateFormGroup.get('slideToggleValue').value;
$scope.currentValue = map[$scope.slideToggleValue];
$scope.updateAttribute();
};
if (self.ctx.datasources && self.ctx.datasources.length) {
var datasource = self.ctx.datasources[0];
if (datasource.type === 'entity') {
if (datasource.entityType === 'DEVICE') {
if (datasource.entityType && datasource.entityId) {
$scope.entityName = datasource.entityName;
if (settings.widgetTitle && settings.widgetTitle.length) {
$scope.titleTemplate = utils.customTranslation(settings.widgetTitle, settings.widgetTitle);
} else {
$scope.titleTemplate = self.ctx.widgetConfig.title;
}
$scope.entityDetected = true;
}
} else {
$scope.message = translate.instant('widgets.input-widgets.not-allowed-entity');
}
}
if (datasource.dataKeys.length) {
if (datasource.dataKeys[0].type !== "attribute") {
$scope.isValidParameter = false;
} else {
$scope.currentKey = datasource.dataKeys[0].name;
$scope.dataKeyType = datasource.dataKeys[0].type;
$scope.dataKeyDetected = true;
}
}
}
self.ctx.widgetTitle = utils.createLabelFromDatasource(self.ctx.datasources[0], $scope.titleTemplate);
$scope.updateAttribute = function() {
if ($scope.entityDetected) {
var datasource = self.ctx.datasources[0];
var valueToSend = $scope.slideToggleValue === 2 ? 2 : 1;
console.log("ValueToSend: ", valueToSend)
attributeService.saveEntityAttributes(
datasource.entity.id,
'SHARED_SCOPE',
[
{
key: $scope.currentKey,
value: valueToSend
// value: $scope.slideToggleValue || false
}
]
).subscribe(
function success() {
if (settings.showResultMessage) {
$scope.showSuccessToast(translate.instant('widgets.input-widgets.update-successful'), 1000, 'bottom', 'left', $scope.toastTargetId);
}
},
function fail() {
if (settings.showResultMessage) {
$scope.showErrorToast(translate.instant('widgets.input-widgets.update-failed'), 'bottom', 'left', $scope.toastTargetId);
}
}
);
console.log("1: ", parseInt($scope.slideToggleValue))
}
};
}
self.onDataUpdated = function() {
try {
if ($scope.dataKeyDetected) {
// $scope.slideToggleValue = self.ctx.data[0].data[0][1] === 'true';
console.log("A:",self.ctx.data[0].data[0][1])
$scope.slideToggleValue = self.ctx.data[0].data[0][1] === 2 ? 2 : 1
$scope.currentValue = map[$scope.slideToggleValue];
$scope.attributeUpdateFormGroup.get('slideToggleValue').patchValue($scope.slideToggleValue);
self.ctx.detectChanges();
console.log("B:",self.ctx.data[0].data)
}
} catch (e) {
console.log(e);
}
}
self.onResize = function() {}
self.typeParameters = function() {
return {
maxDatasources: 1,
maxDataKeys: 1,
singleEntity: true
}
}
self.onDestroy = function() {}
my toggle switch:
<mat-slide-toggle formControlName="slideToggleValue" (change)="changed()"
aria-label="{{'widgets.input-widgets.switch-timeseries-value' | translate}}"
class="custom-slide-toggle">
{{ slideToggleValue.checked ? 2 : 1 }}
</mat-slide-toggle>
