Service filter / firewall feature, prevent bedroom light turn on during nighttime

Hello all,

Last night, I accidentally turned on the lights in the bedroom, which wasn’t a popular action. I knew this was bound to happen at some point, either manually through a UI or through some automation that I’ve made.

I think a helpful feature would be something that can filter or firewall service calls. This would allow me to set a filter up for light.turn_on on specific lights, and prevent those from passing if some condition holds (like nighttime). More advanced, it could be allowed to dim lights if they’re already on - but primarily I’d like to prevent all turning on of those lights through HA.

Turning the lights on through a switch would still work in my instance, as the switch physically controls the light. Other setups may also need to allow turning on the lights if the call comes from a specific switch.

I don’t know what to search for, if this already exists :confused:

Mike.

So how exactly were the lights ‘accidentally’ turned on? Did you use the UI or was it an automation?

In this case it was through the ui. I mistook the lights for a different light and turned it on.

It might as well have been an automation.

I asked ChatGPT, and it suggested that I use some configuration to rebind the service to a new service name, like “light.turn_on_original” and then implement light.turn_on with my filter in python.

I’m not even sure thats possible - never saw anything like that in docs. :slight_smile:

So what do you envision would be needed to prevent you from accidentally doing that again? The “filter service calls” suggestion in your original post isn’t possible so perhaps a Condional card could be used to hide all lights displayed in the UI that shouldn’t be accessible during the night.

Because it doesn’t exist.

Be advised that ChatGPT has a reputation for answering questions about Home Assistant with authoritative-sounding suggestions that are factually incorrect.

As for ChatGPT - imagined as much :slight_smile:

It being not possible makes it a good candidate for a feature request.

Hiding the entity in the ui does not prevent automations or other things. For example, if one were to make a scene that did something, it could inadvertently turn on the light that mustn’t turn on in this instance.

Is it possible to capture the service call in an event and manipulate it there?

Could a custom component overwrite the service call logic (they must all go through the same code before ending in their respective domains) and do something before handing off to the original service call logic?

So I did some digging and I did find the core point where service calls come through.

In this, it’s obvious that:

  • Events cannot manipulate the service call
  • There don’t seem to be any hooks or extension points that I can interact with

But, I did find that the service registry can register service calls - and it doesn’t test if they exist already. So in principle, if, when HA is ready, I made a bit of code that called register(.., 'light', 'turn_on', ..) which then wrapped whatever was already registered as a handler - it could manipulate / filter / do stuff before the current service call.

Part of my issue is that this seems possible, and it seems like something others would have needed. This could be for my use case, or others like security (“only X can call lock.unlock”) - but my search keywords come up empty. :confused:

It will if you ensure the automation implements the same logic that prevents the card from displaying.

The automation that turns on the scene must use the same logic as described above.

An event is posted when a service call is initiated but you can’t “capture” the service call.

If you have the requisite skills, yes. However, I believe you understand the implications (ongoing maintenance responsibilities).

Easier to just use the available features to suppress execution based on conditions.