Toggle Chromecast Audio to off when amplifier switch turns to off



I have in my kitchen an amplifier on a tasmoata switch with a Chromecast Audio connected to it.
Whenever is flip my Xiaomi Cue 90 degrees, my switch toggles from on to off or the other way around.

Right now, my Chromecast stays on. But what I want to have is this;

If I toggle my switch to on, I want to have a test if my chromecast is on and if so, stay on but play StudioBrussel. But if it is off, I want my CA turn on and stat playing.

And when I turn the switch off, I want the CA to stop playing and turn off.

I really don’t know how to code this. I think I has something to do with State. But what?

here is my code;

  • alias: Versterker keuken
    platform: event
    event_type: xiaomi_aqara.cube_action
    entity_id: binary_sensor.cube_158private
    action_type: flip90
    • service: switch.toggle
      entity_id: switch.keukenradio
    • service: media_player.play_media
      entity_id: media_player.harry
      media_content_type: ‘audio/mp4’