commonMain.hassemble.entities.DeviceStateUpdater.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hassemble-jvm Show documentation
Show all versions of hassemble-jvm Show documentation
Interact with your Home Assistant server using Kotlin.
The newest version!
package hassemble.entities
import co.touchlab.kermit.Kermit
import hassemble.ActuatorsByApiName
import hassemble.SensorsByApiName
import hassemble.values.EntityId
import kotlinx.serialization.json.JsonObject
internal class ActuatorStateUpdater(private val actuatorsByApiName: ActuatorsByApiName) {
private val logger = Kermit()
operator fun invoke(newActualState: JsonObject, entityId: EntityId) {
actuatorsByApiName[entityId]?.let { entity ->
entity.trySetAttributesFromAny(newAttributes = newActualState)
entity.trySetActualStateFromAny(newState = newActualState)
logger.d { "Updated state for entity: $entityId with: $newActualState" }
}
}
}
internal class SensorStateUpdater(private val sensorsByApiName: SensorsByApiName) {
private val logger = Kermit()
operator fun invoke(newActualState: JsonObject, entityId: EntityId) {
sensorsByApiName[entityId]?.let { entity ->
entity.trySetAttributesFromAny(newAttributes = newActualState)
entity.trySetActualStateFromAny(newState = newActualState)
logger.d { "Updated state for entity: $entityId with: $newActualState" }
}
}
}