litinoveweedle / SmartIR

⏻ Control Home Assistant climate, fan, media_player and light devices via IR/RF controllers (Broadlink, Xiaomi, MQTT, LOOKin, ESPHome, ZHA)
MIT License
99 stars 31 forks source link

media_player state starts at "unknown" #123

Closed charliejgallo closed 1 month ago

charliejgallo commented 2 months ago

as started here: https://community.home-assistant.io/t/smartir-control-your-climate-tv-and-fan-devices-via-ir-rf-controllers/100798/1540 with the new version i noticed media_players new entity not working and with state set as "unknown"....

in media_player.py, line 89 i see self._state = STATE_UNKNOWN. but then in the "_async_power_sensor_changed" function the state "unknown" is not considered as a posible state... changing line 89 of the original state to self._state = STATE_OFF seems to correct it.

Sorry if im not clear, willing to provide more info upon request....

before this i made a "makeshift" fix by changing the state of the media_player to off ( homeassistant>>developer tools >> State ) and turning the device "on" through the dashboard https://community.home-assistant.io/t/smartir-control-your-climate-tv-and-fan-devices-via-ir-rf-controllers/100798/1543

(HomeAssistant os 13.1 , homeAssistant 2024.8.3, Smartir 1.18, zs06 mqtt controller)

litinoveweedle commented 2 months ago

Hello, thank you for info. I have one question - why is your power_sensor in STATE_UNKNOWN? Is it expected situation/state?

litinoveweedle commented 2 months ago

Also please explain what shall be proposed logic to set device state if you configured power_sensor but it reports STATE_UNKNOWN

charliejgallo commented 2 months ago

Trying to check it out, the previous versions of the library (before this fork) used the original state as OFF and worked that way....

looking into media_player.py , I find this function copied below, and inside the IF statement for the new state, the self._state (line 365) is expecting a previous state of self._state == STATE_OFF, no mention of a STATE_UNKNOWN maybe thats why its not considered as a posible state?

 async def _async_power_sensor_changed(
        self, event: Event[EventStateChangedData]
    ) -> None:
        """Handle power sensor changes."""
        old_state = event.data["old_state"]
        new_state = event.data["new_state"]
        if new_state is None:
            return

        if old_state is not None and new_state.state == old_state.state:
            return

        if new_state.state == STATE_ON and self._state == STATE_OFF:
            self._on_by_remote = True
            self._state = STATE_ON
        elif new_state.state == STATE_OFF:
            self._on_by_remote = False
            if self._state == STATE_ON:
                self._state = STATE_OFF
                # self._source = None
        self.async_write_ha_state()

ill try tonight to play with the code adding the STATE_UNKNOWN in line 365

(sorry for my english and bad redaction)

litinoveweedle commented 2 months ago

OK, maybe I did nto understand it correctly. But what is the state of your power sensor when this situation occurred?

The logic is following:

  1. default state of the entity is set UNKNOWN
  2. previous state of the entity is restored from the HA registry (if it exists)
  3. current state of the entity is set from the power sensor (it shall report only ON or OFF states)

so, is this only first time issue (i.e. 2. is not possible), or is it something wrong with the power sensor (i.e. 3 is not possible)

charliejgallo commented 2 months ago

its only a first time issue, the state does not change from UNKNOWN to anything else (if not edited), after that everything works like a charm!

im personally using an always ON sensor template, so the state of the sensor is always ON, thats why i think it has to do with the UNKNOWN starting state not being considered instead of a "bad" reading of a sensor... (sensor template below)

binary_sensor:
  - platform: template
    sensors:
      always_on_power_sensor:
        value_template: on
        friendly_name: "Always ON Power Sensor"
        device_class: power
        unique_id: binary_sensor.always_on_power_sensor

after having the previous state stored in HA registry (changing with developer tools the State and then turning on the entity through the dashboard) everything works , even after home assistant restarts...

I think that the power sensor function inside media_player.py does not expect a UNKNOWN sensor value (as in the first SmartIR library the original state was set to OFF, not unknown)...

tonight i'm going to edit that part in media_player.py and play a bit with it and posting the results...

thanks again for the work you are doing with this!!

litinoveweedle commented 2 months ago

im personally using an always ON sensor template, so the state of the sensor is always ON, thats why i think it has to do with the UNKNOWN starting state not being considered instead of a "bad" reading of a sensor... (sensor template below)

Well, that is definitely not supported to be used except for this troubleshooting.

