Kodi retrive screensaver status

Hi, it could be really usefull if we can read when kodi goes in screensaver status. For example, devices like raspberry or other tv player with linux-kodi are left always on.
Thanks

I actually wrote some custom code for this, so when an automation tries to play media (like a camera after a motion event), it will check to see if it is on screensaver first. It would be much better to have this as part of the new Kodi device integration, which could include automation actions such as turning on the screensaver.

I am currently adjusting the code, but when I am done I will post it, it consists of a script, an automation, and a helper, and has worked flawlessly so far

So I have been assuming you are referring to display sleep as “screensaver”, though they are actually different, the screensaver is when it dims the screen or sends a black video image to the monitor, the display sleep turns off the video device output, and the monitor or tv will then turn itself off or go to sleep.

The script:

  alias: QueryKodiDPMS
  mode: single
  sequence:
  - data:
      booleans:
      - System.DPMSActive
      entity_id: media_player.kodi
      method: XBMC.GetInfoBooleans
    service: kodi.call_method

The automation:

- id: '1600837653789'
  alias: Kodi SetDPMS
  description: Sets a helper representing the DMPS status of Kodi after the script
    runs
  trigger:
  - event_data:
      entity_id: media_player.kodi
    event_type: kodi_call_method_result
    platform: event
  condition: []
  action:
  - choose:
    - conditions:
      - condition: template
        value_template: '{{ ( trigger.event.data.result["System.DPMSActive"] == False
          ) }}'
      sequence:
      - entity_id: input_boolean.kodi_sleeping
        service: input_boolean.turn_off
    - conditions:
      - condition: template
        value_template: '{{ ( trigger.event.data.result["System.DPMSActive"] == True
          ) }}'
      sequence:
      - entity_id: input_boolean.kodi_sleeping
        service: input_boolean.turn_on
    default: []
  mode: single

Then add a helper named Kodi Sleeping with entity id input_boolean.kodi_sleeping and icon mdi:power-sleep

When the script is run, it asks kodi if the display link power management is on (screen off), and the response is handled by the automation, which sets the helper to the correct value.

To use in another automation, call the script, wait 1s, then query the helper value. My kodi box and HA server are very fast, but a slower device might take more than 1s to get the helper set, run the script a few times and check the logbook to get an idea.

This is obviously for a Kodi entity with id media_player.kodi

If I am right, adding this to HA as a property of the media player should be trivial, since the websocket connection will receive a notification when Kodi toggles its DPMS state

I made some adjustments to the kodi media player component code that would expose the state, but I am not sure if the way I did it is a good idea, since the player will still play if the display is off, and if you are using a different audio device it will be listed as sleeping even though it is playing. It might be a better idea to expose it differently:

https://github.com/home-assistant/core/commit/1366f8ccf318fe85ed0966075fd71f65da54d7f1

If one of the core devs would take a look that would be great, I am NOT a python dev

1 Like

Thanks for your work, i’m sure we are not alone with this needs.
I tried directly the Kodi updated plugin but i got this:

Logger: homeassistant.components.media_player
Source: custom_components/kodi/media_player.py:275

Error while setting up kodi platform for media_player
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 193, in _async_setup_platform
    await asyncio.shield(task)
  File "/config/custom_components/kodi/media_player.py", line 240, in async_setup_entry
    entity = KodiEntity(connection, kodi, name, uid, version)
  File "/config/custom_components/kodi/media_player.py", line 275, in __init__
    self._dpms = false
NameError: name 'false' is not defined

I think false needs to be False and true needs to be True, capital first letters

Ho again, yes with caps it’s loading, but appear as idle in both condition, on screensaver and on stops

Got it! Was missing the forced update which tells HA the device state

This is now returning “Standby” when DPMS is active, whether it was idle or playing before it put the display to sleep. I still do not know if that is a good idea, but if it works for us now that is great.

https://github.com/home-assistant/core/blob/f25327add73980e6fab575a12a1f52fe60d4d3f7/homeassistant/components/kodi/media_player.py

