Very cool! I’m commenting so I can find this later, I work in C# by day
I think I’ve just found a problem with my implementation of the generic class with relation to your attributes. Stay tuned, but it doesn’t look like there’s a good solution using attributes.
Ok so this won’t work but i could make it work. This is just so i can explain it better.
What you want is something like this:
public abstract class FanControl<TFan> where TFan : Switch
{
[Automation]
public async Task ControlFan(TFan fan)
{
// Do stuff
}
}
You would then have to create classes that inherit from said classes like:
public class LivingRoomFanControl : FanControl<Switches.LivingRoomFan> { }
You never have to instantiate automation classes, they are discovered and instantiated by the framework.
Right now this wont work because the automation methods are determined at compile time by a source generator. Since we only have access to the source code it is hard to support inheritance. I would have to move the automation method discovery to the application startup.
Yep that’s what I discovered as well.
It appears I’m having some trouble with the add-on periodically. I haven’t nailed down the cause, but here are the symptoms.
Initially, when I goto the HASS C24HA web page, I receive a message at the bottom of the screen indicating something has gone wrong with a link RELOAD to click. Upon click the link, the spinner comes up and a connection is never made (it spins indefinitely until timing out with the reload link again).
Additionally, I cannot reach the UI of HA in any other capacity in a refresh or a new browser window, it’ll give the Loading Data screen. However if I have happened to have the HA UI open, I can still navigate. Stopping and starting the add-on doesn’t resolve the issue. A full reboot does.
Eventually the spinner times out with the reload option, which works at that point.
I suspect this occurs when I debug new code locally. Having killed the local iteration on which I’m debugging, and navigating to go Build and Deploy is where I usually see this occur.
Please check the add on logs if this ever happens again (Supervisor => C# => Log
). As you said, when the spinner is there forever, the internal instance of the automation application has probably crashed. I should probably work on making this more reliable. Normally restarting the addon fixes issues like this.
In the meantime i’ve create a new preview version that implements the support for inheritance. You can update to the prerelease nuget packages ( 0.7.0-dev ) if you want to. Cases like described above should now be possible.
at Microsoft.Extensions.Hosting.Internal.Host.StopAsync(CancellationToken cancellationToken) in Microsoft.Extensions.Hosting.dll:token 0x6000042+0x124
--- End of inner exception stack trace ---
dbug: hhnl.HomeAssistantNet.CSharpForHomeAssistant.Notifications.HubConnectionAddedNotification.ProcessInfoHandler[0]
Getting new automations.
dbug: hhnl.HomeAssistantNet.CSharpForHomeAssistant.Services.AutomationsService[0]
Getting automation list.
dbug: hhnl.HomeAssistantNet.CSharpForHomeAssistant.Notifications.HubConnectionAddedNotification.ProcessInfoHandler[0]
Got 5 automations.
dbug: hhnl.HomeAssistantNet.CSharpForHomeAssistant.Notifications.NoConnectionNotification.Handler[0]
No client connections. Starting new instance.
info: hhnl.HomeAssistantNet.CSharpForHomeAssistant.Services.BuildService[0]
Starting deployed application: '/config/c-sharp-for-homeassistant/deploy/CS4HA.dll'
dbug: Microsoft.Extensions.Hosting.Internal.Host[1]
Hosting starting
info: hhnl.HomeAssistantNet.Automations.Supervisor.SupervisorClient[0]
Setup supervisor client Url 'http://localhost:20777' Token 'e24ed2d1c6...'
info: hhnl.HomeAssistantNet.Automations.HomeAssistantConnection.HomeAssistantClient[0]
Starting home assistant client. Url 'ws://supervisor:80/core/api/websocket' Token 'e24ed2d1c6...'
info: hhnl.HomeAssistantNet.Automations.HomeAssistantConnection.HomeAssistantClient[0]
Connected to home assistant websocket api.
dbug: hhnl.HomeAssistantNet.Automations.HomeAssistantConnection.HomeAssistantClient[0]
Got auth_required; sending token.
dbug: hhnl.HomeAssistantNet.Automations.HomeAssistantConnection.HomeAssistantClient[0]
Got auth_ok; Init complete.
info: hhnl.HomeAssistantNet.Automations.HomeAssistantConnection.EntityRegistry[0]
Fetching entity states.
info: hhnl.HomeAssistantNet.Automations.HomeAssistantConnection.EntityRegistry[0]
Received 2045 entities.
dbug: hhnl.HomeAssistantNet.CSharpForHomeAssistant.Notifications.HubConnectionAddedNotification.ProcessInfoHandler[0]
Got new connection UsxWfutE7g8wEMbk86XBPA.
dbug: hhnl.HomeAssistantNet.CSharpForHomeAssistant.Notifications.HubConnectionAddedNotification.ProcessInfoHandler[0]
Connection UsxWfutE7g8wEMbk86XBPA is now the default connection.
dbug: hhnl.HomeAssistantNet.CSharpForHomeAssistant.Notifications.HubConnectionAddedNotification.ProcessInfoHandler[0]
Stopping previous connection hhnl.HomeAssistantNet.Shared.Supervisor.ConnectionInfo.
info: hhnl.HomeAssistantNet.Automations.Supervisor.SupervisorClient[0]
Supervisor client started
dbug: hhnl.HomeAssistantNet.Automations.Automation.AutomationService[0]
Enqueueing automation run 'CS4HA.BathFans.MasterToiletFan'. Reason: 'RunOnStart'
dbug: hhnl.HomeAssistantNet.Automations.Automation.AutomationService[0]
Enqueueing automation run 'CS4HA.BathFans.HalfBathFan'. Reason: 'RunOnStart'
dbug: hhnl.HomeAssistantNet.Automations.Automation.AutomationService[0]
Enqueueing automation run 'CS4HA.BathFans.LaundryBathFan'. Reason: 'RunOnStart'
dbug: hhnl.HomeAssistantNet.Automations.Automation.AutomationService[0]
Enqueueing automation run 'CS4HA.SimpleLights.PantryLight'. Reason: 'RunOnStart'
dbug: hhnl.HomeAssistantNet.Automations.Automation.AutomationService[0]
Enqueueing automation run 'CS4HA.SimpleLights.AtriumClosetLight'. Reason: 'RunOnStart'
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
Content root path: /config/c-sharp-for-homeassistant/deploy
dbug: hhnl.HomeAssistantNet.Automations.Automation.Runner.AutomationRunner[0]
Starting automation run 'b511341a-7e2b-4471-8e93-2fcaf80766c2' at: 07/08/2021 11:09:49 -05:00 Reasons: 'RunOnStart' ChangedEntity: ''
dbug: Microsoft.Extensions.Hosting.Internal.Host[2]
Hosting started
dbug: hhnl.HomeAssistantNet.Automations.Automation.Runner.AutomationRunner[0]
Starting automation run 'caaa4773-847b-4d8e-b9c4-d7a616c33b50' at: 07/08/2021 11:09:49 -05:00 Reasons: 'RunOnStart' ChangedEntity: ''
info: CS4HA.BathFans[0]
Master Toilet Fan Switch is on
info: CS4HA.SimpleLights[0]
Atrium Closet Door is off - Atrium Closet Light Switch is off
info: CS4HA.SimpleLights[0]
Turning off Atrium Closet Light Switch
dbug: hhnl.HomeAssistantNet.Automations.Automation.Runner.AutomationRunner[0]
Starting automation run 'c82e1bf4-b25e-4e31-8cd3-5447956a883b' at: 07/08/2021 11:09:49 -05:00 Reasons: 'RunOnStart' ChangedEntity: ''
info: CS4HA.BathFans[0]
Half Bath Fan Switch is off
dbug: hhnl.HomeAssistantNet.Automations.Automation.Runner.AutomationRunner[0]
Starting automation run '9c397b90-d86d-42d3-938d-617163d9ae70' at: 07/08/2021 11:09:49 -05:00 Reasons: 'RunOnStart' ChangedEntity: ''
dbug: hhnl.HomeAssistantNet.Automations.Automation.Runner.AutomationRunner[0]
Starting automation run 'c5604003-a9f5-4e6c-a0f4-4de163a9bbf8' at: 07/08/2021 11:09:49 -05:00 Reasons: 'RunOnStart' ChangedEntity: ''
info: CS4HA.BathFans[0]
Laundry Fan Switch is off
info: CS4HA.SimpleLights[0]
Kitchen Pantry Door: Access Control - Window/door is open is off - Kitchen Pantry Light Switch is off
info: CS4HA.SimpleLights[0]
Turning off Kitchen Pantry Light Switch
dbug: hhnl.HomeAssistantNet.CSharpForHomeAssistant.Notifications.HubConnectionAddedNotification.ProcessInfoHandler[0]
Getting new automations.
dbug: hhnl.HomeAssistantNet.CSharpForHomeAssistant.Services.AutomationsService[0]
Getting automation list.
dbug: hhnl.HomeAssistantNet.CSharpForHomeAssistant.Notifications.HubConnectionAddedNotification.ProcessInfoHandler[0]
Got 5 automations.
dbug: hhnl.HomeAssistantNet.Automations.Automation.AutomationService[0]
Enqueueing automation run 'CS4HA.SimpleLights.PantryLight'. Reason: 'EntityChanged'
dbug: hhnl.HomeAssistantNet.Automations.Automation.Runner.AutomationRunner[0]
Starting automation run 'ac4e441a-6de5-40a5-9cbe-3281abf5e3c5' at: 07/08/2021 11:18:56 -05:00 Reasons: 'EntityChanged' ChangedEntity: 'binary_sensor.kitchen_pantry_door'
info: CS4HA.SimpleLights[0]
Kitchen Pantry Door is off - Kitchen Pantry Light Switch is off
info: CS4HA.SimpleLights[0]
Turning off Kitchen Pantry Light Switch
info: CS4HA.BathFans[0]
Turning Master Toilet Fan Switch Off
dbug: hhnl.HomeAssistantNet.Automations.Automation.AutomationService[0]
Enqueueing automation run 'CS4HA.BathFans.MasterToiletFan'. Reason: 'EntityChanged'
dbug: hhnl.HomeAssistantNet.Automations.Automation.Runner.AutomationRunner[0]
Starting automation run '0d6782a8-f0e6-47cd-8210-c3e45b8b9159' at: 07/08/2021 11:29:49 -05:00 Reasons: 'EntityChanged' ChangedEntity: 'switch.master_toilet_fan_switch'
info: CS4HA.BathFans[0]
Master Toilet Fan Switch is off
It just happened again without me doing any debugging, so that’s not the cause. During the event, the add-on is not pegging the processor or anything, however on this occasion it caused one of my browser sessions to HA to be disconnected.
It looks like the automation instance is running. Might be that there is some kind of deadlock preventing communication between the automation instance and the browser.
At least you automations will continue to run.
It’s happening every time I return to the screen from it being idle for a few minutes. If it didn’t freeze up everything else on HA, I’d agree with you. Unfortunately I’m remote and cannot validate that automations are working during these events. Will validate that tonight.
Maybe one automation runs very often and bombards home assistant with requests. I could show you how to enable file logging so we have persistent log we could look into.
I’d love to know how to turn on the logging.
Point of note, it only seems to happen if I leave the integration screen up and return to it later. Opening it from the menu doesn’t seem to cause it. I’ll try to get some logging to you when I am able.
If that happens again, try opening the Dev Tools by pressing F12 and look for errors.
good idea, will do
The documentation says we can have multiple schedule attributes
…but I’m getting a compile time error.
You are completely right. I’ve pushed new nuget packages (0.6.5 and 0.7.1-dev) that should fix that.
For others less familiar, you’ll need to check the prelease option in NuGet to see these particular updates.
Works fine now. Thanks Andre
Realizing I should be posting my issues over at github, so will do so from here forward to keep this forum uncluttered.
I’ve got a use case not sure if exists a way to handle or if I make a feature request with this one, so let me scenario it out.
I’m thinking in terms of schedules and the workdaysensor.
If you’re not familiar with the workdaysensor:
I’d like to see the ability to constrain the schedule by binary or boolean, rather than having to do a check in the method itself.
Example:
[Schedule(BinarySensors.WorkdaySensor | WeekDay.Monday | WeekDay.Tuesday | WeekDay.Wednesday | WeekDay.Thursday | WeekDay.Friday, 22)]
[Schedule(WeekDay.Saturday | WeekDay.Sunday, 22, 30)]
In the above attribute scenario the first schedule would only fire if the workdaysensor is on. I could see input_booleans being useful in this way too. Having never wrote attributes I’m not sure how complex this ask is or if there is already a better way to go about it.
Thoughts?
How about:
[Automation]
[Schedule(WeekDay.Monday | WeekDay.Tuesday | WeekDay.Wednesday | WeekDay.Thursday | WeekDay.Friday, 22)]
[Schedule(WeekDay.Saturday | WeekDay.Sunday, 22, 30)]
public void MyAutomation([NoTrack]Sensors.Workday workday)
{
// Skip automation if today is not a workday
if (workday.IsOff)
return;
// Do automation stuff ...
}
This is similar to the example in the integration: Workday - Home Assistant