Adding a service with handler

I have created a custom component to contorl my heating, and I would like to register services for switching on the boiler, changing thermostat setpoints, etc. My setup routines is:

def async_setup_platform(hass, config, async_add_devices, discovery_info=None):

    heatingView = HeatingView(async_add_devices)

    def handle_switch_on_boiler(call):
        heatingView.switch_on_boiler()
        hass.states.set('heatingcontrol.switch_on_boiler',None)

    hass.services.async_register(DOMAIN, 'switch_on_boiler', handle_switch_on_boiler)
    hass.http.register_view(heatingView )

    return True

However, within the handle_switch_on_boiler function, the heatingView object won’t be available when the switch_on_boiler service is called. So, what is the correct way to expose the heatingView object within the handler?

Just in case anyone it still need the answer, here’s a working example:

async def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
    alarm = BWAlarm(hass, config, mqtt)

    @callback
    def async_alarm_arm_home(service):
        alarm.async_alarm_arm_home(service.data.get(ATTR_CODE), service.data.get(ATTR_IGNORE_OPEN_SENSORS))

    hass.services.async_register(DOMAIN, SERVICE_ALARM_ARM_HOME, async_alarm_arm_home, EXTENDED_ALARM_SERVICE_SCHEMA)

So it looks like the initial setup should work just fine unless there are some hidden details…

1 Like

Thank you! this seriously helped me out. I believe your def async_alarm_arm_home should be async def async_alarm_arm_home though.