From b58c5646dffcc459e3f55b081e15e9ab58745b91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Tue, 10 Feb 2026 09:46:27 +0100 Subject: [PATCH] Thing: Add state name based set method for possible values --- libnymea/integrations/browseresult.h | 7 ++++--- libnymea/integrations/browseritemresult.h | 7 ++++--- libnymea/integrations/thing.cpp | 25 +++++++++++++++-------- libnymea/integrations/thing.h | 4 ++-- 4 files changed, 26 insertions(+), 17 deletions(-) diff --git a/libnymea/integrations/browseresult.h b/libnymea/integrations/browseresult.h index f8c684bb..aaf94757 100644 --- a/libnymea/integrations/browseresult.h +++ b/libnymea/integrations/browseresult.h @@ -3,7 +3,7 @@ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Copyright (C) 2013 - 2024, nymea GmbH -* Copyright (C) 2024 - 2025, chargebyte austria GmbH +* Copyright (C) 2024 - 2026, chargebyte austria GmbH * * This file is part of nymea. * @@ -25,10 +25,11 @@ #ifndef BROWSERESULT_H #define BROWSERESULT_H -#include #include +#include #include "thing.h" +#include "types/browseritem.h" class ThingManager; @@ -38,7 +39,7 @@ class BrowseResult : public QObject public: explicit BrowseResult(Thing *thing, ThingManager *thingManager, const QString &itemId, const QLocale &locale, QObject *parent, quint32 timeout = 0); - Thing* thing() const; + Thing *thing() const; QString itemId() const; QLocale locale() const; diff --git a/libnymea/integrations/browseritemresult.h b/libnymea/integrations/browseritemresult.h index e2aa2c4e..d52b5724 100644 --- a/libnymea/integrations/browseritemresult.h +++ b/libnymea/integrations/browseritemresult.h @@ -3,7 +3,7 @@ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Copyright (C) 2013 - 2024, nymea GmbH -* Copyright (C) 2024 - 2025, chargebyte austria GmbH +* Copyright (C) 2024 - 2026, chargebyte austria GmbH * * This file is part of nymea. * @@ -25,10 +25,11 @@ #ifndef BROWSERITEMRESULT_H #define BROWSERITEMRESULT_H -#include #include +#include #include "thing.h" +#include "types/browseritem.h" class ThingManager; @@ -39,7 +40,7 @@ class BrowserItemResult : public QObject public: explicit BrowserItemResult(Thing *thing, ThingManager *thingManager, const QString &itemId, const QLocale &locale, QObject *parent, quint32 timeout = 0); - Thing* thing() const; + Thing *thing() const; QString itemId() const; QLocale locale() const; diff --git a/libnymea/integrations/thing.cpp b/libnymea/integrations/thing.cpp index 9962f192..f9a4bd7d 100644 --- a/libnymea/integrations/thing.cpp +++ b/libnymea/integrations/thing.cpp @@ -3,7 +3,7 @@ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Copyright (C) 2013 - 2024, nymea GmbH -* Copyright (C) 2024 - 2025, chargebyte austria GmbH +* Copyright (C) 2024 - 2026, chargebyte austria GmbH * * This file is part of nymea. * @@ -313,7 +313,6 @@ States Thing::states() const return m_states; } - /*! Returns true, a \l{Param} with the given \a paramTypeId exists for this thing. */ bool Thing::hasParam(const QString ¶mName) const { @@ -321,7 +320,6 @@ bool Thing::hasParam(const QString ¶mName) const return m_params.hasParam(paramTypeId); } - /*! Returns true, a \l{Param} with the given \a paramTypeId exists for this thing. */ bool Thing::hasParam(const ParamTypeId ¶mTypeId) const { @@ -516,7 +514,7 @@ void Thing::setStateValue(const QString &stateName, const QVariant &value) setStateValue(stateTypeId, value); } -/*! Sets the minimum value for the \l{State} matching the given \a stateTypeId in this thing to value. */ +/*! Sets the minimum value for the \l{State} matching the given \a stateTypeId in this thing to \a minValue. */ void Thing::setStateMinValue(const StateTypeId &stateTypeId, const QVariant &minValue) { StateType stateType = m_thingClass.stateTypes().findById(stateTypeId); @@ -559,14 +557,14 @@ void Thing::setStateMinValue(const StateTypeId &stateTypeId, const QVariant &min qCWarning(dcThing()).nospace() << this << ": Failed setting minimum state value " << stateType.name() << " to " << minValue; } -/*! Sets the minimum value for the \l{State} matching the given \a stateName in this thing to value. */ +/*! Sets the minimum value for the \l{State} matching the given \a stateName in this thing to \a minValue. */ void Thing::setStateMinValue(const QString &stateName, const QVariant &minValue) { StateTypeId stateTypeId = m_thingClass.stateTypes().findByName(stateName).id(); setStateMinValue(stateTypeId, minValue); } -/*! Sets the maximum value for the \l{State} matching the given \a stateTypeId in this thing to value. */ +/*! Sets the maximum value for the \l{State} matching the given \a stateTypeId in this thing to \a maxValue. */ void Thing::setStateMaxValue(const StateTypeId &stateTypeId, const QVariant &maxValue) { StateType stateType = m_thingClass.stateTypes().findById(stateTypeId); @@ -619,7 +617,7 @@ void Thing::setStateMaxValue(const StateTypeId &stateTypeId, const QVariant &max qCWarning(dcThing()).nospace() << this << ": Failed setting maximum state value " << stateType.name() << " to " << maxValue; } -/*! Sets the maximum value for the \l{State} matching the given \a stateName in this thing to value. */ +/*! Sets the maximum value for the \l{State} matching the given \a stateName in this thing to \a maxValue. */ void Thing::setStateMaxValue(const QString &stateName, const QVariant &maxValue) { StateTypeId stateTypeId = m_thingClass.stateTypes().findByName(stateName).id(); @@ -697,6 +695,14 @@ void Thing::setStateMinMaxValues(const QString &stateName, const QVariant &minVa setStateMinMaxValues(stateTypeId, minValue, maxValue); } +/*! Sets the possible values for the \l{State} matching the given \a stateName in this thing to \a values. */ +void Thing::setStatePossibleValues(const QString &stateName, const QVariantList &values) +{ + const StateTypeId stateTypeId = m_thingClass.stateTypes().findByName(stateName).id(); + setStatePossibleValues(stateTypeId, values); +} + +/*! Sets the possible values for the \l{State} matching the given \a stateTypeId in this thing to \a values. */ void Thing::setStatePossibleValues(const StateTypeId &stateTypeId, const QVariantList &values) { StateType stateType = m_thingClass.stateTypes().findById(stateTypeId); @@ -704,6 +710,7 @@ void Thing::setStatePossibleValues(const StateTypeId &stateTypeId, const QVarian qCWarning(dcThing()) << "No such state type" << stateTypeId.toString() << "in" << m_name << "(" + thingClass().name() + ")"; return; } + for (int i = 0; i < m_states.count(); ++i) { if (m_states.at(i).stateTypeId() == stateTypeId) { if (values == m_states.at(i).possibleValues()) { @@ -739,10 +746,10 @@ void Thing::setStatePossibleValues(const StateTypeId &stateTypeId, const QVarian return; } } - qCWarning(dcThing()).nospace() << this << ": Failed setting maximum state value " << stateType.name() << " to " << values; + qCWarning(dcThing()).nospace() << this << ": Failed setting possible state values " << stateType.name() << " to " << values; Q_ASSERT_X(false, m_name.toUtf8(), - QString("Failed setting possible state values for %1 to %2").arg(stateType.name()).arg(QString(QJsonDocument::fromVariant(values).toJson())).toUtf8()); + QString("Failed setting possible state values for %1 to %2").arg(stateType.name(), QString(QJsonDocument::fromVariant(values).toJson())).toUtf8()); } /*! Returns the \l{State} with the given \a stateTypeId of this thing. */ diff --git a/libnymea/integrations/thing.h b/libnymea/integrations/thing.h index c28e3337..fb520612 100644 --- a/libnymea/integrations/thing.h +++ b/libnymea/integrations/thing.h @@ -3,7 +3,7 @@ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Copyright (C) 2013 - 2024, nymea GmbH -* Copyright (C) 2024 - 2025, chargebyte austria GmbH +* Copyright (C) 2024 - 2026, chargebyte austria GmbH * * This file is part of nymea. * @@ -32,7 +32,6 @@ #include "types/state.h" #include "types/param.h" #include "types/event.h" -#include "types/browseritem.h" #include #include @@ -145,6 +144,7 @@ public: Q_INVOKABLE void setStateMinMaxValues(const StateTypeId &stateTypeId, const QVariant &minValue, const QVariant &maxValue); Q_INVOKABLE void setStateMinMaxValues(const QString &stateName, const QVariant &minValue, const QVariant &maxValue); Q_INVOKABLE void setStatePossibleValues(const StateTypeId &stateTypeId, const QVariantList &values); + Q_INVOKABLE void setStatePossibleValues(const QString &stateName, const QVariantList &values); Q_INVOKABLE State state(const StateTypeId &stateTypeId) const; Q_INVOKABLE State state(const QString &stateName) const;