I think that the power sensor function inside media_player.py does not expect a UNKNOWN sensor value (as in the first SmartIR library the original state was set to OFF, not unknown)...

Please do not confuse power_sensor state vs smart ir entity state! those are two different states!

Also you did not YET provided debug log neither any required inputs as per issue template. So if you would like any support you will need to submit them, otherwise I will close this issue.

charliejgallo commented 2 months ago

sorry if im not making myself clear, the problem is with the media player entity state not changing from the inicial UNKNOWN to other state...

a4644f3a207a204433e131b1b8c8cb10f4a3d848_2_1380x396

already fixed mine, this issue was up to try to help others like CFFAA that had the same problem...

closing this, maybe someone is better explaining the problem than myself and reopens it

litinoveweedle commented 2 months ago

Sorry but this is not helping. Instead to help others by troubleshooting and solving the root cause you close the issue when asked for the mandatory information you should submitted originality when filling the issue. Well that's the attitude...

This is one more reason for me to start closing any opened issue immediately when it doesn't contains debug logs and/or other required information.

charliejgallo commented 2 months ago

im sorry if closing this issue seemed rude, ill try to add a new media_player with smartir, get a full log of the issue and explain everything here as best as i can ... my intention was always trying to help knowing all the work you are providing with this...

as soon as i gatter everything accordly to the requirements im reopening this issue,

again sorry if my actions seemed rude or misleading,

Charlie

charliejgallo commented 2 months ago

Home Assistant Version: 2024.8.3

SmartIR version: 1.18

SmartIR configuration (this is my full config for SmartIR everything works as expected except the new TestTV entity added trying to reproduce this bug)

media_player:
  - platform: smartir
    name: LivingTV
    unique_id: tv_living
    device_code: 9022
    device_class: tv #(tv, speaker, receiver)
    controller_data:
      controller_type: MQTT
      mqtt_topic: zigbee2mqtt/irLiving/set/ir_code_to_send
    delay: 1
    #power_sensor: binary_sensor.tv_living
    power_sensor: binary_sensor.always_on_power_sensor
    power_sensor_restore_state: true
    power_sensor_delay: 2

  - platform: smartir
    name: AudioLiving
    unique_id: audio_living
    device_code: 9023
    device_class: speaker #(tv, speaker, receiver)
    controller_data:
      controller_type: MQTT
      mqtt_topic: zigbee2mqtt/irLiving/set/ir_code_to_send
    delay: 1
    power_sensor: binary_sensor.always_on_power_sensor
    power_sensor_restore_state: true
    power_sensor_delay: 2

  - platform: smartir
    name: DecoLiving
    unique_id: deco_living
    device_code: 9500
    device_class: receiver #(tv, speaker, receiver)
    controller_data:
      controller_type: MQTT
      mqtt_topic: zigbee2mqtt/irLiving/set/ir_code_to_send
    delay: 1
    power_sensor: binary_sensor.always_on_power_sensor
    power_sensor_restore_state: true
    power_sensor_delay: 2

  - platform: smartir
    name: DecoDormitorio
    unique_id: deco_dormitorio
    device_code: 9500
    device_class: receiver #(tv, speaker, receiver)
    controller_data:
      controller_type: MQTT
      mqtt_topic: zigbee2mqtt/irDormitorio/set/ir_code_to_send
    delay: 1
    power_sensor: binary_sensor.always_on_power_sensor
    power_sensor_restore_state: true
    power_sensor_delay: 2

  - platform: smartir
    name: DormitorioTV
    unique_id: tv_dormitorio
    device_code: 9060
    device_class: tv #(tv, speaker, receiver)
    controller_data:
      controller_type: MQTT
      mqtt_topic: zigbee2mqtt/irDormitorio/set/ir_code_to_send
    delay: 1
    #power_sensor: binary_sensor.tv_living
    power_sensor: binary_sensor.always_on_power_sensor
    power_sensor_restore_state: true
    power_sensor_delay: 2

  - platform: smartir
    name: TestTV
    unique_id: tv_test
    device_code: 9500
    device_class: tv #(tv, speaker, receiver)
    controller_data:
      controller_type: MQTT
      mqtt_topic: zigbee2mqtt/irLiving/set/ir_code_to_send
    delay: 1
    power_sensor: binary_sensor.tv_test
    power_sensor_restore_state: true
    power_sensor_delay: 20

