Skip to content

Commit

Permalink
Merge pull request #178 from litinoveweedle/fix_state_logic
Browse files Browse the repository at this point in the history
fix media_player state logic
  • Loading branch information
litinoveweedle authored Jan 4, 2025
2 parents bc1947f + 9644916 commit 307d2ee
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 39 deletions.
12 changes: 5 additions & 7 deletions custom_components/smartir/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -594,12 +594,9 @@ async def _send_command(
)
return
else:
commands = self._commands
if not isinstance(commands, dict):
_LOGGER.error("No device IR codes are defined.")
return

if "on" in commands.keys() and isinstance(commands["on"], str):
if "on" in self._commands.keys() and isinstance(
self._commands["on"], str
):
if (
"off" in self._commands.keys()
and isinstance(self._commands["off"], str)
Expand All @@ -615,9 +612,10 @@ async def _send_command(
else:
# if on code is not present, the on bit can be still set later in the all operation/fan codes"""
_LOGGER.debug("Found 'on' operation mode command.")
await self._controller.send(commands["on"])
await self._controller.send(self._commands["on"])
await asyncio.sleep(self._delay)

commands = self._commands
if hvac_mode in commands.keys():
commands = commands[hvac_mode]
_LOGGER.debug(
Expand Down
90 changes: 58 additions & 32 deletions custom_components/smartir/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,46 +304,72 @@ async def _send_command(self, state, commands):
self._async_power_sensor_check_schedule(state)

try:
if self._state != state:
if state == STATE_ON:
if "on" not in self._commands.keys():
_LOGGER.error("Missing device IR code for 'on' command.")
else:
await self._controller.send(self._commands["on"])
elif state == STATE_OFF:
if "off" not in self._commands.keys():
_LOGGER.error("Missing device IR code for 'off' command.")
else:
await self._controller.send(self._commands["off"])
await asyncio.sleep(self._delay)

for keys in commands:
data = self._commands
for idx in range(len(keys)):
if not (isinstance(data, dict) and keys[idx] in data):
_LOGGER.error(
"Missing device IR code for '%s' command.", keys[idx]
)
return
elif idx + 1 == len(keys):
if not isinstance(data[keys[idx]], str):
if state == STATE_OFF:
if (
"on" in self._commands.keys()
and isinstance(self._commands["on"], str)
and self._commands["on"] == self._commands["off"]
and self._state == STATE_OFF
):
# prevent to resend 'off' command if same as 'on' and device is already off
_LOGGER.debug(
"As 'on' and 'off' commands are identical and device is already in requested '%s' state, skipping sending '%s' command",
self._state,
"off",
)
else:
_LOGGER.debug("Found 'off' operation mode command.")
await self._controller.send(self._commands["off"])
await asyncio.sleep(self._delay)
else:
if (
"off" in self._commands.keys()
and isinstance(self._commands["off"], str)
and self._commands["off"] == self._commands["on"]
and self._state == STATE_ON
):
# prevent to resend 'on' command if same as 'off' and device is already on
_LOGGER.debug(
"As 'on' and 'off' commands are identical and device is already in requested '%s' state, skipping sending '%s' command",
self._state,
"on",
)
else:
# if on code is not present, the on bit can be still set later in the all operation/fan codes"""
_LOGGER.debug("Found 'on' operation mode command.")
await self._controller.send(self._commands["on"])
await asyncio.sleep(self._delay)

for keys in commands:
data = self._commands
for idx in range(len(keys)):
if not (isinstance(data, dict) and keys[idx] in data):
_LOGGER.error(
"Missing device IR code for '%s' command.",
keys[idx],
)
return
elif idx + 1 == len(keys):
if not isinstance(data[keys[idx]], str):
_LOGGER.error(
"Missing device IR code for '%s' command.",
keys[idx],
)
return
else:
await self._controller.send(data[keys[idx]])
await asyncio.sleep(self._delay)
elif isinstance(data[keys[idx]], dict):
data = data[keys[idx]]
else:
await self._controller.send(data[keys[idx]])
await asyncio.sleep(self._delay)
elif isinstance(data[keys[idx]], dict):
data = data[keys[idx]]
else:
_LOGGER.error(
"Missing device IR code for '%s' command.", keys[idx]
)
return
_LOGGER.error(
"Missing device IR code for '%s' command.",
keys[idx],
)
return

self._state = state
self._on_by_remote = False
self.async_write_ha_state()

except Exception as e:
Expand Down

0 comments on commit 307d2ee

Please sign in to comment.