I have a custom integration im trying to create services for but it’s been difficult finding updated docs that follow the current best practices. I have one sensor and two services I’d like to register that save presistent data somewhere. This is what I have so far:
async def async_setup(hass: HomeAssistant, config: ConfigType):
store = Store(hass, version=1, key=DOMAIN)
await store.async_load()
async def ignore_tracking_number(call: ServiceCall) -> None:
_LOGGER.debug('Received ignore_tracking_number', call.data)
tracking_number = call.data['tracking_number']
ignored_tracking_numbers = store.get('ignored_tracking_numbers', [])
if tracking_number not in ignored_tracking_numbers:
ignored_tracking_numbers.append(tracking_number)
await store.async_save({"ignored_tracking_numbers": ignored_tracking_numbers})
_LOGGER.debug(f'Updated ignored_tracking_numbers: {ignored_tracking_numbers}')
else:
_LOGGER.debug('Tracking number is already ignored')
return {'ignored_tracking_numbers': ignored_tracking_numbers}
hass.services.async_register(DOMAIN, 'ignore_tracking_number', ignore_tracking_number)
async def unignore_tracking_number(call: ServiceCall) -> None:
_LOGGER.debug('Received unignore_tracking_number', call.data)
ignored_tracking_numbers = store.get("ignored_tracking_numbers", [])
tracking_number = call.data["tracking_number"]
if tracking_number in ignored_tracking_numbers:
ignored_tracking_numbers.remove(tracking_number)
await store.async_save({"ignored_tracking_numbers": ignored_tracking_numbers})
_LOGGER.debug(f"Updated ignored_tracking_numbers: {ignored_tracking_numbers}")
else:
_LOGGER.debug(f"Tracking number {tracking_number} is not currently ignored")
return {"ignored_tracking_numbers": ignored_tracking_numbers}
hass.services.async_register(DOMAIN, 'unignore_tracking_number', unignore_tracking_number)
async def async_setup_platform(
hass: HomeAssistant,
config: ConfigType,
add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None
):
hass.data[DOMAIN] = {}
add_entities([TrackingNumberEntity(config, hass)], True)
The entity is made but the services dont register. I also have a schema for the services but have no idea where to put this
SERVICE_SCHEMA = vol.Schema({
vol.Required("tracking_number"): cv.string,
vol.Required("entity_id"): cv.string,
})