climate:
  - platform: smartir
    name: Living AC
    unique_id: living_ac
    device_code: 9060
    controller_data:
      controller_type: MQTT
      mqtt_topic: zigbee2mqtt/irLiving/set/ir_code_to_send
    temperature_sensor: sensor.0x842712fffe49d85b_temperature
    humidity_sensor: sensor.0x842712fffe49d85b_humidity
    #power_sensor: binary_sensor.ac_power

  - platform: smartir
    name: Dormitorio AC
    unique_id: dormitorio_ac
    device_code: 9060
    controller_data:
      controller_type: MQTT
      mqtt_topic: zigbee2mqtt/irDormitorio/set/ir_code_to_send
    temperature_sensor: sensor.0x881a14fffef49351_temperature
    humidity_sensor: sensor.0x881a14fffef49351_humidity
    #power_sensor: binary_sensor.ac_power

Describe the bug When adding a new media_player the entity state starts at UNKNOWN and does not change to OFF or ON even if the power_sensor is stating that value… I suspect that the UNKNOWN entity state is not being considered as posible state at the time it compares the current entity state with the posible new one..

Screen Shot 2024-09-03 at 15 23 13 Screen Shot 2024-09-03 at 15 23 49

The media_player entity shows as “grayed out” in the dashboard and wont let you interact with it

in the previous fork of smartIR, the media_player entity used to start with the state OFF , not the current UNKNOWN

to “fix” this I have changed the media_player entity state from UNKNOWN to OFF (developer tools >> STATE >> set state) in the TestTV media_player entity before the power_sensor_delay time and interacted with the media_player entity (dashboard) turning it on..

Screen Shot 2024-09-03 at 15 58 27

after that “fix” the UNKNOWN state is no longer displayed, even after reset and the media_player works as expected and lets you input commands

Screen Shot 2024-09-03 at 15 58 48

To Reproduce Steps to reproduce the behavior:

  1. Add Any media_player entity in Smartir
  2. Try to interact with the media_player

Debug log

