Hi everyone, I have a python script that uses “time.sleep ()” but I would like to use “asyncio.sleep ()”, can you do it?
P.S. sorry, this is an online translation
Hi everyone, I have a python script that uses “time.sleep ()” but I would like to use “asyncio.sleep ()”, can you do it?
P.S. sorry, this is an online translation
This is the script
light_id = data.get('light_id') # entità light da comandare
sensor_id = data.get('riv_id') # entità sensor dal quale leggere la luminosità
lux_level = float(data.get('lux_level')) # livello di luminosità desiderato
light_id_state = hass.states.get(light_id)
sensor_id_state = hass.states.get(riv_id)
if sensor_id_state.state != 'unknown':
lux = float(sensor_id_state.state)
else:
lux = lux_level
brightness = light_id_state.attributes.get('brightness') or 0
step_high = 10
step_mid = 5
step_low = 1
def service(entity, brightness, sensor):
service_data = {"entity_id": entity, "brightness": brightness}
entity_state = hass.states.get(entity).state
if entity_state == 'on':
hass.services.call('light', 'turn_on', service_data)
time.sleep(1)
if sensor_state.state != 'unknown':
lux = float(sensor_state.state)
else:
lux = lux_level
entity_state = hass.states.get(entity).state
return lux, entity_state
if light_id_state.state == 'on':
if lux < lux_level:
while lux < lux_level:
if 0 < lux_level - lux < 5:
brightness = brightness + step_low
elif 5 < (lux_level - lux) < 20:
brightness = brightness + step_mid
else:
brightness = brightness + step_high
if brightness > 255:
break
lux, state = service(light_id, brightness, sensor_id)
if state == 'off':
break
elif lux > lux_level:
while lux > lux_level:
if 0 < lux - lux_level < 5:
brightness = brightness - step_low
elif 5 < (lux - lux_level) < 20:
brightness = brightness - step_mid
else:
brightness = brightness - step_high
if brightness < 0:
break
lux, state = service(light_id, brightness, sensor_id)
if state == 'off':
break