Skip to content

Instantly share code, notes, and snippets.

@v6ak
Last active January 15, 2024 12:55
Show Gist options
  • Save v6ak/c7b640547f0b6270793dfe9b06a3ec67 to your computer and use it in GitHub Desktop.
Save v6ak/c7b640547f0b6270793dfe9b06a3ec67 to your computer and use it in GitHub Desktop.
Hagen: computer desk notifications
import com.v6ak.HeteroMap
import com.v6ak.hagen.actions.*
import com.v6ak.hagen.automation.{AutomationBase, Change}
import com.v6ak.hagen.dashboards.*
import com.v6ak.hagen.expressions.*
import com.v6ak.hagen.extensions.highlights.{Highlightable, Highlightables, SimplePositiveCondition}
import com.v6ak.hagen.output.*
import com.v6ak.hagen.{BinarySensorDef, SensorDef}
import scala.concurrent.duration.*
enum ComputerDeskStatus(override val toString: String):
case Away extends ComputerDeskStatus("away")
case DeskMoving extends ComputerDeskStatus("desk-moving")
case AtDeskWithoutMovement extends ComputerDeskStatus("no-movement")
case class ComputerDesk(
name: String,
id: String,
power: Entity[Double],
lowerLimit: Const[Double],
upperLimit: Const[Double],
) extends SimpleTreeHagenModule:
private val immediateEvaluation = SensorDef(
s"$name status",
(power < lowerLimit).matches(
ifTrue = Const(ComputerDeskStatus.Away),
ifFalse = (power < upperLimit).matches(
ifTrue = Const(ComputerDeskStatus.AtDeskWithoutMovement),
ifFalse = Const(ComputerDeskStatus.DeskMoving)
)
)
)
private val breakdown = HistoryBreakdownModule.forEnum(
name = s"$name history of",
entity = immediateEvaluation.entity,
statsType = HistoryStatsType.Ratio,
period = Period.untilNow(1.hour),
updateImmediatelly = true,
)
private val positionChangeSuggestedDef = BinarySensorDef(
s"$name position change suggested",
breakdown.sensorMap(ComputerDeskStatus.DeskMoving) === Const(0.0) &&
breakdown.sensorMap(ComputerDeskStatus.AtDeskWithoutMovement) > Const(95) &&
immediateEvaluation.entity === Const(ComputerDeskStatus.AtDeskWithoutMovement)
)
private val icon: Icon = MdiIcons.Desk
private val moveHighlightable = Highlightable(
id = s"$name change your position",
conditions = Seq(SimplePositiveCondition(positionChangeSuggestedDef.entity, true)),
priority = 0.1,
link = None,
description = Const("Move it!"),
notification = true,
icon = icon,
group = None,
)
private val automations = Seq(
AutomationBase.on(Change(immediateEvaluation.entity).to(ComputerDeskStatus.DeskMoving)).noMatterWhat()
.doActions(
Notification(
Const("Position changed."),
tag = Some(s"${id}_changed2")
),
Delay(Const(3 * 60)),
Notification(
Const("clear_notification"),
tag = Some(s"${id}_changed2")
)
)
.toAutomation(s"$id position chanted"),
)
override def subModules: Seq[HagenModule] = Seq(breakdown)
override def nodeContent: HeteroMap[HagenKey[_]] = HeteroMap(
Templates -> Seq(immediateEvaluation, positionChangeSuggestedDef),
Highlightables -> Seq(moveHighlightable),
Automations -> automations,
Dashboards -> Map(
"computer-desk" -> Dashboard("Computer desk", "Computer Desk", icon, showInSidebar = true)(
DashboardPage("CDSP", "desk", icon = icon)(
HistoryGraph(hoursToShow = 1)(breakdown.statsEntities*)
)
)
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment