diff --git a/include/hueplusplus/HueDeviceTypes.h b/include/hueplusplus/HueDeviceTypes.h index c7dc455..48e64d6 100644 --- a/include/hueplusplus/HueDeviceTypes.h +++ b/include/hueplusplus/HueDeviceTypes.h @@ -35,7 +35,7 @@ class HueLightFactory public: HueLightFactory(const HueCommandAPI& commands); - HueLight createLight(const std::string& type, int id); + HueLight createLight(const nlohmann::json& lightState, int id); private: HueCommandAPI commands; diff --git a/src/Hue.cpp b/src/Hue.cpp index bb040e2..56d11ec 100644 --- a/src/Hue.cpp +++ b/src/Hue.cpp @@ -231,8 +231,7 @@ HueLight& Hue::getLight(int id) std::cerr << "Error in Hue getLight(): light with id " << id << " is not valid\n"; throw HueException(CURRENT_FILE_INFO, "Light id is not valid"); } - std::string type = lightsCache[std::to_string(id)]["modelid"].get(); - auto light = lightFactory.createLight(type, id); + auto light = lightFactory.createLight(lightsCache[std::to_string(id)], id); lights.emplace(id, light); return lights.find(id)->second; } diff --git a/src/HueDeviceTypes.cpp b/src/HueDeviceTypes.cpp index bff961e..b411c01 100644 --- a/src/HueDeviceTypes.cpp +++ b/src/HueDeviceTypes.cpp @@ -89,42 +89,40 @@ HueLightFactory::HueLightFactory(const HueCommandAPI& commands) extendedColorTemperature(std::make_shared()) {} -HueLight HueLightFactory::createLight(const std::string& type, int id) +HueLight HueLightFactory::createLight(const nlohmann::json& lightState, int id) { - if (getGamutBTypes().count(type)) - { - auto light = HueLight(id, commands, simpleBrightness, extendedColorTemperature, extendedColorHue); - light.colorType = ColorType::GAMUT_B; - return light; - } - else if (getGamutCTypes().count(type)) + std::string type = lightState.value("type", ""); + // Ignore case + std::transform(type.begin(), type.end(), type.begin(), [](char c) { return std::tolower(c); }); + + if (type == "on/off light") { - auto light = HueLight(id, commands, simpleBrightness, extendedColorTemperature, extendedColorHue); - light.colorType = ColorType::GAMUT_C; + HueLight light(id, commands, nullptr, nullptr, nullptr); + light.colorType = ColorType::NONE; return light; } - else if (getGamutATypes().count(type)) + else if (type == "dimmable light") { - auto light = HueLight(id, commands, simpleBrightness, nullptr, simpleColorHue); - light.colorType = ColorType::GAMUT_A; + HueLight light(id, commands, simpleBrightness, nullptr, nullptr); + light.colorType = ColorType::NONE; return light; } - else if (getNoColorTypes().count(type)) + else if (type == "color temperature light") { - auto light = HueLight(id, commands, simpleBrightness, nullptr, nullptr); - light.colorType = ColorType::NONE; + HueLight light(id, commands, simpleBrightness, simpleColorTemperature, nullptr); + light.colorType = ColorType::TEMPERATURE; return light; } - else if (getNonDimmableTypes().count(type)) + else if (type == "color light") { - auto light = HueLight(id, commands); - light.colorType = ColorType::NONE; + HueLight light(id, commands, simpleBrightness, nullptr, simpleColorHue); + light.colorType = ColorType::GAMUT_A; // getColorType(state); return light; } - else if (getTemperatureLightTypes().count(type)) + else if (type == "extended color light") { - auto light = HueLight(id, commands, simpleBrightness, simpleColorTemperature, nullptr); - light.colorType = ColorType::TEMPERATURE; + HueLight light(id, commands, simpleBrightness, extendedColorTemperature, extendedColorHue); + light.colorType = ColorType::GAMUT_B_TEMPERATURE; // getColorType(state); return light; } std::cerr << "Could not determine HueLight type:" << type << "!\n";