[media_player] Use register_apply_condition for the state and muted conditions (#19557)

This commit is contained in:
J. Nick Koston
2026-09-24 08:55:39 -04:00
committed by GitHub
parent 1861d8282f
commit 6768cb818b
2 changed files with 18 additions and 67 deletions
+18 -30
View File
@@ -190,16 +190,15 @@ _STATE_TRIGGERS = (
(CONF_ON_TURN_OFF, MediaPlayerState.MEDIA_PLAYER_STATE_OFF),
)
# State conditions that all share the same schema and codegen handler
_STATE_CONDITIONS = [
"idle",
"paused",
"playing",
"announcing",
"on",
"off",
"muted",
]
# State conditions: (config_key suffix, checked state)
_STATE_CONDITIONS = (
("idle", MediaPlayerState.MEDIA_PLAYER_STATE_IDLE),
("paused", MediaPlayerState.MEDIA_PLAYER_STATE_PAUSED),
("playing", MediaPlayerState.MEDIA_PLAYER_STATE_PLAYING),
("announcing", MediaPlayerState.MEDIA_PLAYER_STATE_ANNOUNCING),
("on", MediaPlayerState.MEDIA_PLAYER_STATE_ON),
("off", MediaPlayerState.MEDIA_PLAYER_STATE_OFF),
)
MediaPlayerCommand = media_player_ns.enum("MediaPlayerCommand", is_class=True)
@@ -360,27 +359,16 @@ for _action_name in _COMMAND_ACTIONS:
)
def _snake_to_camel(name):
return "".join(word.capitalize() for word in name.split("_"))
for _condition_name, _state in _STATE_CONDITIONS:
automation.register_apply_condition(
f"media_player.is_{_condition_name}",
MEDIA_PLAYER_CONDITION_SCHEMA,
f"state == {_state}",
)
def _register_state_conditions():
async def handler(config, action_id, template_arg, args):
var = cg.new_Pvariable(action_id, template_arg)
await cg.register_parented(var, config[CONF_ID])
return var
for condition_name in _STATE_CONDITIONS:
class_name = f"Is{_snake_to_camel(condition_name)}Condition"
condition_class = media_player_ns.class_(class_name, automation.Condition)
automation.register_condition(
f"media_player.is_{condition_name}",
condition_class,
MEDIA_PLAYER_CONDITION_SCHEMA,
)(handler)
_register_state_conditions()
automation.register_apply_condition(
"media_player.is_muted", MEDIA_PLAYER_CONDITION_SCHEMA, "is_muted()"
)
automation.register_apply_action(
@@ -27,41 +27,4 @@ static_assert(std::is_trivially_copyable_v<StateAnyForwarder>);
static_assert(sizeof(StateEnterForwarder<MediaPlayerState::MEDIA_PLAYER_STATE_IDLE>) <= sizeof(void *));
static_assert(std::is_trivially_copyable_v<StateEnterForwarder<MediaPlayerState::MEDIA_PLAYER_STATE_IDLE>>);
template<typename... Ts> class IsIdleCondition final : public Condition<Ts...>, public Parented<MediaPlayer> {
public:
bool check(const Ts &...x) override { return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_IDLE; }
};
template<typename... Ts> class IsPlayingCondition final : public Condition<Ts...>, public Parented<MediaPlayer> {
public:
bool check(const Ts &...x) override { return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_PLAYING; }
};
template<typename... Ts> class IsPausedCondition final : public Condition<Ts...>, public Parented<MediaPlayer> {
public:
bool check(const Ts &...x) override { return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_PAUSED; }
};
template<typename... Ts> class IsAnnouncingCondition final : public Condition<Ts...>, public Parented<MediaPlayer> {
public:
bool check(const Ts &...x) override {
return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_ANNOUNCING;
}
};
template<typename... Ts> class IsOnCondition final : public Condition<Ts...>, public Parented<MediaPlayer> {
public:
bool check(const Ts &...x) override { return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_ON; }
};
template<typename... Ts> class IsOffCondition final : public Condition<Ts...>, public Parented<MediaPlayer> {
public:
bool check(const Ts &...x) override { return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_OFF; }
};
template<typename... Ts> class IsMutedCondition final : public Condition<Ts...>, public Parented<MediaPlayer> {
public:
bool check(const Ts &...x) override { return this->parent_->is_muted(); }
};
} // namespace esphome::media_player