Skip to content

Commit

Permalink
pick up icon override from entities card
Browse files Browse the repository at this point in the history
  • Loading branch information
Habbie committed Dec 15, 2024
1 parent 037eceb commit 38cc136
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/front-lvgl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,13 @@ void renderCard(std::vector<std::unique_ptr<UIEntity>>& uielements, nlohmann::ba
auto objs = card["entities"];
for (auto ent : objs) {
string entityname;
string icon;
if (ent.type() == json::value_t::string) {
entityname = ent;
}
else {
entityname = ent["entity"];
icon = ent.value("icon", "");
}
std::shared_ptr<HAEntity> entity = HABackend::getInstance().getEntityByName(entityname);
if (entity->getEntityType() == EntityType::Light) {
Expand All @@ -100,7 +102,7 @@ void renderCard(std::vector<std::unique_ptr<UIEntity>>& uielements, nlohmann::ba
uielements.push_back(std::move(btn));
}
else if (entity->getEntityType() == EntityType::Sensor) {
std::unique_ptr<UIEntity> sensor = std::make_unique<UISensor>(entity, cont_row);
std::unique_ptr<UIEntity> sensor = std::make_unique<UISensor>(entity, cont_row, icon);
uielements.push_back(std::move(sensor));
}
else {
Expand Down
10 changes: 7 additions & 3 deletions src/uicomponents/UIComponents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,12 @@ void UIDummy::update()
}
};

string getIconFor(std::shared_ptr<HAEntity> _entity) // for now, this function -always- returns something that starts with mdi:
// if _icon is passed, we got one from the dashboard, use that
string getIconFor(std::shared_ptr<HAEntity> _entity, std::string _icon) // for now, this function -always- returns something that starts with mdi:
{
if (!_icon.empty()) {
return _icon;
}
json state = _entity->getJsonState();

// 1. see if the state simply contains an icon - user might have set it explicitly
Expand Down Expand Up @@ -218,7 +222,7 @@ string getIconFor(std::shared_ptr<HAEntity> _entity) // for now, this function -
return "mdi:border-none";
}

UISensor::UISensor(std::shared_ptr<HAEntity> _entity, lv_obj_t* _parent) :
UISensor::UISensor(std::shared_ptr<HAEntity> _entity, lv_obj_t* _parent, std::string _icon) :
UIEntity(_entity, _parent)
{
lv_obj_t* flowpanel = lv_obj_create(_parent);
Expand All @@ -235,7 +239,7 @@ UISensor::UISensor(std::shared_ptr<HAEntity> _entity, lv_obj_t* _parent) :
lv_obj_set_height(iconpart, LV_SIZE_CONTENT);
lv_obj_set_style_pad_all(iconpart, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
lv_obj_set_style_border_width(iconpart, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
string icon = getIconFor(_entity);
string icon = getIconFor(_entity, _icon);
// cerr << "iconmap[" << _entity->platform << "," << _entity->translation_key << "]=" << voorkant::lvgl::iconmap[{_entity->platform, _entity->translation_key}] << endl;

if (icon.substr(0, 4) == "mdi:") {
Expand Down
2 changes: 1 addition & 1 deletion src/uicomponents/UIComponents.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class UIDummy : public UIEntity
class UISensor : public UIEntity
{
public:
UISensor(std::shared_ptr<HAEntity> _entity, lv_obj_t* _parent);
UISensor(std::shared_ptr<HAEntity> _entity, lv_obj_t* _parent, std::string _icon = "");
void update() override;

private:
Expand Down

0 comments on commit 38cc136

Please sign in to comment.