Skip to content

Commit

Permalink
Improve alarm_control_panel, binary_sensor and vacuum translations
Browse files Browse the repository at this point in the history
* Enable alarm page button translations
* Allow arm_custom_bypass action to be used on alarm page
* Attempt to translate all generic states
  • Loading branch information
olicooper committed Aug 28, 2024
1 parent 6239a81 commit cd6d0b8
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 34 deletions.
21 changes: 8 additions & 13 deletions components/nspanel_lovelace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,8 @@ def AUTO_LOAD():
TRANSLATION_ITEM = nspanel_lovelace_ns.enum("translation_item", True)

ALARM_ARM_ACTION = nspanel_lovelace_ns.enum("alarm_arm_action", True)
ALARM_ARM_OPTIONS = ['arm_home','arm_away','arm_night','arm_vacation']
ALARM_ARM_OPTION_MAP = {
'arm_home': [ALARM_ARM_ACTION.arm_home, "Arm Home"],
'arm_away': [ALARM_ARM_ACTION.arm_away, "Arm Away"],
'arm_night': [ALARM_ARM_ACTION.arm_night, "Arm Night"],
'arm_vacation': [ALARM_ARM_ACTION.arm_vacation, "Arm Vacation"],
}
ALARM_ARM_OPTIONS = ['arm_home','arm_away','arm_night','arm_vacation','arm_custom_bypass']
ALARM_ARM_DEFAULT_OPTIONS = ALARM_ARM_OPTIONS[:4]