2024-09-03 15:46:29.163 DEBUG (MainThread) [custom_components.smartir.climate] Setting up the SmartIR climate platform
2024-09-03 15:46:29.163 DEBUG (MainThread) [custom_components.smartir] Loading custom climate device JSON file '9060.json'.
2024-09-03 15:46:29.163 DEBUG (MainThread) [custom_components.smartir.climate] Setting up the SmartIR climate platform
2024-09-03 15:46:29.164 DEBUG (MainThread) [custom_components.smartir] Loading custom climate device JSON file '9060.json'.
2024-09-03 15:46:29.166 DEBUG (SyncWorker_5) [custom_components.smartir] Loading device JSON file '/config/custom_components/smartir/custom_codes/climate/9060.json'.
2024-09-03 15:46:29.166 DEBUG (SyncWorker_5) [custom_components.smartir] Loaded device JSON file '/config/custom_components/smartir/custom_codes/climate/9060.json'.
2024-09-03 15:46:29.170 DEBUG (MainThread) [custom_components.smartir.climate] SmartIRClimate init started for device Living AC supported models ['R09AWN', 'R24AWN', 'E09EK']
2024-09-03 15:46:29.170 DEBUG (MainThread) [custom_components.smartir.climate] convert temp '18' from '°C' to '°C' with '1' precision.
2024-09-03 15:46:29.170 DEBUG (MainThread) [custom_components.smartir.climate] convert temp '30' from '°C' to '°C' with '1' precision.
2024-09-03 15:46:29.171 DEBUG (MainThread) [custom_components.smartir.climate] async_added_to_hass <entity climate.living_ac=unknown> Living AC 393
2024-09-03 15:46:29.176 DEBUG (MainThread) [custom_components.smartir.climate] SmartIRClimate init started for device Dormitorio AC supported models ['R09AWN', 'R24AWN', 'E09EK']
2024-09-03 15:46:29.176 DEBUG (MainThread) [custom_components.smartir.climate] convert temp '18' from '°C' to '°C' with '1' precision.
2024-09-03 15:46:29.176 DEBUG (MainThread) [custom_components.smartir.climate] convert temp '30' from '°C' to '°C' with '1' precision.
2024-09-03 15:46:29.177 DEBUG (MainThread) [custom_components.smartir.climate] async_added_to_hass <entity climate.dormitorio_ac=unknown> Dormitorio AC 393
2024-09-03 15:46:29.182 DEBUG (MainThread) [custom_components.smartir.media_player] Setting up the SmartIR media player platform
2024-09-03 15:46:29.182 DEBUG (MainThread) [custom_components.smartir] Loading custom media_player device JSON file '9022.json'.
2024-09-03 15:46:29.183 DEBUG (MainThread) [custom_components.smartir.media_player] Setting up the SmartIR media player platform
2024-09-03 15:46:29.183 DEBUG (MainThread) [custom_components.smartir] Loading custom media_player device JSON file '9023.json'.
2024-09-03 15:46:29.183 DEBUG (MainThread) [custom_components.smartir.media_player] Setting up the SmartIR media player platform
2024-09-03 15:46:29.183 DEBUG (MainThread) [custom_components.smartir] Loading custom media_player device JSON file '9500.json'.
2024-09-03 15:46:29.183 DEBUG (SyncWorker_2) [custom_components.smartir] Loading device JSON file '/config/custom_components/smartir/custom_codes/media_player/9022.json'.
2024-09-03 15:46:29.184 DEBUG (SyncWorker_3) [custom_components.smartir] Loading device JSON file '/config/custom_components/smartir/custom_codes/media_player/9023.json'.
2024-09-03 15:46:29.184 DEBUG (SyncWorker_3) [custom_components.smartir] Loaded device JSON file '/config/custom_components/smartir/custom_codes/media_player/9023.json'.
2024-09-03 15:46:29.184 DEBUG (SyncWorker_2) [custom_components.smartir] Loaded device JSON file '/config/custom_components/smartir/custom_codes/media_player/9022.json'.
2024-09-03 15:46:29.185 DEBUG (SyncWorker_4) [custom_components.smartir] Loading device JSON file '/config/custom_components/smartir/custom_codes/media_player/9500.json'.
2024-09-03 15:46:29.185 DEBUG (SyncWorker_4) [custom_components.smartir] Loaded device JSON file '/config/custom_components/smartir/custom_codes/media_player/9500.json'.
2024-09-03 15:46:29.186 DEBUG (MainThread) [custom_components.smartir.media_player] Setting up the SmartIR media player platform
2024-09-03 15:46:29.186 DEBUG (MainThread) [custom_components.smartir] Loading custom media_player device JSON file '9500.json'.
2024-09-03 15:46:29.187 DEBUG (MainThread) [custom_components.smartir.media_player] Setting up the SmartIR media player platform
2024-09-03 15:46:29.187 DEBUG (SyncWorker_1) [custom_components.smartir] Loading device JSON file '/config/custom_components/smartir/custom_codes/media_player/9500.json'.
2024-09-03 15:46:29.187 DEBUG (MainThread) [custom_components.smartir] Loading custom media_player device JSON file '9060.json'.
2024-09-03 15:46:29.188 DEBUG (MainThread) [custom_components.smartir.media_player] Setting up the SmartIR media player platform
2024-09-03 15:46:29.188 DEBUG (SyncWorker_1) [custom_components.smartir] Loaded device JSON file '/config/custom_components/smartir/custom_codes/media_player/9500.json'.
2024-09-03 15:46:29.188 DEBUG (MainThread) [custom_components.smartir] Loading custom media_player device JSON file '9500.json'.
2024-09-03 15:46:29.191 DEBUG (SyncWorker_5) [custom_components.smartir] Loading device JSON file '/config/custom_components/smartir/custom_codes/media_player/9060.json'.
2024-09-03 15:46:29.194 DEBUG (SyncWorker_1) [custom_components.smartir] Loading device JSON file '/config/custom_components/smartir/custom_codes/media_player/9500.json'.
2024-09-03 15:46:29.194 DEBUG (SyncWorker_5) [custom_components.smartir] Loaded device JSON file '/config/custom_components/smartir/custom_codes/media_player/9060.json'.
2024-09-03 15:46:29.194 DEBUG (SyncWorker_1) [custom_components.smartir] Loaded device JSON file '/config/custom_components/smartir/custom_codes/media_player/9500.json'.

after the "makeshift" fix described before, this is the log when first interacting with the (now working) entity

2024-09-03 15:58:36.472 DEBUG (MainThread) [custom_components.smartir.media_player] Scheduled power sensor check for 'off' state.
2024-09-03 15:58:56.474 DEBUG (MainThread) [custom_components.smartir.media_player] Executing power sensor check for expected state 'off', current state 'None'.

sorry again if I sounded rude or demanding before.., willing to expand the information upon requirement

litinoveweedle commented 2 months ago

