mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-30 22:53:59 +00:00 
			
		
		
		
	[media_player] Add new media player conditions (#7667)
This commit is contained in:
		| @@ -21,6 +21,7 @@ media_player_ns = cg.esphome_ns.namespace("media_player") | |||||||
|  |  | ||||||
| MediaPlayer = media_player_ns.class_("MediaPlayer") | MediaPlayer = media_player_ns.class_("MediaPlayer") | ||||||
|  |  | ||||||
|  |  | ||||||
| PlayAction = media_player_ns.class_( | PlayAction = media_player_ns.class_( | ||||||
|     "PlayAction", automation.Action, cg.Parented.template(MediaPlayer) |     "PlayAction", automation.Action, cg.Parented.template(MediaPlayer) | ||||||
| ) | ) | ||||||
| @@ -60,7 +61,11 @@ AnnoucementTrigger = media_player_ns.class_( | |||||||
|     "AnnouncementTrigger", automation.Trigger.template() |     "AnnouncementTrigger", automation.Trigger.template() | ||||||
| ) | ) | ||||||
| IsIdleCondition = media_player_ns.class_("IsIdleCondition", automation.Condition) | IsIdleCondition = media_player_ns.class_("IsIdleCondition", automation.Condition) | ||||||
|  | IsPausedCondition = media_player_ns.class_("IsPausedCondition", automation.Condition) | ||||||
| IsPlayingCondition = media_player_ns.class_("IsPlayingCondition", automation.Condition) | IsPlayingCondition = media_player_ns.class_("IsPlayingCondition", automation.Condition) | ||||||
|  | IsAnnouncingCondition = media_player_ns.class_( | ||||||
|  |     "IsAnnouncingCondition", automation.Condition | ||||||
|  | ) | ||||||
|  |  | ||||||
|  |  | ||||||
| async def setup_media_player_core_(var, config): | async def setup_media_player_core_(var, config): | ||||||
| @@ -159,9 +164,15 @@ async def media_player_play_media_action(config, action_id, template_arg, args): | |||||||
| @automation.register_condition( | @automation.register_condition( | ||||||
|     "media_player.is_idle", IsIdleCondition, MEDIA_PLAYER_ACTION_SCHEMA |     "media_player.is_idle", IsIdleCondition, MEDIA_PLAYER_ACTION_SCHEMA | ||||||
| ) | ) | ||||||
|  | @automation.register_condition( | ||||||
|  |     "media_player.is_paused", IsPausedCondition, MEDIA_PLAYER_ACTION_SCHEMA | ||||||
|  | ) | ||||||
| @automation.register_condition( | @automation.register_condition( | ||||||
|     "media_player.is_playing", IsPlayingCondition, MEDIA_PLAYER_ACTION_SCHEMA |     "media_player.is_playing", IsPlayingCondition, MEDIA_PLAYER_ACTION_SCHEMA | ||||||
| ) | ) | ||||||
|  | @automation.register_condition( | ||||||
|  |     "media_player.is_announcing", IsAnnouncingCondition, MEDIA_PLAYER_ACTION_SCHEMA | ||||||
|  | ) | ||||||
| async def media_player_action(config, action_id, template_arg, args): | async def media_player_action(config, action_id, template_arg, args): | ||||||
|     var = cg.new_Pvariable(action_id, template_arg) |     var = cg.new_Pvariable(action_id, template_arg) | ||||||
|     await cg.register_parented(var, config[CONF_ID]) |     await cg.register_parented(var, config[CONF_ID]) | ||||||
|   | |||||||
| @@ -68,5 +68,15 @@ template<typename... Ts> class IsPlayingCondition : public Condition<Ts...>, pub | |||||||
|   bool check(Ts... x) override { return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_PLAYING; } |   bool check(Ts... x) override { return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_PLAYING; } | ||||||
| }; | }; | ||||||
|  |  | ||||||
|  | template<typename... Ts> class IsPausedCondition : public Condition<Ts...>, public Parented<MediaPlayer> { | ||||||
|  |  public: | ||||||
|  |   bool check(Ts... x) override { return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_PAUSED; } | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | template<typename... Ts> class IsAnnouncingCondition : public Condition<Ts...>, public Parented<MediaPlayer> { | ||||||
|  |  public: | ||||||
|  |   bool check(Ts... x) override { return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_ANNOUNCING; } | ||||||
|  | }; | ||||||
|  |  | ||||||
| }  // namespace media_player | }  // namespace media_player | ||||||
| }  // namespace esphome | }  // namespace esphome | ||||||
|   | |||||||
| @@ -37,6 +37,10 @@ const char *media_player_command_to_string(MediaPlayerCommand command) { | |||||||
|       return "UNMUTE"; |       return "UNMUTE"; | ||||||
|     case MEDIA_PLAYER_COMMAND_TOGGLE: |     case MEDIA_PLAYER_COMMAND_TOGGLE: | ||||||
|       return "TOGGLE"; |       return "TOGGLE"; | ||||||
|  |     case MEDIA_PLAYER_COMMAND_VOLUME_UP: | ||||||
|  |       return "VOLUME_UP"; | ||||||
|  |     case MEDIA_PLAYER_COMMAND_VOLUME_DOWN: | ||||||
|  |       return "VOLUME_DOWN"; | ||||||
|     default: |     default: | ||||||
|       return "UNKNOWN"; |       return "UNKNOWN"; | ||||||
|   } |   } | ||||||
|   | |||||||
| @@ -27,6 +27,10 @@ media_player: | |||||||
|           media_player.is_idle: |           media_player.is_idle: | ||||||
|       - wait_until: |       - wait_until: | ||||||
|           media_player.is_playing: |           media_player.is_playing: | ||||||
|  |       - wait_until: | ||||||
|  |           media_player.is_announcing: | ||||||
|  |       - wait_until: | ||||||
|  |           media_player.is_paused: | ||||||
|       - media_player.volume_up: |       - media_player.volume_up: | ||||||
|       - media_player.volume_down: |       - media_player.volume_down: | ||||||
|       - media_player.volume_set: 50% |       - media_player.volume_set: 50% | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user