TEMPERATURE_UNIT = nspanel_lovelace_ns.enum("temperature_unit_t", True)
TEMPERATURE_UNIT_OPTIONS = ['celcius','fahrenheit']
Expand All @@ -76,7 +71,9 @@ def AUTO_LOAD():
"comfort","eco","home","sleep","cool","cooling","dry","drying","fan","heat","heating",
"heat_cool","idle","auto","fan_only","on","off","currently","state","action","lock","unlock",
"paused","active","activate","press","run","speed","brightness","color","color_temp",
"position","start","pause","cancel","finish","disarm","tilt_position",
"position","start","pause","cancel","finish","arm_home","arm_away","arm_night","arm_vacation",
"arm_custom_bypass","armed_home","armed_away","armed_night","armed_vacation","armed_custom_bypass",
"arming","disarmed","pending","triggered","disarm","tilt_position",
"above_horizon","below_horizon","not_home","start_cleaning","return_to_base","docked",
"turn_on","turn_off"
]
Expand Down Expand Up @@ -435,7 +432,7 @@ def validate_config(config):
}),
CARD_ALARM: SCHEMA_CARD_BASE.extend({
cv.Required(CONF_CARD_ALARM_ENTITY_ID): valid_entity_id(['alarm_control_panel']),
cv.Optional(CONF_CARD_ALARM_SUPPORTED_MODES, default=ALARM_ARM_OPTIONS):
cv.Optional(CONF_CARD_ALARM_SUPPORTED_MODES, default=ALARM_ARM_DEFAULT_OPTIONS):
cv.All(
cv.ensure_list(cv.one_of(*ALARM_ARM_OPTIONS)),
cv.Length(1, 4, f"Must be a list of up to 4 items from the following list: {ALARM_ARM_OPTIONS}"),
Expand Down Expand Up @@ -620,7 +617,6 @@ async def to_code(config):

cgv = []
for k,v in translationJson.items():
# _LOGGER.info(f"{k},{v}")
if k in REQUIRED_TRANSLATION_KEYS:
if k in cv.RESERVED_IDS:
k += '_'
Expand All @@ -629,8 +625,7 @@ async def to_code(config):
cg.add_define("TRANSLATION_MAP_SIZE", len(cgv))
cg.add_global(cg.RawStatement(
"constexpr FrozenCharMap<const char *, TRANSLATION_MAP_SIZE> "
f"esphome::{nspanel_lovelace_ns}::TRANSLATION_MAP {{{cg.ArrayInitializer(*cgv, multiline=True)}}};"
))
f"esphome::{nspanel_lovelace_ns}::TRANSLATION_MAP {{{cg.ArrayInitializer(*cgv, multiline=True)}}};"))

if CONF_TEMPERATURE_UNIT in locale_config:
cg.add(GlobalConfig.set_temperature_unit(TEMPERATURE_UNIT_OPTION_MAP[locale_config[CONF_TEMPERATURE_UNIT]]))
Expand Down Expand Up @@ -827,7 +822,7 @@ async def to_code(config):
cg.add(card_class.set_qr_text(card_config[CONF_CARD_QR_TEXT]))
elif card_config[CONF_CARD_TYPE] == CARD_ALARM:
for mode in card_config[CONF_CARD_ALARM_SUPPORTED_MODES]:
cg.add(card_class.set_arm_button(ALARM_ARM_OPTION_MAP[mode][0], ALARM_ARM_OPTION_MAP[mode][1]))
cg.add(card_class.add_arm_button(ALARM_ARM_ACTION.class_(mode)))

gen_card_entities(
card_config.get(CONF_CARD_ENTITIES, []),
Expand Down
28 changes: 23 additions & 5 deletions components/nspanel_lovelace/card_items.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ void EntitiesCardEntityItem::on_entity_attribute_change(

void EntitiesCardEntityItem::state_generic_fn(StatefulPageItem *me) {
auto me_ = static_cast<EntitiesCardEntityItem*>(me);
me_->value_ = me_->get_state();
me_->value_ = get_translation(me_->get_state());
}

void EntitiesCardEntityItem::state_on_off_fn(StatefulPageItem *me) {
Expand Down Expand Up @@ -259,11 +259,26 @@ void EntitiesCardEntityItem::state_sun_fn(StatefulPageItem *me) {
me_->value_ = get_translation(me_->get_state());
}

void EntitiesCardEntityItem::state_vacuum_fn(StatefulPageItem *me) {
auto me_ = static_cast<EntitiesCardEntityItem*>(me);
me_->value_ = get_translation(me_->is_state(entity_state::docked) ?
translation_item::start_cleaning : translation_item::return_to_base);
}

void EntitiesCardEntityItem::state_translate_fn(StatefulPageItem *me) {
auto me_ = static_cast<EntitiesCardEntityItem*>(me);
// person: backend.component.person.state
// vacuum: frontend.ui.card.vacuum.actions
me_->value_ = get_translation(me_->get_state());
// Firstly try to find a match for a specific entity type, then
// find a match for the generic state, otherwise use the raw state value
const char *ret;
std::string key = me->get_type();
key.append(1, '.').append(me_->get_state());
if (!try_get_value(TRANSLATION_MAP, ret, key)) {
if (!try_get_value(TRANSLATION_MAP, ret, me_->get_state())) {
me_->value_ = me_->get_state();
return;
}
}
me_->value_ = ret;
}

void EntitiesCardEntityItem::set_on_state_callback_(const char *type) {
Expand Down Expand Up @@ -300,9 +315,12 @@ void EntitiesCardEntityItem::set_on_state_callback_(const char *type) {
this->on_state_callback_ = EntitiesCardEntityItem::state_weather_fn;
} else if (type == entity_type::sun) {
this->on_state_callback_ = EntitiesCardEntityItem::state_sun_fn;
} else if (type == entity_type::vacuum) {
this->on_state_callback_ = EntitiesCardEntityItem::state_vacuum_fn;
} else if (
type == entity_type::person ||
type == entity_type::vacuum) {
type == entity_type::alarm_control_panel ||
type == entity_type::binary_sensor) {
this->on_state_callback_ = EntitiesCardEntityItem::state_translate_fn;
} else {
this->on_state_callback_ = EntitiesCardEntityItem::state_generic_fn;
Expand Down
1 change: 1 addition & 0 deletions components/nspanel_lovelace/card_items.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class EntitiesCardEntityItem :
static void state_lock_fn(StatefulPageItem *me);
static void state_weather_fn(StatefulPageItem *me);
static void state_sun_fn(StatefulPageItem *me);
static void state_vacuum_fn(StatefulPageItem *me);
static void state_translate_fn(StatefulPageItem *me);

void set_on_state_callback_(const char *type) override;
Expand Down
15 changes: 5 additions & 10 deletions components/nspanel_lovelace/cards.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ AlarmCard::~AlarmCard() {

void AlarmCard::accept(PageVisitor& visitor) { visitor.visit(*this); }

bool AlarmCard::set_arm_button(
alarm_arm_action action, const std::string &display_name) {
bool AlarmCard::add_arm_button(alarm_arm_action action) {
if (this->items_.size() >= 4) {
return false;
}
Expand All @@ -123,23 +122,19 @@ bool AlarmCard::set_arm_button(
case alarm_arm_action::arm_vacation:
action_type = button_type::armVacation;
break;
case alarm_arm_action::arm_custom_bypass:
action_type = button_type::armCustomBypass;
break;
}

this->items_.push_back(
std::unique_ptr<AlarmButtonItem>(
new AlarmButtonItem(
std::string(this->uuid_).append(1, '_').append(action_type),
action_type, display_name)));
action_type, get_translation(action_type))));
return true;
}

void AlarmCard::set_disarm_button(const std::string &display_name) {
this->disarm_button_.reset();
this->disarm_button_ = std::unique_ptr<AlarmButtonItem>(
new AlarmButtonItem(std::string(this->uuid_).append("_d"),
button_type::disarm, display_name));
}

void AlarmCard::on_entity_state_change(const std::string &state) {
this->status_icon_flashing_ = false;

Expand Down
4 changes: 1 addition & 3 deletions components/nspanel_lovelace/cards.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,7 @@ class AlarmCard : public Card, public IEntitySubscriber {
void accept(PageVisitor& visitor) override;

void set_show_keypad(bool show_keypad) { this->show_keypad_ = show_keypad; }
bool set_arm_button(
alarm_arm_action action, const std::string &display_name);
void set_disarm_button(const std::string &display_name);
bool add_arm_button(alarm_arm_action action);

void on_entity_state_change(const std::string &state) override;
void on_entity_attribute_change(ha_attr_type attr, const std::string &value) override;
Expand Down
4 changes: 2 additions & 2 deletions components/nspanel_lovelace/page_item_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,15 +414,15 @@ void StatefulPageItem::state_alarm_fn(StatefulPageItem *me) {
if (!me->icon_color_overridden_)
me->icon_color_ = icon.color;
}

// todo: also change colour
void StatefulPageItem::state_sun_fn(StatefulPageItem *me) {
if (me->icon_value_overridden_) return;
if (me->is_state(entity_state::above_horizon))
me->icon_value_ = icon_t::weather_sunset_up;
else
me->icon_value_ = icon_t::weather_sunset_down;
}

// todo: also change colour
void StatefulPageItem::state_lock_fn(StatefulPageItem *me) {
if (me->icon_value_overridden_) return;
if (me->is_state(entity_state::unlocked))
Expand Down
14 changes: 14 additions & 0 deletions components/nspanel_lovelace/translations.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,21 @@ struct translation_item {
static constexpr const char* cancel = ha_action_type::cancel;
static constexpr const char* finish = ha_action_type::finish;
// alarm_control_panel
static constexpr const char* arm_home = button_type::armHome;
static constexpr const char* arm_away = button_type::armAway;
static constexpr const char* arm_night = button_type::armNight;
static constexpr const char* arm_vacation = button_type::armVacation;
static constexpr const char* arm_custom_bypass = button_type::armCustomBypass;
static constexpr const char* disarm = button_type::disarm;
static constexpr const char* disarmed = entity_state::disarmed;
static constexpr const char* arming = entity_state::arming;
static constexpr const char* pending = entity_state::pending;
static constexpr const char* triggered = entity_state::triggered;
static constexpr const char* armed_home = entity_state::armed_home;
static constexpr const char* armed_away = entity_state::armed_away;
static constexpr const char* armed_night = entity_state::armed_night;
static constexpr const char* armed_vacation = entity_state::armed_vacation;
static constexpr const char* armed_custom_bypass = entity_state::armed_custom_bypass;
// cover
static constexpr const char* tilt_position = "tilt_pos";
// sun (backend.component.sun.state)
Expand Down
14 changes: 14 additions & 0 deletions components/nspanel_lovelace/translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,21 @@
"pause": "Pause",
"cancel": "Abbrechen",
"finish": "Ende",
"arm_home": "Aktivieren - Zuhause",
"arm_away": "Aktivieren - Unterwegs",
"arm_night": "Aktivieren - Nacht",
"arm_vacation": "Aktiviere Urlaub",
"arm_custom_bypass": "Benutzerdefinierter Bypass",
"disarm": "Deaktivieren",
"disarmed": "Inaktiv",
"arming": "Aktiv.",
"pending": "Warte",
"triggered": "Ausgel.",
"armed_home": "Aktiv",
"armed_away": "Aktiv",
"armed_night": "Aktiv",
"armed_vacation": "Aktiv",
"armed_custom_bypass": "Aktiv",
"not_home": "Abwesend",
"start_cleaning": "Reinigung starten",
"return_to_base": "Zurück zur Dockingstation",
Expand Down
14 changes: 14 additions & 0 deletions components/nspanel_lovelace/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,21 @@
"pause": "Pause",
"cancel": "Cancel",
"finish": "Finish",
"arm_home": "Arm home",
"arm_away": "Arm away",
"arm_night": "Arm night",
"arm_vacation": "Arm vacation",
"arm_custom_bypass": "Arm bypass",
"disarm": "Disarm",
"disarmed": "Disarm",
"arming": "Arming",
"pending": "Pend",
"triggered": "Trig",
"armed_away": "Armed",
"armed_home": "Armed",
"armed_night": "Armed",
"armed_vacation": "Armed",
"armed_custom_bypass": "Armed",
"not_home": "Away",
"start_cleaning": "Start cleaning",
"return_to_base": "Return to dock",
Expand Down
14 changes: 14 additions & 0 deletions components/nspanel_lovelace/translations/en_GB.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,21 @@
"pause": "Pause",
"cancel": "Cancel",
"finish": "Finish",
"arm_home": "Arm home",
"arm_away": "Arm away",
"arm_night": "Arm night",
"arm_vacation": "Arm vacation",
"arm_custom_bypass": "Arm bypass",
"disarm": "Disarm",
"disarmed": "Disarm",
"arming": "Arming",
"pending": "Pend",
"triggered": "Trig",
"armed_home": "Armed",
"armed_away": "Armed",
"armed_night": "Armed",
"armed_vacation": "Armed",
"armed_custom_bypass": "Armed",
"not_home": "Away",
"start_cleaning": "Start cleaning",
"return_to_base": "Return to dock",
Expand Down
3 changes: 2 additions & 1 deletion components/nspanel_lovelace/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace nspanel_lovelace {

enum class render_page_option : uint8_t { prev, next, screensaver, default_page };

enum class alarm_arm_action : uint8_t { arm_home, arm_away, arm_night, arm_vacation };
enum class alarm_arm_action : uint8_t { arm_home, arm_away, arm_night, arm_vacation, arm_custom_bypass };

static const std::array<std::array<const char *, 2>, 7> dow_names = {
{{"Sun", "Sunday"},
Expand Down Expand Up @@ -420,6 +420,7 @@ struct button_type {
static constexpr const char* armAway = "arm_away";
static constexpr const char* armNight = "arm_night";
static constexpr const char* armVacation = "arm_vacation";
static constexpr const char* armCustomBypass = "arm_custom_bypass";
static constexpr const char* opnSensorNotify = "opnSensorNotify";

// unlock page
Expand Down
2 changes: 2 additions & 0 deletions test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ nspanel_lovelace:
name: Person
- entity_id: text.text
name: Text
- entity_id: alarm_control_panel.security
name: Alarm

- type: cardQR
id: qr_card
Expand Down

0 comments on commit cd6d0b8

Please sign in to comment.