How to send array to ESPHome service from python

I am trying to send a dynamic list of times to an esphome service eg an schedule. First I tried a value template (jinja) but hit the problem with the string being over 255 chars. So now I am using a python script. In the script I create a list which I want to send to the service call. But a list is not an array so Im abit stuck, as the usual way in python to create array is to use an import ’ numpy’ but because in HA python scripts run in a sandbox I cant use the import.
Any help either how to solve the python array issue or a different way of achieving the desired outcome would be much appreciated.

Thanks

# schedule_service.py
daySchedule = []
day_entity_id = data.get('day_entity_id')
wk_entity_id = data.get('wk_entity_id')
logger.info('send schedule called')
if day_entity_id is not None:
    state = hass.states.get(day_entity_id)
    days = state.attributes.get('weekdays') or 'none'
    times = state.attributes.get('timeslots') or 'none'
    actions = state.attributes.get('actions') or 'none'
    for day in days:
      action_cnt = 0
      for time in times:
        action = list(actions[action_cnt].values())[0:1][0]
        action = action.split('_')[1]
        time = time.split(' - ')[0]
        day = day.replace('sun', '1').replace( 'mon', '2').replace( 'tue', '3').replace( 'wed', '4',).replace('thu', '5').replace( 'fri', '6').replace( 'sat', '7')
        sched_item = day + ':' + time + ',' + action
        daySchedule.append(sched_item)
        action_cnt =  action_cnt+ 1
if wk_entity_id is not None:
    state = hass.states.get(wk_entity_id)
    days = state.attributes.get('weekdays') or 'none'
    times = state.attributes.get('timeslots') or 'none'
    actions = state.attributes.get('actions') or 'none'
    for day in days:
      action_cnt = 0
      for time in times:
        action = list(actions[action_cnt].values())[0:1][0]
        action = action.split('_')[1]
        time = time.split(' - ')[0]
        day = day.replace('sun', '1').replace( 'mon', '2').replace( 'tue', '3').replace( 'wed', '4',).replace('thu', '5').replace( 'fri', '6').replace( 'sat', '7')
        sched_item = day + ':' + time + ',' + action
        daySchedule.append(sched_item)
        action_cnt =  action_cnt+ 1
logger.info('Sending %s', daySchedule)
if daySchedule is not None:
    hass.services.call('esphome.boiler_controller_update_schedule',{daySchedule}, False)

For future reference for anybody else struggling with the lack of documentation for python scripts in HA the solution was as follows


if daySchedule is not None:
    hass.services.call('esphome', 'boiler_controller_update_schedule', {'schedule_string':daySchedule })

1 Like