Thank you for your help. I am also sorry if I sounded angry, but the issue is, that half of the issues are being open without logs or any other required information. So there goes my frustration that I would like to help if possible but it is kind hard to do.

Anyway I think we are now getting somewhere. ;-)

These logs refer to the states of the power_sensor:

2024-09-03 15:58:36.472 DEBUG (MainThread) [custom_components.smartir.media_player] Scheduled power sensor check for 'off' state.
2024-09-03 15:58:56.474 DEBUG (MainThread) [custom_components.smartir.media_player] Executing power sensor check for expected state 'off', current state 'None'.

This is very interesting, as you previously reported that your power sensor reports always STATE_ON but as you can see, this it is logged, that the current reported state of your power_sensor is None. If you remember my original question I asked you, what is the state of your power_sensor. For some reason you power sensors in not reporting either STATE_ON or STATE_OFF, but value None. This is only possible when integration is unable to query state of the entity you configured as power_sensor. Please double check, that you power_sensor works correctly at all times.

Another question is - can you try to post logs without any change in the code - when you try to switch on, off and on the newly added media player device? I am asking that, because in line 345 of media_player.py state of the media player entity is ALWAYS set to the value commanded, and this is independent from the power_sensor check logic.

charliejgallo commented 2 months ago

for this last test i just added a power_sensor: binary_sensor.tv_test line to the new entity to make the test easy, did not use the Always ON sensor ..., in going to test again just to be sure

im also going to read all the code again to think about this, maybe the issue is that the line 345 in media_player is inside a function called async def _send_command(self, state, commands): .. the problem is that with the state being UNKNOWN you can't send commands at all to set that change in state...

maybe my makeshift solution works because changing the original state from UNKNOWN to OFF lets you call the "_send_command " part that later changes the state as needed...

i dont want to spam you and take all your time, going to try and log everything and report back with findings....

charliejgallo commented 2 months ago

been reading the media_player.py inside smartir and then looking in the HomeAssistant Github, there i find that in Media_Player/reproduce_state.py the states to show the service are listed differently than in the climate/reproduce_state.py...

in media_player/reproduce_state.py the posible states described to interact with the service are if STATE_OFF (Service states as OFF, interface shows only off button)

Screen Shot 2024-09-03 at 18 06 38 Screen Shot 2024-09-03 at 18 06 57

or if state.state in ( STATE_BUFFERING, STATE_IDLE, STATE_ON, STATE_PAUSED, STATE_PLAYING, ). (Service states as ON, interface shows other commands as well)

Screen Shot 2024-09-03 at 18 07 28 Screen Shot 2024-09-03 at 18 07 17

i don't see the possible state of UNKNOWN there, making the media_player to not work as neither SERVICE_TURN_ON or SERVICE_TURN_OFF are set and needed to send a command with the entity..

Screen Shot 2024-09-03 at 15 23 13

changing the initial state STATE_UNKNOWN from line 89 of media_player.py to STATE_OFF seems to do the trick

edit: now it kinda makes sense, with the original state as unknown the dashboard shows no posible command, and the line 345 state is never reached

edit2: Media_Player possible states from developers.home-assistant.io

litinoveweedle commented 1 month ago

Hello, I can confirm your observation. The root cause is combination of the two issues:

  1. media_player class seems to be unable to handle state 'UNKNOWNcorrectly, even if the entity is usingassumed_state`True.
  2. There is additional problem in the _async_power_sensor_changed logic conditions, where it doesn't count with the entity state UNKNOWN

For the:

  1. I will probably open the issue with HomeAssistant core, but this is long run. Therefore I will revert default state to OFF (as you proposed) as an interim solution
  2. I will improve logic condition to be compatible with current state UNKNOWN
litinoveweedle commented 1 month ago

Fix for the issue shall be provided in #141. I will release beta version containing the fix soon.

litinoveweedle commented 1 month ago

Fix shall be available in the latest beta, please test and report back. Thank you.

charliejgallo commented 1 month ago

THANK YOU!!! everything works as expected in 1.19.0b1

added a new media player with default settings, im able to interact with the entity with no problems at all at first start after settings!!

Screenshot 2024-09-27 at 7 11 32 PM Screenshot 2024-09-27 at 7 11 20 PM
litinoveweedle commented 1 month ago

@charliejgallo Hello, thank you very much for reporting and testing! I will close this issue now. In case of any problems please feel free to reopen it or create new one. Thank you once again.