I tried to update media_player.py but still appear as IDLE, currently i’m in office. I wanna check at home, maybe kodi doen’t send its state as it’s already on dpms_on
I’ll let you know in few hours, always thank you for your work!

Checked now, it refreshed itself and now appear as powered-off. Awesome work, i think this should be in the stable release of this plugin.
Really thank you for your help!

There is a limitation, if HA starts when the Kodi DPMS is active, there is currently no function in the code to force query the status on connect to update immediately.

All other sequences of events should show the state correctly. I think the next step would be adding more available states to the HA constant list such as “sleeping” “power save” “suspended” “loading” to facilitate future development of both the core code and new integrations

Unfortunately now appear again as idle, but Haas was running already when Kodi goes on screensaver. How can i help you to debug?

I have had 0 false state updates so far, everything has been accurate and state changes occur in HA within 1s of Kodi changing state. 0.115 currently does have some severe state issue bugs, it may be related to that, I think they are trying to fix for next version. I will keep a close watch on the state changes to see if there is any funny business

I created a kodi folder inside CUSTOM_COMPONENTS and cloned here the github subfolder. Could be that there’s something which conflict with the standard kodi components. It should not because CUSTOM_COMPONENTS should override standard ones, but maybe something is wrong…

I actually replaced the file inside the docker container after renaming the old file, so there is no possibility of conflict, but the next time i upgrade i will need to do it again. It is still working 100% here

Ok found the solution. You were right, the problem is that i’m looking for the wrong thing!

a) you can copy the kodi component folder (from docker src or from github) into CUSTOM_COMPONENTS and overwrite here the media_player.py leaving untouched the docker container. Hass.io load the custom_compontents one and leave unloaded the default, so no need to edit the container every time you update hass.io

b) in the media_player.py you are looking for DPMS event, which is right and fine. But when kodi is running on amlogic (coreelec or libreelec) DPMS is totally out because there’s not an X11 system. So we need to check the pure screensaver event here:

        self._connection.server.GUI.OnScreensaverActivated = self.async_on_dpmson
        self._connection.server.GUI.OnScreensaverDeactivated = self.async_on_dpmsoff

indeed on these platform the screen_off command is controlled by HDMI-CEC driver on screensaver activation/deactivation.

I am using a NUC with no CEC support, the TV only goes off when DPMS stops sending video to the TV (or when I power it off).

Supporting both scenarios would be… more complex, my screensaver runs after 7 mins to dim the screen, for now I assume we will be using this as custom code with modifications

Actually having an attribute (not state) for screensaver AND DPMS is a great idea, I will probably create a PR for that after some testing, I will use state changes until then

Hi, i’m also really interestent in this feature, since my TV neither responds properly to CEC nor to no-signal. So my plan is to toggle the power of the tv by home-assistant when the screensaver goes active.

How is the current state of this regarding upstreaming/PR?

I think having an own attribute for screensaver and/or DPMS instead of state is could be needed anyways, since afaik it’s possible to have music playing in kodi while the screensaver or the DPMS is active.

I have a new WIP code against the latest HA dev branch that will query both the screensaver and dpms status when the kodi entity goes live, as well as update when the status changes. This is done by calling the XBMC.GetInfoBooleans method right after the websocket is connected and parsing the results, as well as adding listeners for the change methods.

However, instead of changing the state, it adds attributes for both screensaver and dpms, so it is no longer limited to a “common” standby or sleeping state, as per the previous discussions

HOWEVER, I do not know if this is kosher re: HA arch and code requirements, so a PR may not make it live, it also makes assumptions about the received response being in the correct format, etc. I just got the DPMS test done a few minutes ago

and here is the code!:

Hi everyone, to revive this topic a little bit…
I create a PR, based on the feedback to @richieframe’s PR, which adds screensaver and DPMS information as two binary sensor entities to the Kodi platform. Just in case someone is in the same need as me.