Light fader with RGB transitions

Based on/inspired by Light Fader by Transition Time by @mrsnyds I’ve tried to make a python script for fading an RGB light including colour transition. It works fairly well in that the colour fade works, but it stops whenever it reaches around 50%, or 127 brightness. This is my first time using Python and first script of some complexity in Home Assistant, and I’m stumped as to why it stops. I’d appreciate any feedback!

# INPUT
entity_id = data.get ('entity_id')
duration = data.get ('duration')
b_start = int (data.get ('brightness_start', -1))
b_end = int (data.get ('brightness_end', -1))
x_start = round(float (data.get ('x_start', -1)),6)
y_start = round(float (data.get ('y_start', -1)),6)
x_end = round(float (data.get ('x_end', -1)),6)
y_end = round(float (data.get ('y_end', -1)),6)

#logger.info ("Debug:\nentity_id: " + entity_id + "\nduration: " + str(duration) + "\nb_start: " + str(b_start) + "\nb_end: " + str(b_end) + "\nx_start: " + str(x_start) + "\nx_end: " + str(x_end) + "\ny_start: " + str(y_start) + "\ny_end: " + str(y_end))

states = hass.states.get (entity_id)  
b_cur = states.attributes.get ('brightness') or 0
xy_cur = list(states.attributes.get ('xy_color')) or [0.0,0.0]
x_cur = round(xy_cur[0],6) or 0.0
y_cur = round(xy_cur[1],6) or 0.0
duration = int (duration[:2]) * 3600 + int (duration[3:5]) * 60 + int (duration[-2:])
b_increment = float ((b_end - b_start) / duration )
x_increment = round(float ((x_end - x_start) / duration ),6)
y_increment = round(float ((y_end - y_start) / duration ),6)

#logger.info ("\nDebug:\nb_cur: " + str(b_cur) + "\nxy_cur: " + str(xy_cur) + "\nx_cur: " + str(x_cur) + "\ny_cur: " + str(y_cur) + "\nduration: " + str(duration) + "\nb_increment: " + str(b_increment) + "\nx_increment: " + str(x_increment) + "\ny_increment: " + str(y_increment))

sleep_delay = abs(duration/(b_end - b_start))

b_new = b_start
x_new = x_start
y_new = y_start
xy_new = xy_start = [x_start,y_start]

#logger.info ("\nDebug:\nsleep_delay: " + str(sleep_delay) + "\nb_new: " + str(b_new) + "\nxy_new: " + str(xy_new) + "\nx_new: " + str(x_new) + "\ny_new: " + str(y_new) )

data = { "entity_id" : entity_id, "brightness" : b_start, "xy_color" : xy_start }

#logger.info ("\nDebug:\ndata: " + str(data))

hass.services.call('light', 'turn_on', data)

#logger.info ("\nDebug:\nstates:" + str(hass.states.get(entity_id)))

time.sleep(1)  # without delay,'hass.states.get' would not get the new value

while round(b_cur) != round(b_end) : #until we get to end levels

    states = hass.states.get(entity_id)           ##  acquire status of entity
    b_cur = states.attributes.get('brightness') or 0
    #logger.info("\nDebug:\nstates: " + str(states) + "\nb_cur: " + str(b_cur))
    xy_cur = list(states.attributes.get ('xy_color')) or [0.0,0.0]
    x_cur = round(xy_cur[0],6)
    y_cur = round(xy_cur[1],6)
    #logger.info ("\nDebug:\nb_cur: " + str(b_cur) + "\nb_new: " + str(round(b_new)) + "\nxy_cur: " + str(xy_cur) + "\nxy_new: " + str(xy_new) + "\nx_cur: " + str(x_cur) + "\nx_new: " + str(x_new) + "\ny_cur: " + str(y_cur) + "\ny_new: " + str(y_new) )

    if round(b_cur) != round(b_new) :

        logger.info('Exiting sun sim')
        break;

    else :
        b_new = b_new + b_increment
        x_new = round(x_new + x_increment,6)
        y_new = round(y_new + y_increment,6)
        xy_new = [x_new,y_new]

        logger.info('Setting brightness of ' + str(entity_id) + ' from ' + str(b_cur) + ' to ' + str(round(b_new)) + ' and xy_color from ' + str(xy_cur) + ' to ' +  str(xy_new))

        data = {
            "entity_id" : entity_id,
            "brightness" : round(b_new),
            "xy_color" : xy_new
        }

        hass.services.call('light', 'turn_on', data)
        time.sleep(sleep_delay)