Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warm-up and cool-down delays in media players #118

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions custom_components/smartir/media_player.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import asyncio
import logging

import time
import voluptuous as vol

from homeassistant.components.media_player import MediaPlayerEntity, PLATFORM_SCHEMA
Expand Down Expand Up @@ -93,6 +93,7 @@ def __init__(self, hass, config, device_data):
self._support_flags = 0
self._power_sensor_check_expect = None
self._power_sensor_check_cancel = None
self._ready_at = None

self._manufacturer = device_data["manufacturer"]
self._supported_models = device_data["supportedModels"]
Expand Down Expand Up @@ -157,6 +158,10 @@ def __init__(self, hass, config, device_data):
for key in self._commands["sources"]:
self._sources_list.append(key)

# Initialization times
self._warm_up_delay = device_data['warmUpDelay'] if 'warmUpDelay' in device_data else None
self._cool_down_delay = device_data['coolDownDelay'] if 'coolDownDelay' in device_data else None

# Init exclusive lock for sending IR commands
self._temp_lock = asyncio.Lock()

Expand Down Expand Up @@ -253,7 +258,7 @@ async def async_turn_off(self):
await self._send_command(STATE_OFF, [])

async def async_turn_on(self):
"""Turn the media player off."""
"""Turn the media player on."""
await self._send_command(STATE_ON, [])

async def async_media_previous_track(self):
Expand Down Expand Up @@ -299,6 +304,8 @@ async def async_play_media(self, media_type, media_id, **kwargs):
async def _send_command(self, state, commands):
async with self._temp_lock:

await self.wait_until_ready()

if self._power_sensor and self._state != state:
self._async_power_sensor_check_schedule(state)

Expand All @@ -309,12 +316,17 @@ async def _send_command(self, state, commands):
_LOGGER.error("Missing device IR code for 'on' command.")
else:
await self._controller.send(self._commands["on"])
self._ready_at = time.monotonic() + max(self._warm_up_delay or 0, float(self._delay))

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)
self._ready_at = time.monotonic() + max(self._cool_down_delay or 0, float(self._delay))

if commands:
await self.wait_until_ready()

for keys in commands:
data = self._commands
Expand Down Expand Up @@ -372,6 +384,13 @@ async def _async_power_sensor_changed(
# self._source = None
self.async_write_ha_state()

async def wait_until_ready(self):
if self._ready_at:
time_to_wait = self._ready_at - time.monotonic()
if time_to_wait > 0:
await asyncio.sleep(time_to_wait)
self._ready_at = None

@callback
def _async_power_sensor_check_schedule(self, state):
if self._power_sensor_check_cancel:
Expand Down