Skip to content

Commit

Permalink
Update docs to inject generated types as default (#242)
Browse files Browse the repository at this point in the history
* update docs to inject generated types

* fix typo
  • Loading branch information
FrankBakkerNl authored Oct 18, 2024
1 parent 078f5b9 commit 630bcfa
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 30 deletions.
44 changes: 42 additions & 2 deletions docs/user/hass_model/hass_model_generated_entities.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,49 @@ id: hass_model_generated_entities
title: Using generated entities
---

After generating code from Home Assistant with `nd-codegen` you can use a strongly typed API to work with entities in Home Assistant. The entities can be accessed like this:
After generating code from Home Assistant with `nd-codegen` you can use a strongly typed API to work with entities in Home Assistant. The Entities can be injected into your app like this:


```csharp
[NetDaemonApp]
class MyApp
{
public MyApp(Entities entities)
{
LightEntity livingRoomLight = entities.Light.LivingRoomLight;
// ..
}
}
```


`Entities` is a generated class that provides access to all your entities in Home Assistant. The object returned by `Entities.Light.LivingRoomLight` is of type `LightEntity` which is also generated. This specific instance represents the `light.living_room_light` entity in Home Assistant.


It is also possible to directly inject eg the LightEntities class


```csharp
[NetDaemonApp]
class MyApp
{
public MyApp(LightEntities lightEntities)
{
LightEntity livingRoomLight = lightEntities.LivingRoomLight;
// ..
}
}
```


:::note

To inject the generated types, make sure you call `AddHomeAssistantGenerated()` in your startup code.

:::


Alternatively you can create an instance of the generated `Entities` class via its constructor that takes the IHaContext.
```csharp
_myEntities = new Entities(haContext);

Expand All @@ -13,7 +54,6 @@ _myEntities = new Entities(haContext);
LightEntity livingRoomLight = _myEntities.Light.LivingRoomLight;
```

`Entities` is a generated class that provides access to all your entities in Home Assistant. The object returned by `_myEntities.Light.LivingRoomLight` is of type `LightEntity` which is also generated. This specific instance represents the `light.living_room_light` entity in Home Assistant.

The LightEntity class provides strongly typed access to the state and attributes of the entity like:

Expand Down
65 changes: 45 additions & 20 deletions docs/user/hass_model/hass_model_generated_service.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,31 @@ After generating code from Home Assistant with `nd-codegen` you can use a strong

## Call services via typed entity classes

The code generator generates a class for each domain that has entities in Home Assistant. For each service in Home Assistant that accepts an entity of that domain there is also a generated extension method for that `Entity` class. This allows services to be called like this:
The code generator generates a class for each domain that has entities in Home Assistant. For each service in Home Assistant that accepts an entity of that domain as a target, there is also a generated extension method for that `Entity` class. This allows services to be called like this:

```csharp
_myEntities = new Entities(haContext);

// ...
_myEntities.Light.LivingRoomLight.TurnOn(brightness: 100, colorTemp: 80);
[NetDaemonApp]
class MyApp
{
public MyApp(Entities entities)
{
entities.Light.LivingRoomLight.TurnOn(brightness: 100, colorTemp: 80);

_myEntities.Climate.AtticHeater.SetHvacMode(hvacMode:"off")
entities.Climate.AtticHeater.SetHvacMode(hvacMode:"off")
}
}

```

:::note

To inject the generated types, make sure you call `AddHomeAssistantGenerated()` in your startup code.

:::



The service methods have parameters that corresponds to the fields that are required by the service. Optional fields are provided as optional parameters so they can be skipped by using named parameters for the ones that are used.

## Using the generated Services class
Expand All @@ -29,28 +41,41 @@ The generated code also contains a class `Services`. This class provides access
Example:

```csharp
_services = new Services(haContext);

// ...
_services.PersistentNotification.Create(
message: "NetDaemon Application started",
title: "Awesome!");

_services.Climate.SetTemperature(
ServiceTarget.FromEntity("climate.bathroom"),
temperature: 20.5);
[NetDaemonApp]
class MyApp
{
public MyApp(Services services)
{
services.PersistentNotification.Create(
message: "NetDaemon Application started",
title: "Awesome!");

_services.Light.TurnOn(ServiceTarget.FromEntities("light.livingroom_light", "light.diner"),
brightness: 100,
colorTemp: 80);
services.Climate.SetTemperature(
ServiceTarget.FromEntity("climate.bathroom"),
temperature: 20.5);

services.Light.TurnOn(ServiceTarget.FromEntities("light.livingroom_light", "light.diner"),
brightness: 100,
colorTemp: 80);
}
}
```

Just as with the extension methods on the `Entity` classes, these methods have parameters that correspond to the fields of the service in Home Assistant.

If the service requires a target, the generated method will also contain a parameter of type 'ServiceTarget' that can be used to pass one or more entities, areas or devices.


Alternatively you can create an instance of the generated `Services` class via its constructor that takes the IHaContext.
```csharp
var services = new Services(haContext);

services.PersistentNotification.Create(
message: "NetDaemon Application started",
title: "Awesome!");
```


## Use the generated services with return values

Some Home Assistant services can return values. The generated services have a async method that returns a `JsonElement` that you can parse or serialize
Expand Down
11 changes: 4 additions & 7 deletions docs/user/hass_model/hass_model_working_with_entities.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,20 @@ The `Entity` class provides access to the entity's current state, attributes, an

## Accessing Entities

The basic way to create an instance of an `Entity` is by calling its constructor:
The basic way to create an instance of an `Entity` is by by using the `Entity()` method on `IHaContext`:

```csharp
var atticLight = new Entity(haContext, "light.attic");
var atticLight = haContext.Entity("light.attic");
```

The `Entity` class also has a public constructor that takes a `IHaContext` and the entityId. Using the `Entity()` method on `IHaContext` however, will create an instance of a specific generated Entity Type based on the provided entityId if available.

:::note

This does not create a new entity in Home Assistant. It creates an instance of the NetDaemon `Entity` class to interact with an existing Home Assistant entity.

:::

An alternative way to do this is by using the `Entity` extension method on your `IHaContext`:

```csharp
var atticLight = haContext.Entity("light.attic");
```

When using the code generator all discovered entities in your Home Assistant will be generated as strongly typed properties that can be accessed like this:

Expand Down
2 changes: 1 addition & 1 deletion docusaurus.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
title: 'NetDaemon',
tagline: 'Application platform written in c# for Home Assistant',
tagline: 'C# Automation platform for Home Assistant',
url: 'https://netdaemon.xyz',
baseUrl: '',
favicon: 'img/favicon.png',
Expand Down

0 comments on commit 630bcfa

Please sign in to comment.