Compare commits
54
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
73d4255fd7 | ||
|
|
7ad36d8ccb | ||
|
|
f4c82df968 | ||
|
|
219f75c0e4 | ||
|
|
7ed846d0fc | ||
|
|
53c36a81de | ||
|
|
1bf2258167 | ||
|
|
4d8cb8dc36 | ||
|
|
1697d55ad5 | ||
|
|
bd1402ab3a | ||
|
|
c412435172 | ||
|
|
3b08988e30 | ||
|
|
44497173de | ||
|
|
894d8f8436 | ||
|
|
bd5f97f96e | ||
|
|
d6f785662b | ||
|
|
5d16bf4ade | ||
|
|
05bd609c7e | ||
|
|
4ea75beb7b | ||
|
|
96d3482af8 | ||
|
|
7fc07c1883 | ||
|
|
a7becaf2d4 | ||
|
|
9c47bbcb42 | ||
|
|
6063aafe8b | ||
|
|
fafc7111bb | ||
|
|
69cac934ec | ||
|
|
b2a74cbee4 | ||
|
|
769ab43fd2 | ||
|
|
15f3fe7c9e | ||
|
|
416a9484a5 | ||
|
|
e3bc25ce54 | ||
|
|
036473ab81 | ||
|
|
27f271691c | ||
|
|
73d546395b | ||
|
|
a969730de1 | ||
|
|
213f9d5d2a | ||
|
|
2cb8035ef4 | ||
|
|
1ec8c7211f | ||
|
|
3d7f6cb3c6 | ||
|
|
64561718a5 | ||
|
|
7570711768 | ||
|
|
7c2cfde814 | ||
|
|
9fda48f589 | ||
|
|
319104641b | ||
|
|
9861266c1e | ||
|
|
0893862f3a | ||
|
|
6078a64d00 | ||
|
|
7d08acec9d | ||
|
|
2a477b4977 | ||
|
|
09479b5c48 | ||
|
|
a19e5afd5e | ||
|
|
af46776bb0 | ||
|
|
d1bdd2eb67 | ||
|
|
9108ba9c8f |
No files matched your search
@@ -10,3 +10,4 @@ docs/superpowers
|
|||||||
*.eventlog
|
*.eventlog
|
||||||
*.eventlog.html
|
*.eventlog.html
|
||||||
*.rrd
|
*.rrd
|
||||||
|
*.rrd.*bak*
|
||||||
+5
-1
@@ -1,3 +1,7 @@
|
|||||||
creation_rules:
|
creation_rules:
|
||||||
- path_regex: "secrets.yaml"
|
- path_regex: "secrets.yaml"
|
||||||
pgp: "95DBE64C2EA64D6B0E0C8C2F9DE6E04ED1918118"
|
age:
|
||||||
|
# usb-c yubikey
|
||||||
|
- "age1yubikey1qw7m7rad6malegqypgntr7qmu97xrxxurp0c00s23263nuxnpek6w0jye60"
|
||||||
|
# usb-a yubikey
|
||||||
|
- "age1yubikey1qdnx7jum6k4fzenkqj8y5met9hyq6lqxrpzrl2788wr0q445vhupzuqtpk0"
|
||||||
+11
-9
@@ -1,8 +1,9 @@
|
|||||||
{ mkDerivation, aeson, annotated-exception, async, base, bytestring
|
{ mkDerivation, aeson, annotated-exception, async, base, bytestring
|
||||||
, cereal, cereal-conduit, conduit, containers, directory, ekg-core
|
, cereal, cereal-conduit, conduit, containers, directory, ekg-core
|
||||||
, filepath, hedgehog, hspec, hspec-hedgehog, katip, lens
|
, exceptions, filepath, hedgehog, hspec, hspec-hedgehog, http-media
|
||||||
, lens-aeson, lib, network, process, stm, text, time
|
, http-types, katip, lens, lens-aeson, lib, network, process, retry
|
||||||
, unordered-containers, uuid, websockets
|
, servant, servant-server, stm, temporary, text, time, unliftio
|
||||||
|
, unordered-containers, uuid, wai, warp, websockets
|
||||||
}:
|
}:
|
||||||
mkDerivation {
|
mkDerivation {
|
||||||
pname = "home-assistant-controller";
|
pname = "home-assistant-controller";
|
||||||
@@ -12,15 +13,16 @@ mkDerivation {
|
|||||||
isExecutable = true;
|
isExecutable = true;
|
||||||
libraryHaskellDepends = [
|
libraryHaskellDepends = [
|
||||||
aeson annotated-exception async base bytestring cereal
|
aeson annotated-exception async base bytestring cereal
|
||||||
cereal-conduit conduit containers directory ekg-core filepath katip
|
cereal-conduit conduit containers directory ekg-core exceptions
|
||||||
lens lens-aeson network process stm text time unordered-containers
|
filepath http-media http-types katip lens lens-aeson network
|
||||||
uuid websockets
|
process retry servant servant-server stm text time unliftio
|
||||||
|
unordered-containers uuid wai warp websockets
|
||||||
];
|
];
|
||||||
executableHaskellDepends = [ base ];
|
executableHaskellDepends = [ base ];
|
||||||
testHaskellDepends = [
|
testHaskellDepends = [
|
||||||
aeson annotated-exception async base cereal containers directory
|
aeson annotated-exception async base bytestring cereal containers
|
||||||
ekg-core hedgehog hspec hspec-hedgehog katip process stm text time
|
directory ekg-core hedgehog hspec hspec-hedgehog katip process
|
||||||
unordered-containers uuid
|
servant stm temporary text time unordered-containers uuid
|
||||||
];
|
];
|
||||||
license = lib.meta.getLicenseFromSpdxId "BSD-3-Clause";
|
license = lib.meta.getLicenseFromSpdxId "BSD-3-Clause";
|
||||||
mainProgram = "home-assistant-controller";
|
mainProgram = "home-assistant-controller";
|
||||||
|
|||||||
@@ -62,14 +62,20 @@ library
|
|||||||
exposed-modules: AFRP
|
exposed-modules: AFRP
|
||||||
, HomeAssistant.Controller
|
, HomeAssistant.Controller
|
||||||
, HomeAssistant.Controller.Bedroom
|
, HomeAssistant.Controller.Bedroom
|
||||||
|
, HomeAssistant.Controller.Hallway
|
||||||
, HomeAssistant.Controller.Kitchen
|
, HomeAssistant.Controller.Kitchen
|
||||||
, HomeAssistant.Controller.Children
|
, HomeAssistant.Controller.Children
|
||||||
|
, HomeAssistant.Controller.Livingroom
|
||||||
, HomeAssistant.Controller.Ruuvi
|
, HomeAssistant.Controller.Ruuvi
|
||||||
, HomeAssistant.Runtime
|
, HomeAssistant.Runtime
|
||||||
, HomeAssistant.Runtime.Bus
|
, HomeAssistant.Runtime.Bus
|
||||||
, HomeAssistant.Runtime.Connection
|
, HomeAssistant.Runtime.Connection
|
||||||
|
, HomeAssistant.Runtime.Flags
|
||||||
|
, HomeAssistant.Runtime.Graphing
|
||||||
, HomeAssistant.Runtime.Metrics
|
, HomeAssistant.Runtime.Metrics
|
||||||
, HomeAssistant.Runtime.Supervisor
|
, HomeAssistant.Runtime.Supervisor
|
||||||
|
, HomeAssistant.Runtime.RateLimit
|
||||||
|
, HttpServer
|
||||||
|
|
||||||
-- Modules included in this library but not exported.
|
-- Modules included in this library but not exported.
|
||||||
-- other-modules:
|
-- other-modules:
|
||||||
@@ -80,10 +86,13 @@ library
|
|||||||
-- Other library packages from which modules are imported.
|
-- Other library packages from which modules are imported.
|
||||||
build-depends: base ^>=4.20.2.0
|
build-depends: base ^>=4.20.2.0
|
||||||
, websockets
|
, websockets
|
||||||
|
, http-media
|
||||||
, aeson
|
, aeson
|
||||||
, lens-aeson
|
, lens-aeson
|
||||||
, lens
|
, lens
|
||||||
|
, exceptions
|
||||||
, text
|
, text
|
||||||
|
, unliftio
|
||||||
, network
|
, network
|
||||||
, bytestring
|
, bytestring
|
||||||
, time
|
, time
|
||||||
@@ -102,6 +111,12 @@ library
|
|||||||
, filepath
|
, filepath
|
||||||
, conduit
|
, conduit
|
||||||
, cereal-conduit
|
, cereal-conduit
|
||||||
|
, servant
|
||||||
|
, servant-server
|
||||||
|
, http-types
|
||||||
|
, warp
|
||||||
|
, wai
|
||||||
|
, retry
|
||||||
|
|
||||||
-- Directories containing source files.
|
-- Directories containing source files.
|
||||||
hs-source-dirs: src
|
hs-source-dirs: src
|
||||||
@@ -143,15 +158,17 @@ test-suite home-assistant-controller-test
|
|||||||
|
|
||||||
-- Modules included in this executable, other than Main.
|
-- Modules included in this executable, other than Main.
|
||||||
other-modules: AFRPLawsSpec
|
other-modules: AFRPLawsSpec
|
||||||
, AFRPSpec
|
, AFRPSpec
|
||||||
, BackoffProp
|
, BedroomSpec
|
||||||
, BedroomSpec
|
, BusSpec
|
||||||
, BusSpec
|
, ConnectionSpec
|
||||||
, ConnectionSpec
|
, FlagsSpec
|
||||||
, MetricsSpec
|
, GraphingSpec
|
||||||
, RuntimeSpec
|
, KitchenSpec
|
||||||
, SupervisorSpec
|
, LivingroomSpec
|
||||||
, Support
|
, MetricsSpec
|
||||||
|
, RuntimeSpec
|
||||||
|
, Support
|
||||||
|
|
||||||
-- LANGUAGE extensions used by modules in this package.
|
-- LANGUAGE extensions used by modules in this package.
|
||||||
-- other-extensions:
|
-- other-extensions:
|
||||||
@@ -169,8 +186,10 @@ test-suite home-assistant-controller-test
|
|||||||
build-depends:
|
build-depends:
|
||||||
base ^>=4.20.2.0,
|
base ^>=4.20.2.0,
|
||||||
home-assistant-controller,
|
home-assistant-controller,
|
||||||
|
bytestring,
|
||||||
hspec,
|
hspec,
|
||||||
stm,
|
stm,
|
||||||
|
servant,
|
||||||
aeson,
|
aeson,
|
||||||
cereal,
|
cereal,
|
||||||
text,
|
text,
|
||||||
@@ -185,4 +204,5 @@ test-suite home-assistant-controller-test
|
|||||||
ekg-core,
|
ekg-core,
|
||||||
unordered-containers,
|
unordered-containers,
|
||||||
process,
|
process,
|
||||||
directory
|
directory,
|
||||||
|
temporary
|
||||||
+21
-21
@@ -1,28 +1,28 @@
|
|||||||
HA_TOKEN: ENC[AES256_GCM,data:zs9nTT2EV/OhBiH9alUuupUvYURXj40lxe7IKKWXdExW+BW98yEDHcQr/KNsQ0Iu9+U2GrsA8WpODeM351kI3Owj3rl082tObBJ2jwF1iyYcXhoH3xvbHb+HN802SBOTIO32m+ZwGvIVPjF8W+67eGKTrIUcqnKj7Ab/TchG+mYPLdil0mYjITY+DCUi0mW1nElCaTTexovj4PHlUdA1xIsCYF+XnTypQv8o+Zy8MsMZtdzNQKJG,iv:Lmxmp5y15sqhQvEO7liVk6x3Y26qtoL39TT2MSqZdok=,tag:1Xvs6YUilP/XH0UxYngTIg==,type:str]
|
HA_TOKEN: ENC[AES256_GCM,data:zs9nTT2EV/OhBiH9alUuupUvYURXj40lxe7IKKWXdExW+BW98yEDHcQr/KNsQ0Iu9+U2GrsA8WpODeM351kI3Owj3rl082tObBJ2jwF1iyYcXhoH3xvbHb+HN802SBOTIO32m+ZwGvIVPjF8W+67eGKTrIUcqnKj7Ab/TchG+mYPLdil0mYjITY+DCUi0mW1nElCaTTexovj4PHlUdA1xIsCYF+XnTypQv8o+Zy8MsMZtdzNQKJG,iv:Lmxmp5y15sqhQvEO7liVk6x3Y26qtoL39TT2MSqZdok=,tag:1Xvs6YUilP/XH0UxYngTIg==,type:str]
|
||||||
HA_HOST: ENC[AES256_GCM,data:MfA+RReJHnKP31bD4YAoZrc=,iv:Z9xDtzPsUER4jQ12BbM2EtnLNggZhOWzCb7VAL2I02g=,tag:/Eogbd2kT7phdhaR/vGNDA==,type:str]
|
HA_HOST: ENC[AES256_GCM,data:MfA+RReJHnKP31bD4YAoZrc=,iv:Z9xDtzPsUER4jQ12BbM2EtnLNggZhOWzCb7VAL2I02g=,tag:/Eogbd2kT7phdhaR/vGNDA==,type:str]
|
||||||
sops:
|
sops:
|
||||||
|
age:
|
||||||
|
- enc: |
|
||||||
|
-----BEGIN AGE ENCRYPTED FILE-----
|
||||||
|
YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IHBpdi1wMjU2IHJKaXRSQSBBeEpPRXVT
|
||||||
|
NDY2ZUxZRDB5a21waDFoUXJsQkdsbFhQRjhobFBGVDlaZmlpSApGNEpKS1JzUStw
|
||||||
|
eWFJcUkxbWRtWURLQm0rWWtURXVNVEwvVlRLa3prNFZRCi0tLSBJWjhiL2RLV3ZO
|
||||||
|
NStpUlVSRHlSTEJWa2ZkenI3T292RnMwaHB6cnJRSW1NCt3zpORUDB2m9V+IK9UI
|
||||||
|
Z94uFopKZsRxzbxbZWklNykYoryjKfVCTO7sFDxapzr1UlG3UjVp2kgBJeDCEBVt
|
||||||
|
SwQ=
|
||||||
|
-----END AGE ENCRYPTED FILE-----
|
||||||
|
recipient: age1yubikey1qw7m7rad6malegqypgntr7qmu97xrxxurp0c00s23263nuxnpek6w0jye60
|
||||||
|
- enc: |
|
||||||
|
-----BEGIN AGE ENCRYPTED FILE-----
|
||||||
|
YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IHBpdi1wMjU2IFVnM21GQSBBNFYvaEM2
|
||||||
|
MFoyR3NhcDc4eklaTHJaUVZRWlJzT2dkclBOaExLYWZDb05DaQpidVcrRklydWJZ
|
||||||
|
WUhxQUR1c281R1RtdGlxNy9uMmJPODE1YXQ1K2g3bnFJCi0tLSAwblViWnhPMUNC
|
||||||
|
dTRoelphM1djcW9DK0NkWE44TSsvYmE5aEtIMW5RVG9vCqO5SqZ73i4wRn9qWyXA
|
||||||
|
8bW5XLeTN4qCIeMFEKloiAKDUKFWutHt/9SedYUsqMuRmQ9btmWg5TdkHV4S/6iR
|
||||||
|
qDU=
|
||||||
|
-----END AGE ENCRYPTED FILE-----
|
||||||
|
recipient: age1yubikey1qdnx7jum6k4fzenkqj8y5met9hyq6lqxrpzrl2788wr0q445vhupzuqtpk0
|
||||||
lastmodified: "2026-08-21T10:17:59Z"
|
lastmodified: "2026-08-21T10:17:59Z"
|
||||||
mac: ENC[AES256_GCM,data:GuNyo/3flhR30e6kds1iXcDGmq0Xq2hqvBmAvYtakKwzUzMocP9vImfxs+MfVNiMGirlkwbihrX5mFSbfORjES/FDTs6p0100H1tkBqPTecW88PC6/66Js9umVevGYbgToCNBjV9mUBu7o7SyU+NYLnZ/8X4xdx0y6/MKnuvjUA=,iv:/nL5AIL80iV3sutjMTmHyF62SRezmX9XyJJ6+o9TdkQ=,tag:SNMu17rgMIhdzZVsGmXyfg==,type:str]
|
mac: ENC[AES256_GCM,data:GuNyo/3flhR30e6kds1iXcDGmq0Xq2hqvBmAvYtakKwzUzMocP9vImfxs+MfVNiMGirlkwbihrX5mFSbfORjES/FDTs6p0100H1tkBqPTecW88PC6/66Js9umVevGYbgToCNBjV9mUBu7o7SyU+NYLnZ/8X4xdx0y6/MKnuvjUA=,iv:/nL5AIL80iV3sutjMTmHyF62SRezmX9XyJJ6+o9TdkQ=,tag:SNMu17rgMIhdzZVsGmXyfg==,type:str]
|
||||||
pgp:
|
|
||||||
- created_at: "2026-08-20T05:39:51Z"
|
|
||||||
enc: |-
|
|
||||||
-----BEGIN PGP MESSAGE-----
|
|
||||||
|
|
||||||
hQIMA5Urps8/GyTEAQ//ZQeXRy2SHcR2/hVqleE8V9/3u150TWX7GpDPVdjK7pxu
|
|
||||||
zK1JZSw/FSpNrUPFHXk6rWDm6lv1k/GCx34egHwgumlO1/192nQLMxwNHXhDeJmc
|
|
||||||
k6y44pb3Z1m/upVp62fIif2/f7X2BIQqGI0nZBaCM6sD5FE8HqXCk+jYw4YMvNZp
|
|
||||||
I6dtxxnYwW5wK5Gfv86hTHkn2ibDqARMgd0QsB8fv9fpE9iHWTeeSHqkCBVnb5Kn
|
|
||||||
s0Xk1DeED/ylLAlHfY0p0LVIjeCK5KCQCbIHXh4yb8ioxUxg9LQp8glXbjqbfaBx
|
|
||||||
FadLksLDxBuO0CWxAB4bVk4G/MKCL1nN3KzZBWfy96aknHHGQre0DCe7IxfmSYjd
|
|
||||||
f5UhfeFCVrWQTOtaoq3Oimw6CFeoGe04FKogQmZOWK1zmJmHxjCRGn4n5vzEZB1g
|
|
||||||
Cq+fzDLl2+Krr91Wq/xGwatoWaPl+SDzw1i7CnfGhWJn50BtO/YI1229yLSyANhs
|
|
||||||
M2R9JXxiWFst8xur0nJBKkzH+EZpaVOOLT7FgFpf/vWOO/se1kVPaMAhg6fQ3IwJ
|
|
||||||
JphPD+FTkTDY4GBefVP/qOhOnibcIjhwTiDWP6ppcaOxgVjyDyLsqS96jYSwfyKQ
|
|
||||||
puDyzrD4BlLv1lc2lULCtgMwZj428Vpd7YI1SjnxxKnKhIQ1IZx0ddneHn9fSivS
|
|
||||||
XgE6NpQKQ110FFYse8P1fb2yNaYb2ZRbLh0cUOe2qcTATTvK5OdDVlgCjNsO8Fgl
|
|
||||||
OO8hwmsk+1QziCwO4Md8NPdyNn4Kup1iRojMyAh23CT5LyYXoccakWvF/awLvz8=
|
|
||||||
=nX50
|
|
||||||
-----END PGP MESSAGE-----
|
|
||||||
fp: 95DBE64C2EA64D6B0E0C8C2F9DE6E04ED1918118
|
|
||||||
unencrypted_suffix: _unencrypted
|
unencrypted_suffix: _unencrypted
|
||||||
version: 3.13.3
|
version: 3.13.3
|
||||||
+4
-3
@@ -103,9 +103,10 @@ mergeCodec (Codec agetter aputter) (Codec bgetter bputter) = Codec (mergeGet age
|
|||||||
data Pair a b = Pair !a !b
|
data Pair a b = Pair !a !b
|
||||||
|
|
||||||
data Request = Request
|
data Request = Request
|
||||||
{ requestTime :: !UTCTime
|
{ requestTime :: !UTCTime
|
||||||
, requestTimeZone :: !TimeZone
|
, requestTimeZone :: !TimeZone
|
||||||
, requestTraceId :: !UUID
|
, requestTraceId :: !UUID
|
||||||
|
, requestHandler :: !T.Text
|
||||||
} deriving (Show, Eq)
|
} deriving (Show, Eq)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -19,6 +19,8 @@ module HomeAssistant.Controller
|
|||||||
, light
|
, light
|
||||||
, Presence(..)
|
, Presence(..)
|
||||||
, presence
|
, presence
|
||||||
|
, motion
|
||||||
|
, motionToPresence
|
||||||
, debug
|
, debug
|
||||||
, traceEvent
|
, traceEvent
|
||||||
, traceValue
|
, traceValue
|
||||||
@@ -26,20 +28,28 @@ module HomeAssistant.Controller
|
|||||||
, Target(..)
|
, Target(..)
|
||||||
, brightness
|
, brightness
|
||||||
, Light(..)
|
, Light(..)
|
||||||
|
, ikeaQuickButton
|
||||||
|
, IkeaButton(..)
|
||||||
|
, IkeaGesture(..)
|
||||||
|
, activateScene
|
||||||
|
, activateSceneWith
|
||||||
|
, ikeaBoxRemote
|
||||||
|
, IkeaController(..)
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import AFRP (Mealy (..), eff, Event(..), events, filterA, (>>|), toEvent, Request)
|
import AFRP (Mealy (..), eff, Event(..), events, filterA, (>>|), toEvent, Request, hold, lMerge, waitFor, tag, edge)
|
||||||
import Control.Arrow (Arrow(..), returnA)
|
import Control.Arrow (Arrow(..), returnA)
|
||||||
import Control.Category ((>>>))
|
import Control.Category ((>>>))
|
||||||
import Data.Aeson (Value, object, (.=))
|
import Data.Aeson (Value, object, (.=))
|
||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
import qualified Data.Set as S
|
import qualified Data.Set as S
|
||||||
import Control.Lens (has, only, (^?), to)
|
import Control.Lens (has, only, (^?), to, traversed)
|
||||||
import Data.Aeson.Lens (key, _String, _Integral)
|
import Data.Aeson.Lens (key, _String, _Integral)
|
||||||
import qualified Data.Text.Lens as TL
|
import qualified Data.Text.Lens as TL
|
||||||
import Data.Bool (bool)
|
import Data.Bool (bool)
|
||||||
import Data.Serialize (Serialize)
|
import Data.Serialize (Serialize)
|
||||||
import GHC.Generics (Generic)
|
import GHC.Generics (Generic)
|
||||||
|
import Data.Time (NominalDiffTime)
|
||||||
|
|
||||||
data Target = EntityId !T.Text | AreaId !T.Text
|
data Target = EntityId !T.Text | AreaId !T.Text
|
||||||
deriving (Show,Eq,Ord)
|
deriving (Show,Eq,Ord)
|
||||||
@@ -93,10 +103,35 @@ data Presence = Occupied | Unoccupied
|
|||||||
instance Serialize Presence
|
instance Serialize Presence
|
||||||
|
|
||||||
presence :: T.Text -> HASS (Event Value) (Event Presence)
|
presence :: T.Text -> HASS (Event Value) (Event Presence)
|
||||||
presence entityId =entityBool entityId
|
presence entityId = entityBool entityId
|
||||||
>>> arr (fmap (bool Unoccupied Occupied))
|
>>> arr (fmap (bool Unoccupied Occupied))
|
||||||
|
|
||||||
|
|
||||||
|
data Motion = MotionDetected | MotionNotDetected | MotionUnknown
|
||||||
|
deriving (Show, Eq, Generic)
|
||||||
|
|
||||||
|
|
||||||
|
motion :: T.Text -> HASS (Event Value) Motion
|
||||||
|
motion entityId = entityBool entityId
|
||||||
|
>>> arr (fmap (bool MotionNotDetected MotionDetected))
|
||||||
|
>>> hold MotionUnknown
|
||||||
|
|
||||||
|
|
||||||
|
-- Convert a motion status into a simulated presence
|
||||||
|
--
|
||||||
|
-- A motion tracker detects _motion_, but if you are still, it can't detect you.
|
||||||
|
-- A simple and cheap heuristic is to assume the person is a bit longer there
|
||||||
|
motionToPresence :: NominalDiffTime -> HASS Motion Presence
|
||||||
|
motionToPresence delay = (eventOccupied &&& eventUnoccupied)
|
||||||
|
>>> arr (uncurry lMerge)
|
||||||
|
>>> AFRP.hold Unoccupied
|
||||||
|
where
|
||||||
|
eventOccupied :: HASS Motion (Event Presence)
|
||||||
|
eventOccupied = arr (== MotionDetected) >>> edge >>> arr (fmap (const Occupied))
|
||||||
|
eventUnoccupied :: HASS Motion (Event Presence)
|
||||||
|
eventUnoccupied = arr (== MotionNotDetected) >>> waitFor delay >>> arr (tag Unoccupied)
|
||||||
|
|
||||||
|
instance Serialize Motion
|
||||||
|
|
||||||
data Light
|
data Light
|
||||||
= Off
|
= Off
|
||||||
@@ -163,3 +198,74 @@ brightness entityId =
|
|||||||
>>> AFRP.toEvent
|
>>> AFRP.toEvent
|
||||||
where
|
where
|
||||||
eventBrightness v = v ^? key "event" . key "variables" . key "trigger" . key "to_state" . key "attributes" . key "brightness" . _Integral
|
eventBrightness v = v ^? key "event" . key "variables" . key "trigger" . key "to_state" . key "attributes" . key "brightness" . _Integral
|
||||||
|
|
||||||
|
data IkeaGesture
|
||||||
|
= ShortClick
|
||||||
|
| ShortRelease
|
||||||
|
| DoubleClick
|
||||||
|
| LongClick
|
||||||
|
deriving Show
|
||||||
|
|
||||||
|
data IkeaButton a = OnButton !a | OffButton !a
|
||||||
|
deriving Show
|
||||||
|
|
||||||
|
|
||||||
|
toIkeaQuickButton :: T.Text -> Maybe (IkeaButton IkeaGesture)
|
||||||
|
toIkeaQuickButton = \case
|
||||||
|
-- 1 is on, 2 is off side of the button
|
||||||
|
"1_initial_press" -> Just $ OnButton ShortClick
|
||||||
|
"1_short_release" -> Just $ OnButton ShortRelease
|
||||||
|
"1_double_press" -> Just $ OnButton DoubleClick
|
||||||
|
"1_long_press" -> Just $ OnButton LongClick
|
||||||
|
"2_initial_press" -> Just $ OffButton ShortClick
|
||||||
|
"2_short_release" -> Just $ OffButton ShortRelease
|
||||||
|
"2_double_press" -> Just $ OffButton DoubleClick
|
||||||
|
"2_long_press" -> Just $ OffButton LongClick
|
||||||
|
_ -> Nothing
|
||||||
|
|
||||||
|
ikeaQuickButton :: T.Text -> HASS (Event Value) (Event (IkeaButton IkeaGesture))
|
||||||
|
ikeaQuickButton entityId =
|
||||||
|
entityChangeEvent' entityId
|
||||||
|
>>| arr (maybe (Left ()) Right . eventType)
|
||||||
|
>>> toEvent
|
||||||
|
where
|
||||||
|
eventType v = v ^? key "event" . key "variables" . key "trigger" . key "to_state" . key "attributes" . key "event_type" . _String . to toIkeaQuickButton . traversed
|
||||||
|
|
||||||
|
data IkeaController
|
||||||
|
= ControllerOn -- Short press on
|
||||||
|
| ControllerOff -- Short press off
|
||||||
|
| ControllerBrighnessUp -- Turn brightness up
|
||||||
|
| ControllerBrighnessDown -- Turn brighness down
|
||||||
|
| ControllerArrowRight -- Turn brighness down
|
||||||
|
| ControllerArrowLeft -- Turn brighness down
|
||||||
|
| ControllerBrighnessStop
|
||||||
|
deriving Show
|
||||||
|
|
||||||
|
toIkeaController :: T.Text -> Maybe IkeaController
|
||||||
|
toIkeaController = \case
|
||||||
|
"on" -> Just ControllerOn
|
||||||
|
"off" -> Just ControllerOff
|
||||||
|
"brightness_move_up" -> Just ControllerBrighnessUp
|
||||||
|
"brightness_move_down" -> Just ControllerBrighnessDown
|
||||||
|
"brightness_stop" -> Just ControllerBrighnessStop
|
||||||
|
"arrow_right_click" -> Just ControllerArrowRight
|
||||||
|
"arrow_left_click" -> Just ControllerArrowLeft
|
||||||
|
_ -> Nothing
|
||||||
|
|
||||||
|
ikeaBoxRemote :: T.Text -> HASS (Event Value) (Event IkeaController)
|
||||||
|
ikeaBoxRemote entityId = entityChangeEvent' entityId
|
||||||
|
>>| arr (maybe (Left ()) Right . eventType)
|
||||||
|
>>> toEvent
|
||||||
|
where
|
||||||
|
eventType v = v ^? key "event" . key "variables" . key "trigger" . key "to_state" . key "attributes" . key "event_type" . _String . to toIkeaController . traversed
|
||||||
|
|
||||||
|
activateScene :: T.Text -> Service
|
||||||
|
activateScene sceneId = activateSceneWith sceneId Nothing
|
||||||
|
|
||||||
|
activateSceneWith :: T.Text -> Maybe Value -> Service
|
||||||
|
activateSceneWith sceneId d = Service
|
||||||
|
{ serviceDomain="scene"
|
||||||
|
, serviceName= "turn_on"
|
||||||
|
, serviceTarget= [EntityId sceneId]
|
||||||
|
, serviceData = d
|
||||||
|
}
|
||||||
@@ -4,11 +4,9 @@ module HomeAssistant.Controller.Bedroom where
|
|||||||
|
|
||||||
import HomeAssistant.Controller
|
import HomeAssistant.Controller
|
||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
import AFRP (Event (..), (>>|), toEvent, lMerge, duration, edge)
|
import AFRP (Event (..), lMerge, duration, edge)
|
||||||
import Data.Aeson (Value, object, (.=))
|
import Data.Aeson (Value, object, (.=))
|
||||||
import Control.Arrow ((>>>), returnA, arr, Arrow (..))
|
import Control.Arrow ((>>>), returnA, arr, Arrow (..))
|
||||||
import Control.Lens ((^?), to, traversed)
|
|
||||||
import Data.Aeson.Lens (key, _String)
|
|
||||||
import Data.Bool (bool)
|
import Data.Bool (bool)
|
||||||
import Data.Time (NominalDiffTime)
|
import Data.Time (NominalDiffTime)
|
||||||
import qualified AFRP
|
import qualified AFRP
|
||||||
@@ -29,84 +27,83 @@ createBedroomScene = Service
|
|||||||
, serviceTarget= []
|
, serviceTarget= []
|
||||||
}
|
}
|
||||||
|
|
||||||
|
data PresenceLights
|
||||||
activateScene :: T.Text -> Service
|
= LightsOn -- Restore lights
|
||||||
activateScene sceneId = Service
|
| LightsOff -- Timer expired, darkness
|
||||||
{ serviceDomain="scene"
|
| LightsSleep -- No presence detected, sleep mode
|
||||||
, serviceName= "turn_on"
|
deriving Show
|
||||||
, serviceTarget= [EntityId sceneId]
|
|
||||||
, serviceData = Nothing
|
|
||||||
}
|
|
||||||
|
|
||||||
-- Bedroom presence
|
-- Bedroom presence
|
||||||
bedroomPresence :: HASS (Event Value) (Event Presence)
|
bedroomPresence :: HASS (Event Value) (Event Presence)
|
||||||
bedroomPresence = presence "binary_sensor.presence_sensor_bedroom_occupancy"
|
bedroomPresence = presence "binary_sensor.presence_sensor_bedroom_occupancy"
|
||||||
|
|
||||||
|
presenceLightEvents :: HASS (Event Value) (Event PresenceLights)
|
||||||
|
presenceLightEvents =
|
||||||
|
bedroomPresence >>> AFRP.hold Unoccupied
|
||||||
|
>>> arr lightWanted
|
||||||
|
>>> (sleepEvent &&& offEvent &&& onEvent)
|
||||||
|
>>> arr (\(s,(o,n)) -> s `AFRP.lMerge` o `AFRP.lMerge` n)
|
||||||
|
where
|
||||||
|
lightWanted o = o == Occupied
|
||||||
|
sleepEvent = arr not >>> AFRP.edge >>> arr (AFRP.tag LightsSleep)
|
||||||
|
onEvent = AFRP.edge >>> arr (AFRP.tag LightsOn)
|
||||||
|
offEvent = arr not >>> AFRP.waitFor 300 >>> arr (AFRP.tag LightsOff)
|
||||||
|
|
||||||
bedroomPresenceController :: HASS (Event Value) ()
|
bedroomPresenceController :: HASS (Event Value) ()
|
||||||
bedroomPresenceController = proc x -> do
|
bedroomPresenceController = proc x -> do
|
||||||
p <- bedroomPresence -< x
|
p <- presenceLightEvents >>> traceEvent -< x
|
||||||
case p of
|
case p of
|
||||||
Event Unoccupied -> callService createBedroomScene >>> callService (light bedroomLights Off) -< ()
|
Event LightsOff -> do
|
||||||
Event Occupied -> callService (activateScene "makuuhuone_lights_snapshot") -< ()
|
callService (light bedroomLights Off) -< ()
|
||||||
|
Event LightsOn -> do
|
||||||
|
callService (activateSceneWith "scene.makuuhuone_lights_snapshot" transition) -< ()
|
||||||
|
Event LightsSleep -> do
|
||||||
|
callService createBedroomScene -< ()
|
||||||
|
callService (activateSceneWith "scene.makuuhuone_lepotila" transition) -< ()
|
||||||
_ -> returnA -< ()
|
_ -> returnA -< ()
|
||||||
|
|
||||||
data IkeaGesture
|
|
||||||
= ShortClick
|
|
||||||
| ShortRelease
|
|
||||||
| DoubleClick
|
|
||||||
| LongClick
|
|
||||||
deriving Show
|
|
||||||
data IkeaButton a = OnButton !a | OffButton !a
|
|
||||||
deriving Show
|
|
||||||
|
|
||||||
|
|
||||||
toIkeaQuickButton :: T.Text -> Maybe (IkeaButton IkeaGesture)
|
|
||||||
toIkeaQuickButton = \case
|
|
||||||
-- 1 is on, 2 is off side of the button
|
|
||||||
"1_initial_press" -> Just $ OnButton ShortClick
|
|
||||||
"1_short_release" -> Just $ OnButton ShortRelease
|
|
||||||
"1_double_press" -> Just $ OnButton DoubleClick
|
|
||||||
"1_long_press" -> Just $ OnButton LongClick
|
|
||||||
"2_initial_press" -> Just $ OffButton ShortClick
|
|
||||||
"2_short_release" -> Just $ OffButton ShortRelease
|
|
||||||
"2_double_press" -> Just $ OffButton DoubleClick
|
|
||||||
"2_long_press" -> Just $ OffButton LongClick
|
|
||||||
_ -> Nothing
|
|
||||||
|
|
||||||
ikeaQuickButton :: T.Text -> HASS (Event Value) (Event (IkeaButton IkeaGesture))
|
|
||||||
ikeaQuickButton entityId =
|
|
||||||
entityChangeEvent' entityId
|
|
||||||
>>| arr (maybe (Left ()) Right . eventType)
|
|
||||||
>>> toEvent
|
|
||||||
where
|
where
|
||||||
eventType v = v ^? key "event" . key "variables" . key "trigger" . key "to_state" . key "attributes" . key "event_type" . _String . to toIkeaQuickButton . traversed
|
transition = Just $ object ["transition" .= (10 :: Int)]
|
||||||
|
|
||||||
|
|
||||||
data BedroomControls
|
data BedroomControls
|
||||||
= Masse (IkeaButton IkeaGesture)
|
= Masse (IkeaButton IkeaGesture)
|
||||||
| Enishen (IkeaButton IkeaGesture)
|
| Enishen (IkeaButton IkeaGesture)
|
||||||
|
| Central IkeaController
|
||||||
deriving Show
|
deriving Show
|
||||||
|
|
||||||
|
|
||||||
bedroomButton :: HASS (Event Value) (Event BedroomControls)
|
bedroomButton :: HASS (Event Value) (Event BedroomControls)
|
||||||
bedroomButton = (masseButton &&& enishenButton) >>> arr (uncurry lMerge)
|
bedroomButton = (masseButton &&& enishenButton &&& centralButton)
|
||||||
|
>>> arr (\(a,(b,c)) -> a `lMerge` b `lMerge` c)
|
||||||
where
|
where
|
||||||
masseButton = fmap Masse <$> ikeaQuickButton "event.bedroom_quick_remote_masse_action"
|
masseButton = fmap Masse <$> ikeaQuickButton "event.bedroom_quick_remote_masse_action"
|
||||||
enishenButton = fmap Enishen <$> ikeaQuickButton "event.bedroom_quick_jemina_action"
|
enishenButton = fmap Enishen <$> ikeaQuickButton "event.bedroom_quick_jemina_action"
|
||||||
|
centralButton = fmap Central <$> ikeaBoxRemote "event.bedroom_remote_action"
|
||||||
|
|
||||||
bedroomButtonController :: HASS (Event Value) ()
|
bedroomButtonController :: HASS (Event Value) ()
|
||||||
bedroomButtonController = proc x -> do
|
bedroomButtonController = proc x -> do
|
||||||
ev <- bedroomButton >>> traceEvent -< x
|
ev <- bedroomButton >>> traceEvent -< x
|
||||||
|
-- Any event will cause the sleep mode to be turned off
|
||||||
case ev of
|
case ev of
|
||||||
Event (Masse (OnButton ShortRelease)) -> callService (activateScene "scene.makuuhuone_masse") -< ()
|
Event (Masse (OnButton ShortRelease)) -> do
|
||||||
Event (Masse (OnButton DoubleClick)) -> callService (activateScene "scene.makuuhuone_keski") -< ()
|
callService (activateSceneWith "scene.makuuhuone_masse" transition) -< ()
|
||||||
Event (Masse (OnButton LongClick)) -> callService (activateScene "scene.makuuhuone_kirkas") -< ()
|
Event (Masse (OnButton DoubleClick)) -> do
|
||||||
Event (Masse (OffButton _)) -> callService (light [AreaId "makuuhuone"] Off) -< ()
|
callService (activateSceneWith "scene.makuuhuone_keski" transition) -< ()
|
||||||
Event (Enishen (OnButton ShortRelease)) -> callService (activateScene "scene.makuuhuone_jemina") -< ()
|
Event (Masse (OnButton LongClick)) -> do
|
||||||
Event (Enishen (OnButton DoubleClick)) -> callService (activateScene "scene.makuuhuone_keski") -< ()
|
callService (activateSceneWith "scene.makuuhuone_kirkas" transition) -< ()
|
||||||
Event (Enishen (OnButton LongClick)) -> callService (activateScene "scene.makuuhuone_kirkas") -< ()
|
Event (Masse (OffButton _)) -> do
|
||||||
Event (Enishen (OffButton _)) -> callService (light [AreaId "makuuhuone"] Off) -< ()
|
callService (light [AreaId "makuuhuone"] Off) -< ()
|
||||||
|
Event (Enishen (OnButton ShortRelease)) -> do
|
||||||
|
callService (activateSceneWith "scene.makuuhuone_jemina" transition) -< ()
|
||||||
|
Event (Enishen (OnButton DoubleClick)) -> do
|
||||||
|
callService (activateSceneWith "scene.makuuhuone_keski" transition) -< ()
|
||||||
|
Event (Enishen (OnButton LongClick)) -> do
|
||||||
|
callService (activateSceneWith "scene.makuuhuone_kirkas" transition) -< ()
|
||||||
|
Event (Enishen (OffButton _)) -> do
|
||||||
|
callService (light [AreaId "makuuhuone"] Off) -< ()
|
||||||
_ -> returnA -< ()
|
_ -> returnA -< ()
|
||||||
|
where
|
||||||
|
transition = Just $ object ["transition" .= (5 :: Int)]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +1,14 @@
|
|||||||
{-# LANGUAGE Arrows #-}
|
{-# LANGUAGE Arrows #-}
|
||||||
{-# LANGUAGE OverloadedStrings #-}
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
module HomeAssistant.Controller.Children where
|
module HomeAssistant.Controller.Children where
|
||||||
import HomeAssistant.Controller (HASS, callService, Target (AreaId), light, Light(..))
|
import HomeAssistant.Controller (HASS, callService, Target (AreaId), light, Light(..), ikeaQuickButton, IkeaGesture (..), IkeaButton (..), traceEvent, activateScene)
|
||||||
import AFRP (Event)
|
import AFRP (Event (..))
|
||||||
import qualified AFRP
|
import qualified AFRP
|
||||||
import Control.Arrow (Arrow(..), (>>>))
|
import Control.Arrow (Arrow(..), (>>>), returnA)
|
||||||
import Data.Time (Day, TimeOfDay (..), localDay, LocalTime (..))
|
import Data.Time (Day, TimeOfDay (..), localDay, LocalTime (..))
|
||||||
import Data.Time.Calendar.OrdinalDate (WeekOfYear, mondayStartWeek)
|
import Data.Time.Calendar.OrdinalDate (WeekOfYear, mondayStartWeek)
|
||||||
import Data.Functor.Contravariant (Predicate (..), (>$<))
|
import Data.Functor.Contravariant (Predicate (..), (>$<))
|
||||||
|
import Data.Aeson (Value)
|
||||||
|
|
||||||
-- Let's see building some reasonable interface for utctime
|
-- Let's see building some reasonable interface for utctime
|
||||||
dow :: Day -> (WeekOfYear, Int)
|
dow :: Day -> (WeekOfYear, Int)
|
||||||
@@ -48,7 +49,6 @@ timersOff =
|
|||||||
, day 3 <> at (08,15)
|
, day 3 <> at (08,15)
|
||||||
, day 4 <> at (08,15)
|
, day 4 <> at (08,15)
|
||||||
, day 5 <> at (08,15)
|
, day 5 <> at (08,15)
|
||||||
, at (18,57) -- debug
|
|
||||||
]
|
]
|
||||||
where
|
where
|
||||||
dayOfWeek = snd . mondayStartWeek . localDay
|
dayOfWeek = snd . mondayStartWeek . localDay
|
||||||
@@ -62,7 +62,6 @@ timersOn =
|
|||||||
, day 3 <> at (07,30)
|
, day 3 <> at (07,30)
|
||||||
, day 4 <> at (07,30)
|
, day 4 <> at (07,30)
|
||||||
, day 5 <> at (07,30)
|
, day 5 <> at (07,30)
|
||||||
, at (18,55) -- debug
|
|
||||||
]
|
]
|
||||||
where
|
where
|
||||||
dayOfWeek = snd . mondayStartWeek . localDay
|
dayOfWeek = snd . mondayStartWeek . localDay
|
||||||
@@ -80,3 +79,17 @@ lightsOn = foldMap atTime timersOn
|
|||||||
lightsOff :: HASS a ()
|
lightsOff :: HASS a ()
|
||||||
lightsOff = foldMap atTime timersOff
|
lightsOff = foldMap atTime timersOff
|
||||||
>>> AFRP.onEvent (callService (light [AreaId "lasten_makuuhuone"] Off))
|
>>> AFRP.onEvent (callService (light [AreaId "lasten_makuuhuone"] Off))
|
||||||
|
|
||||||
|
|
||||||
|
bedroomButton :: HASS (Event Value) (Event (IkeaButton IkeaGesture))
|
||||||
|
bedroomButton = ikeaQuickButton "event.children_quick_remote_action"
|
||||||
|
|
||||||
|
childrenBedroomButtonController :: HASS (Event Value) ()
|
||||||
|
childrenBedroomButtonController = proc x -> do
|
||||||
|
ev <- bedroomButton >>> traceEvent -< x
|
||||||
|
case ev of
|
||||||
|
Event (OnButton ShortRelease) -> callService (activateScene "scene.lasten_nukkuvalmistelu") -< ()
|
||||||
|
Event (OnButton DoubleClick) -> callService (activateScene "scene.lasten_luku") -< ()
|
||||||
|
Event (OnButton LongClick) -> callService (activateScene "scene.lasten_kirkas") -< ()
|
||||||
|
Event (OffButton _) -> callService (light [AreaId "lasten_makuuhuone"] Off) -< ()
|
||||||
|
_ -> returnA -< ()
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
|
{-# LANGUAGE Arrows #-}
|
||||||
|
module HomeAssistant.Controller.Hallway where
|
||||||
|
|
||||||
|
import HomeAssistant.Controller (Presence (..), HASS, motion, motionToPresence, traceEvent, callServiceDyn, light, Target (..), Light (..))
|
||||||
|
import AFRP (Event (..))
|
||||||
|
import qualified AFRP
|
||||||
|
import Data.Aeson (Value)
|
||||||
|
import Control.Arrow ((>>>), Arrow (..), returnA)
|
||||||
|
import Data.Time (LocalTime(..), TimeOfDay (..))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
hallwayPresence :: HASS (Event Value) Presence
|
||||||
|
hallwayPresence = motion "binary_sensor.hallway_movement_occupancy" >>> motionToPresence 500
|
||||||
|
|
||||||
|
data Lights = LightsOn | LightsOff
|
||||||
|
deriving (Show)
|
||||||
|
|
||||||
|
eventLights :: HASS Presence (Event Lights)
|
||||||
|
eventLights = AFRP.changes >>> arr (fmap presenceLights)
|
||||||
|
where
|
||||||
|
presenceLights Occupied = LightsOn
|
||||||
|
presenceLights Unoccupied = LightsOff
|
||||||
|
|
||||||
|
hallwayLightsController :: HASS (Event Value) ()
|
||||||
|
hallwayLightsController = proc x -> do
|
||||||
|
now <- AFRP.currentTime -< ()
|
||||||
|
p <- hallwayPresence -< x
|
||||||
|
ev <- eventLights -< p
|
||||||
|
traceEvent -< ev
|
||||||
|
case ev of
|
||||||
|
-- The lights entity is a "group of lights" in hass
|
||||||
|
Event LightsOn | lightsAllowed now -> callServiceDyn (light [EntityId "light.hallway_lights"]) -< On Nothing
|
||||||
|
Event LightsOff -> callServiceDyn (light [EntityId "light.hallway_lights"]) -< Off
|
||||||
|
_ -> returnA -< ()
|
||||||
|
where
|
||||||
|
lightsAllowed (LocalTime _ tod) = not (tod > TimeOfDay 1 45 0 && tod < TimeOfDay 5 0 0)
|
||||||
@@ -6,38 +6,17 @@ import AFRP (Event (..))
|
|||||||
import qualified AFRP
|
import qualified AFRP
|
||||||
import Data.Aeson (Value)
|
import Data.Aeson (Value)
|
||||||
import Control.Arrow ((>>>), Arrow (..), returnA)
|
import Control.Arrow ((>>>), Arrow (..), returnA)
|
||||||
import Data.Bool (bool)
|
|
||||||
import GHC.Generics (Generic)
|
|
||||||
import Data.Serialize (Serialize)
|
|
||||||
import Prelude hiding (id)
|
import Prelude hiding (id)
|
||||||
import Data.Time (LocalTime(..), TimeOfDay (..))
|
import Data.Time (LocalTime(..), TimeOfDay (..))
|
||||||
|
|
||||||
|
|
||||||
-- Kitchen has two "presence" sensors. One IKEA motion sensor and one SwitchBot presence sensor
|
-- Kitchen has two "presence" sensors. One IKEA motion sensor and one SwitchBot presence sensor
|
||||||
data Motion = MotionDetected | MotionNotDetected | MotionUnknown
|
|
||||||
deriving (Show, Eq, Generic)
|
|
||||||
|
|
||||||
instance Serialize Motion
|
data Lights = LightsOn | LightsOff | LightsSleep
|
||||||
|
|
||||||
data Lights = LightsOn | LightsOff
|
|
||||||
deriving (Show)
|
deriving (Show)
|
||||||
|
|
||||||
kitchenMotion :: HASS (Event Value) Motion
|
kitchenPresence :: HASS (Event Value) Presence
|
||||||
kitchenMotion = entityBool "binary_sensor.kitchen_movement_occupancy"
|
kitchenPresence = motion "binary_sensor.kitchen_movement_occupancy" >>> motionToPresence 500
|
||||||
>>> arr (fmap (bool MotionNotDetected MotionDetected))
|
|
||||||
>>> traceEvent
|
|
||||||
>>> AFRP.hold MotionUnknown
|
|
||||||
|
|
||||||
|
|
||||||
kitchenPresence :: HASS Motion Presence
|
|
||||||
kitchenPresence = (eventOccupied &&& eventUnoccupied)
|
|
||||||
>>> arr (uncurry AFRP.lMerge) >>> traceEvent
|
|
||||||
>>> AFRP.hold Unoccupied
|
|
||||||
where
|
|
||||||
eventOccupied :: HASS Motion (Event Presence)
|
|
||||||
eventOccupied = arr (== MotionDetected) >>> AFRP.edge >>> arr (fmap (const Occupied))
|
|
||||||
eventUnoccupied :: HASS Motion (Event Presence)
|
|
||||||
eventUnoccupied = arr (== MotionNotDetected) >>> AFRP.waitFor 300 >>> arr (AFRP.tag Unoccupied)
|
|
||||||
|
|
||||||
eventLights :: HASS Presence (Event Lights)
|
eventLights :: HASS Presence (Event Lights)
|
||||||
eventLights = AFRP.changes >>> arr (fmap presenceLights)
|
eventLights = AFRP.changes >>> arr (fmap presenceLights)
|
||||||
@@ -45,10 +24,10 @@ eventLights = AFRP.changes >>> arr (fmap presenceLights)
|
|||||||
presenceLights Occupied = LightsOn
|
presenceLights Occupied = LightsOn
|
||||||
presenceLights Unoccupied = LightsOff
|
presenceLights Unoccupied = LightsOff
|
||||||
|
|
||||||
kitchenMotionController :: HASS (Event Value) ()
|
kitchenCeilingController :: HASS (Event Value) ()
|
||||||
kitchenMotionController = proc x -> do
|
kitchenCeilingController = proc x -> do
|
||||||
now <- AFRP.currentTime -< ()
|
now <- AFRP.currentTime -< ()
|
||||||
p <- kitchenMotion >>> kitchenPresence -< x
|
p <- kitchenPresence -< x
|
||||||
ev <- eventLights -< p
|
ev <- eventLights -< p
|
||||||
traceEvent -< ev
|
traceEvent -< ev
|
||||||
case ev of
|
case ev of
|
||||||
@@ -57,3 +36,52 @@ kitchenMotionController = proc x -> do
|
|||||||
_ -> returnA -< ()
|
_ -> returnA -< ()
|
||||||
where
|
where
|
||||||
lightsAllowed (LocalTime _ tod) = not (tod > TimeOfDay 1 45 0 && tod < TimeOfDay 5 0 0)
|
lightsAllowed (LocalTime _ tod) = not (tod > TimeOfDay 1 45 0 && tod < TimeOfDay 5 0 0)
|
||||||
|
|
||||||
|
|
||||||
|
dinnerLightLevel :: HASS (Event Value) Int
|
||||||
|
dinnerLightLevel = entityRead "sensor.sijainti_keittio_light_level" >>> AFRP.hold 0
|
||||||
|
|
||||||
|
dinnerPresence :: HASS (Event Value) Presence
|
||||||
|
dinnerPresence = presence "binary_sensor.sijainti_keittio_occupancy" >>> AFRP.hold Unoccupied
|
||||||
|
|
||||||
|
dinnerLightEvents :: HASS (Event Value) (Event Lights)
|
||||||
|
dinnerLightEvents =
|
||||||
|
((,) <$> dinnerLightLevel <*> dinnerPresence)
|
||||||
|
>>> arr lightWanted
|
||||||
|
>>> (sleepEvent &&& offEvent &&& onEvent)
|
||||||
|
>>> arr (\(s, (o, n)) -> s `AFRP.lMerge` o `AFRP.lMerge` n)
|
||||||
|
where
|
||||||
|
lightWanted (lightLevel, occ) = occ == Occupied && lightLevel < 13
|
||||||
|
|
||||||
|
onEvent =
|
||||||
|
AFRP.edge
|
||||||
|
>>> arr (fmap (const LightsOn))
|
||||||
|
|
||||||
|
sleepEvent =
|
||||||
|
arr not
|
||||||
|
>>> AFRP.edge
|
||||||
|
>>> arr (fmap (const LightsSleep))
|
||||||
|
|
||||||
|
offEvent =
|
||||||
|
arr not
|
||||||
|
>>> AFRP.waitFor 300
|
||||||
|
>>> arr (fmap (const LightsOff))
|
||||||
|
|
||||||
|
dinnerTableController :: HASS (Event Value) ()
|
||||||
|
dinnerTableController = proc x -> do
|
||||||
|
ev <- dinnerLightEvents -< x
|
||||||
|
case ev of
|
||||||
|
Event LightsOn -> do
|
||||||
|
sleep -< False
|
||||||
|
callServiceDyn (light [EntityId "light.kitchen_dining_large"]) -< On Nothing
|
||||||
|
Event LightsOff -> do
|
||||||
|
sleep -< False
|
||||||
|
callServiceDyn (light [EntityId "light.kitchen_dining_large"]) -< Off
|
||||||
|
Event LightsSleep -> do
|
||||||
|
sleep -< True
|
||||||
|
_ -> returnA -< ()
|
||||||
|
where
|
||||||
|
sleep = callServiceDyn (switch [EntityId "switch.adaptive_lighting_ruokapoydan_valot_sleep_mode"])
|
||||||
|
|
||||||
|
kitchenMotionController :: HASS (Event Value) ()
|
||||||
|
kitchenMotionController = kitchenCeilingController <> dinnerTableController
|
||||||
@@ -0,0 +1,100 @@
|
|||||||
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
|
{-# LANGUAGE Arrows #-}
|
||||||
|
module HomeAssistant.Controller.Livingroom where
|
||||||
|
|
||||||
|
import HomeAssistant.Controller (HASS, Presence (..), Target (..), presence, callServiceDyn, light, Light (..), traceEvent, callService, activateScene)
|
||||||
|
import qualified AFRP
|
||||||
|
import Control.Arrow ((>>>), Arrow (..), returnA)
|
||||||
|
import Data.Aeson (Value)
|
||||||
|
import Prelude hiding ((.))
|
||||||
|
import Control.Category ((.))
|
||||||
|
import Data.Time (LocalTime(..), toGregorian, TimeOfDay (..))
|
||||||
|
|
||||||
|
data Lights = LightsOn | LightsOff
|
||||||
|
deriving (Show)
|
||||||
|
|
||||||
|
livingroomPresence :: HASS (AFRP.Event Value) Presence
|
||||||
|
livingroomPresence = presence "binary_sensor.occupancy_olohuone_derived" >>> AFRP.hold Unoccupied
|
||||||
|
|
||||||
|
eventLights :: HASS Presence (AFRP.Event Lights)
|
||||||
|
eventLights = AFRP.changes >>> arr (fmap presenceLights)
|
||||||
|
where
|
||||||
|
presenceLights Occupied = LightsOn
|
||||||
|
presenceLights Unoccupied = LightsOff
|
||||||
|
|
||||||
|
|
||||||
|
-- There's a small race condition / corner case bug in the og implementation
|
||||||
|
-- I have a couple of lights in the Livingroom
|
||||||
|
--
|
||||||
|
-- - Ceiling ilght
|
||||||
|
-- - "Sofa light", a small reading nook light
|
||||||
|
-- - A few strong lights at a plant corner
|
||||||
|
--
|
||||||
|
-- The plant corner lights are not started by the automation. They are started by a separate schedule
|
||||||
|
-- But, they are turned off by the automation
|
||||||
|
-- So their status gets messed if their schedule turns on while there is nobody in the room
|
||||||
|
--
|
||||||
|
--
|
||||||
|
-- With that in mind, I'll be explicitly controlling the two lights, without any scene shenanigans
|
||||||
|
|
||||||
|
|
||||||
|
lights :: [Target]
|
||||||
|
lights = [EntityId "light.livingroom_ceiling", EntityId "light.livingroom_sofa"]
|
||||||
|
|
||||||
|
livingroomPresenceController :: HASS (AFRP.Event Value) ()
|
||||||
|
livingroomPresenceController = proc x -> do
|
||||||
|
ev <- eventLights . livingroomPresence -< x
|
||||||
|
traceEvent -< ev
|
||||||
|
case ev of
|
||||||
|
AFRP.Event LightsOn -> callServiceDyn (light lights) -< On Nothing
|
||||||
|
AFRP.Event LightsOff -> callServiceDyn (light lights) -< Off
|
||||||
|
_ -> returnA -< ()
|
||||||
|
returnA -< ()
|
||||||
|
|
||||||
|
data Season = Summer | Fall | Winter | Spring
|
||||||
|
deriving Show
|
||||||
|
|
||||||
|
|
||||||
|
season :: HASS a Season
|
||||||
|
season = AFRP.currentTime >>> arr toSeason
|
||||||
|
where
|
||||||
|
toSeason t =
|
||||||
|
case toGregorian (localDay t) of
|
||||||
|
(_, m, _)
|
||||||
|
| m >= 12 || m <= 2 -> Winter
|
||||||
|
| m <= 5 -> Spring
|
||||||
|
| m <= 8 -> Summer
|
||||||
|
| otherwise -> Fall
|
||||||
|
|
||||||
|
tod :: HASS a TimeOfDay
|
||||||
|
tod = AFRP.currentTime >>> arr localTimeOfDay
|
||||||
|
|
||||||
|
data PlantLightMode = Growth | Ambience
|
||||||
|
deriving Show
|
||||||
|
|
||||||
|
plantLightMode :: HASS (Season, TimeOfDay) (AFRP.Event PlantLightMode)
|
||||||
|
plantLightMode =
|
||||||
|
(lightMode &&& ambienceMode) >>> arr (uncurry AFRP.lMerge)
|
||||||
|
where
|
||||||
|
lightMode = arr (uncurry lightWanted) >>> AFRP.edge >>> arr (AFRP.tag Growth)
|
||||||
|
ambienceMode = arr (uncurry ambienceWanted) >>> AFRP.edge >>> arr (AFRP.tag Ambience)
|
||||||
|
-- Full brightness during daytime only non-summer
|
||||||
|
lightWanted :: Season -> TimeOfDay -> Bool
|
||||||
|
lightWanted Summer _ = False
|
||||||
|
lightWanted _ TimeOfDay{todHour=10,todMin=00} = True
|
||||||
|
lightWanted _ _ = False
|
||||||
|
-- Ambience mode is always
|
||||||
|
ambienceWanted :: Season -> TimeOfDay -> Bool
|
||||||
|
ambienceWanted _ TimeOfDay{todHour=20,todMin=0} = True
|
||||||
|
ambienceWanted _ _ = False
|
||||||
|
|
||||||
|
livingroomPlantLights :: HASS (AFRP.Event a) ()
|
||||||
|
livingroomPlantLights = (season &&& tod)
|
||||||
|
>>> plantLightMode
|
||||||
|
>>> setLights
|
||||||
|
where
|
||||||
|
setLights = proc mode -> do
|
||||||
|
case mode of
|
||||||
|
AFRP.Event Growth -> callService (activateScene "scene.olohuoneen_kasvivalo") -< ()
|
||||||
|
AFRP.Event Ambience -> callService (activateScene "scene.kasvivalo_iltatila") -< ()
|
||||||
|
_ -> returnA -< ()
|
||||||
@@ -14,16 +14,15 @@ module HomeAssistant.Runtime
|
|||||||
) where
|
) where
|
||||||
|
|
||||||
import AFRP (Event (..), Mealy (..), Request (..), Auto, stepAutoSerializing, load, DecodedAuto (..))
|
import AFRP (Event (..), Mealy (..), Request (..), Auto, stepAutoSerializing, load, DecodedAuto (..))
|
||||||
import Control.Concurrent.Async (async, waitAny)
|
|
||||||
import Control.Concurrent.STM (atomically, dupTChan, readTChan)
|
import Control.Concurrent.STM (atomically, dupTChan, readTChan)
|
||||||
import Data.Aeson (Value)
|
import Data.Aeson (Value)
|
||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
import Data.Time (getCurrentTime, getCurrentTimeZone)
|
import Data.Time (getCurrentTime, getCurrentTimeZone)
|
||||||
import Data.Void (Void, absurd)
|
import Data.Void (Void)
|
||||||
import HomeAssistant.Controller (HASS, HASSEff (..))
|
import HomeAssistant.Controller (HASS, HASSEff (..))
|
||||||
import HomeAssistant.Runtime.Bus
|
import HomeAssistant.Runtime.Bus
|
||||||
import HomeAssistant.Runtime.Connection (readerAction, writerAction)
|
import HomeAssistant.Runtime.Flags (loadFlags)
|
||||||
import HomeAssistant.Runtime.Supervisor (defaultBackoff, supervised)
|
import HomeAssistant.Runtime.Connection (writerAction, readerAction)
|
||||||
import Network.Socket (withSocketsDo)
|
import Network.Socket (withSocketsDo)
|
||||||
import System.Environment (getEnv, lookupEnv)
|
import System.Environment (getEnv, lookupEnv)
|
||||||
import HomeAssistant.Controller.Bedroom (bedroomPresenceController, bedroomButtonController, bedroomDrawerController, humidifierController)
|
import HomeAssistant.Controller.Bedroom (bedroomPresenceController, bedroomButtonController, bedroomDrawerController, humidifierController)
|
||||||
@@ -31,39 +30,48 @@ import Data.UUID (UUID, toText)
|
|||||||
import qualified Data.UUID.V4 as UUID.V4
|
import qualified Data.UUID.V4 as UUID.V4
|
||||||
import Katip (runKatipT, logF, sl, Severity (..), ls, Namespace (Namespace), runKatipContextT)
|
import Katip (runKatipT, logF, sl, Severity (..), ls, Namespace (Namespace), runKatipContextT)
|
||||||
import Control.Monad.IO.Class (liftIO, MonadIO)
|
import Control.Monad.IO.Class (liftIO, MonadIO)
|
||||||
import HomeAssistant.Controller.Ruuvi (ruuviController)
|
import HomeAssistant.Controller.Children (schoolLightController, childrenBedroomButtonController)
|
||||||
import HomeAssistant.Controller.Children (schoolLightController)
|
|
||||||
import Data.Maybe (fromMaybe)
|
import Data.Maybe (fromMaybe)
|
||||||
import qualified System.Metrics
|
import qualified System.Metrics
|
||||||
import qualified HomeAssistant.Runtime.Metrics
|
import qualified HomeAssistant.Runtime.Metrics
|
||||||
import System.FilePath ((</>))
|
import System.FilePath ((</>))
|
||||||
|
import Text.Read (readMaybe)
|
||||||
import HomeAssistant.Controller.Kitchen (kitchenMotionController)
|
import HomeAssistant.Controller.Kitchen (kitchenMotionController)
|
||||||
|
import HomeAssistant.Controller.Livingroom (livingroomPresenceController, livingroomPlantLights)
|
||||||
|
import HomeAssistant.Runtime.RateLimit (slidingWindowLimiter, registerRateLimitMetrics)
|
||||||
|
import UnliftIO.Async
|
||||||
|
import HomeAssistant.Runtime.Supervisor (supervised)
|
||||||
|
import HomeAssistant.Controller.Hallway (hallwayLightsController)
|
||||||
|
import qualified HttpServer
|
||||||
|
|
||||||
step :: (MonadIO m) => FilePath -> UUID -> Auto m a b -> a -> m (b, Auto m a b)
|
step :: (MonadIO m) => FilePath -> T.Text -> UUID -> Auto m a b -> a -> m (b, Auto m a b)
|
||||||
step path trace st a = do
|
step path name trace st a = do
|
||||||
now <- liftIO getCurrentTime
|
now <- liftIO getCurrentTime
|
||||||
tz <- liftIO getCurrentTimeZone
|
tz <- liftIO getCurrentTimeZone
|
||||||
let req = Request now tz trace
|
let req = Request now tz trace name
|
||||||
stepAutoSerializing path st req a
|
stepAutoSerializing path st req a
|
||||||
|
|
||||||
data Controller = forall b. Controller T.Text (HASS (Event Value) b) Bool
|
data Controller = forall b. Controller T.Text (HASS (Event Value) b)
|
||||||
|
|
||||||
controllers :: [Controller]
|
controllers :: [Controller]
|
||||||
controllers =
|
controllers =
|
||||||
[ Controller "bedroom-presence" bedroomPresenceController False
|
[ Controller "bedroom-presence" bedroomPresenceController
|
||||||
, Controller "bedroom-button" bedroomButtonController False -- This works but leaving for vacation
|
, Controller "bedroom-button" bedroomButtonController
|
||||||
, Controller "bedroom-drawer" bedroomDrawerController True
|
, Controller "bedroom-drawer" bedroomDrawerController
|
||||||
, Controller "bedroom-humidifier" humidifierController True
|
, Controller "bedroom-humidifier" humidifierController
|
||||||
, Controller "ruuvi-controller" ruuviController False
|
, Controller "school-light-controller" schoolLightController
|
||||||
, Controller "school-light-controller" schoolLightController True
|
, Controller "kitchen-motion-controller" kitchenMotionController
|
||||||
, Controller "kitchen-motion-controller" kitchenMotionController True
|
, Controller "livingroom-presence" livingroomPresenceController
|
||||||
|
, Controller "hallway-motion-controller" hallwayLightsController
|
||||||
|
, Controller "children-button" childrenBedroomButtonController
|
||||||
|
, Controller "livingroom-plants" livingroomPlantLights
|
||||||
]
|
]
|
||||||
|
|
||||||
-- | Steps the machine for every inbound message; service calls go to the
|
-- | Steps the machine for every inbound message; service calls go to the
|
||||||
-- bus. A restart re-dups the inbound channel and starts from the machine's
|
-- bus. A restart re-dups the inbound channel and starts from the machine's
|
||||||
-- initial state; messages broadcast during the restart window are lost.
|
-- initial state; messages broadcast during the restart window are lost.
|
||||||
runController :: FilePath -> Bus -> Controller -> IO Void
|
runController :: MonadIO m => FilePath -> Bus -> Controller -> m Void
|
||||||
runController rootDir bus (Controller name machine _enabled) = do
|
runController rootDir bus (Controller name machine ) = liftIO $ do
|
||||||
inbound <- atomically (dupTChan (busInbound bus))
|
inbound <- atomically (dupTChan (busInbound bus))
|
||||||
let ns = Namespace [name]
|
let ns = Namespace [name]
|
||||||
let workerDefinition = runMealy machine (runKatipContextT (busLogEnv bus) () ns . channelHassEval bus)
|
let workerDefinition = runMealy machine (runKatipContextT (busLogEnv bus) () ns . channelHassEval bus)
|
||||||
@@ -76,30 +84,44 @@ runController rootDir bus (Controller name machine _enabled) = do
|
|||||||
go path inbound f = do
|
go path inbound f = do
|
||||||
msg <- atomically (readTChan inbound)
|
msg <- atomically (readTChan inbound)
|
||||||
uuid <- UUID.V4.nextRandom
|
uuid <- UUID.V4.nextRandom
|
||||||
(_, next) <- step path uuid f msg
|
(_, next) <- step path name uuid f msg
|
||||||
go path inbound next
|
go path inbound next
|
||||||
|
|
||||||
|
lookupPort :: IO Int
|
||||||
|
lookupPort = do
|
||||||
|
m <- lookupEnv "HA_METRICS_PORT"
|
||||||
|
case m of
|
||||||
|
Nothing -> pure 8124
|
||||||
|
Just s -> case readMaybe s of
|
||||||
|
Just p | p >= 1 && p <= 65535 -> pure p
|
||||||
|
_ -> fail ("HA_METRICS_PORT must be a port in 1..65535: " <> s)
|
||||||
|
|
||||||
defaultMain :: IO ()
|
defaultMain :: IO ()
|
||||||
defaultMain = withSocketsDo $ do
|
defaultMain = withSocketsDo $ do
|
||||||
severity <- maybe InfoS (const DebugS) <$> lookupEnv "HA_DEBUG"
|
severity <- maybe InfoS (const DebugS) <$> lookupEnv "HA_DEBUG"
|
||||||
withBus severity $ \bus -> do
|
store <- System.Metrics.newStore
|
||||||
|
System.Metrics.registerGcMetrics store
|
||||||
|
appMetrics <- HomeAssistant.Runtime.Metrics.registerAppMetrics store
|
||||||
|
rateLimitMetrics <- registerRateLimitMetrics store
|
||||||
|
rootPath <- fromMaybe "/tmp/" <$> lookupEnv "HA_LIB_DIR"
|
||||||
|
let allNames = map (\(Controller n _) -> n) controllers
|
||||||
|
flags <- loadFlags rootPath allNames
|
||||||
|
withBus severity appMetrics flags $ \bus -> do
|
||||||
token <- getEnv "HA_TOKEN"
|
token <- getEnv "HA_TOKEN"
|
||||||
host <- getEnv "HA_HOST"
|
host <- getEnv "HA_HOST"
|
||||||
rootPath <- fromMaybe "/tmp/" <$> lookupEnv "HA_LIB_DIR"
|
let rrdPath = rootPath </> "hass-controller.rrd"
|
||||||
store <- System.Metrics.newStore
|
|
||||||
System.Metrics.registerGcMetrics store
|
|
||||||
rrdPath <- fromMaybe "hass-controller.rrd" <$> lookupEnv "HA_RRD_PATH"
|
|
||||||
rrdtool <- fromMaybe "rrdtool" <$> lookupEnv "HA_RRDTOOL"
|
rrdtool <- fromMaybe "rrdtool" <$> lookupEnv "HA_RRDTOOL"
|
||||||
let active = [c | c@(Controller _ _ True) <- controllers]
|
metricsPort <- lookupPort
|
||||||
ents = foldMap (\(Controller _ m _) -> entities m) active
|
writerLimiter <- slidingWindowLimiter rateLimitMetrics 10 20
|
||||||
|
let ents = foldMap (\(Controller _ m) -> entities m) controllers
|
||||||
workers =
|
workers =
|
||||||
[ ("reader", readerAction host 8123 token ents bus)
|
[ ("reader", readerAction host 8123 token ents bus)
|
||||||
, ("writer", writerAction bus)
|
, ("writer", writerAction writerLimiter bus)
|
||||||
] ++ [ (name, runController rootPath bus c) | c@(Controller name _ True) <- controllers ]
|
, ("metrics", HomeAssistant.Runtime.Metrics.metricsAction store rrdPath rrdtool)
|
||||||
++ [("metrics", HomeAssistant.Runtime.Metrics.metricsAction store rrdPath rrdtool)]
|
, ("metrics-http", HttpServer.runHttpServer (busLogEnv bus) rrdPath rrdtool (busFlags bus) metricsPort)
|
||||||
as <- mapM (\(name, act) -> async (supervised name defaultBackoff act)) workers
|
] ++ [ (name, runController rootPath bus c) | c@(Controller name _) <- controllers ]
|
||||||
(_, v) <- waitAny as
|
runKatipContextT (busLogEnv bus) () mempty $
|
||||||
absurd v
|
mapConcurrently_ (uncurry supervised) workers
|
||||||
|
|
||||||
dryRunHassEval :: Namespace -> Bus -> HASSEff a -> IO a
|
dryRunHassEval :: Namespace -> Bus -> HASSEff a -> IO a
|
||||||
dryRunHassEval ns bus = \case
|
dryRunHassEval ns bus = \case
|
||||||
|
|||||||
@@ -6,6 +6,8 @@ module HomeAssistant.Runtime.Bus
|
|||||||
, CallIdGen(..)
|
, CallIdGen(..)
|
||||||
, mkCallIdGen
|
, mkCallIdGen
|
||||||
, withBus
|
, withBus
|
||||||
|
, recordInbound
|
||||||
|
, recordOutbound
|
||||||
, channelHassEval
|
, channelHassEval
|
||||||
) where
|
) where
|
||||||
|
|
||||||
@@ -21,13 +23,17 @@ import Control.Concurrent.STM
|
|||||||
import Data.Aeson (Value)
|
import Data.Aeson (Value)
|
||||||
import Data.IORef (atomicModifyIORef', newIORef)
|
import Data.IORef (atomicModifyIORef', newIORef)
|
||||||
import HomeAssistant.Controller (HASSEff (..), Service)
|
import HomeAssistant.Controller (HASSEff (..), Service)
|
||||||
|
import HomeAssistant.Runtime.Metrics (AppMetrics (..))
|
||||||
import Network.WebSockets (Connection)
|
import Network.WebSockets (Connection)
|
||||||
import Katip (LogEnv, closeScribes, mkHandleScribe, ColorStrategy (..), permitItem, Severity (..), Verbosity (V2), registerScribe, defaultScribeSettings, initLogEnv, ls, sl, logFM, katipAddContext, KatipContext)
|
import Katip (LogEnv, closeScribes, mkHandleScribe, ColorStrategy (..), permitItem, Severity (..), Verbosity (V2), registerScribe, defaultScribeSettings, initLogEnv, ls, sl, logFM, katipAddContext, KatipContext)
|
||||||
import Control.Exception (bracket)
|
import Control.Exception (bracket)
|
||||||
import System.IO (stdout)
|
import System.IO (stdout)
|
||||||
|
import System.Metrics.Counter (inc)
|
||||||
import Data.UUID (toText)
|
import Data.UUID (toText)
|
||||||
import AFRP (Request(..), Event(..))
|
import AFRP (Request(..), Event(..))
|
||||||
|
import Control.Monad (when)
|
||||||
import Control.Monad.IO.Class (MonadIO, liftIO)
|
import Control.Monad.IO.Class (MonadIO, liftIO)
|
||||||
|
import HomeAssistant.Runtime.Flags (Flags, isEnabled)
|
||||||
|
|
||||||
-- | Shared runtime state: inbound is a broadcast channel (controllers
|
-- | Shared runtime state: inbound is a broadcast channel (controllers
|
||||||
-- read from 'dupTChan' copies), outbound queues service calls for the
|
-- read from 'dupTChan' copies), outbound queues service calls for the
|
||||||
@@ -38,10 +44,12 @@ data Bus = Bus
|
|||||||
, busConn :: TVar (Maybe Connection)
|
, busConn :: TVar (Maybe Connection)
|
||||||
, busGen :: CallIdGen
|
, busGen :: CallIdGen
|
||||||
, busLogEnv :: LogEnv
|
, busLogEnv :: LogEnv
|
||||||
|
, busMetrics :: AppMetrics
|
||||||
|
, busFlags :: Flags
|
||||||
}
|
}
|
||||||
|
|
||||||
withBus :: Severity -> (Bus -> IO a) -> IO a
|
withBus :: Severity -> AppMetrics -> Flags -> (Bus -> IO a) -> IO a
|
||||||
withBus severity callback = do
|
withBus severity metrics flags callback = do
|
||||||
handleScribe <- mkHandleScribe ColorIfTerminal stdout (permitItem severity) V2
|
handleScribe <- mkHandleScribe ColorIfTerminal stdout (permitItem severity) V2
|
||||||
let makeLogEnv = registerScribe "stdout" handleScribe defaultScribeSettings =<< initLogEnv "hass-controller" "production"
|
let makeLogEnv = registerScribe "stdout" handleScribe defaultScribeSettings =<< initLogEnv "hass-controller" "production"
|
||||||
-- closeScribes will stop accepting new logs, flush existing ones and clean up resources
|
-- closeScribes will stop accepting new logs, flush existing ones and clean up resources
|
||||||
@@ -52,13 +60,22 @@ withBus severity callback = do
|
|||||||
<*> newTVarIO Nothing
|
<*> newTVarIO Nothing
|
||||||
<*> mkCallIdGen 0
|
<*> mkCallIdGen 0
|
||||||
<*> pure le
|
<*> pure le
|
||||||
|
<*> pure metrics
|
||||||
|
<*> pure flags
|
||||||
callback bus
|
callback bus
|
||||||
|
|
||||||
|
recordInbound :: Bus -> IO ()
|
||||||
|
recordInbound bus = inc (amTriggersIn (busMetrics bus))
|
||||||
|
|
||||||
|
recordOutbound :: Bus -> IO ()
|
||||||
|
recordOutbound bus = inc (amServicesOut (busMetrics bus))
|
||||||
|
|
||||||
channelHassEval :: (MonadIO m, KatipContext m) => Bus -> HASSEff a -> m a
|
channelHassEval :: (MonadIO m, KatipContext m) => Bus -> HASSEff a -> m a
|
||||||
channelHassEval bus = \case
|
channelHassEval bus = \case
|
||||||
CallService req svc -> katipAddContext (sl "traceId" (toText (requestTraceId req))) $ do
|
CallService req svc -> katipAddContext (sl "traceId" (toText (requestTraceId req))) $ do
|
||||||
logFM DebugS (ls $ show svc)
|
logFM DebugS (ls $ show svc)
|
||||||
liftIO $ atomically $ writeTChan (busOutbound bus) (req, svc)
|
enabled <- isEnabled (busFlags bus) (requestHandler req)
|
||||||
|
when enabled $ liftIO $ atomically $ writeTChan (busOutbound bus) (req, svc)
|
||||||
Debug x -> logFM DebugS (ls $ show x)
|
Debug x -> logFM DebugS (ls $ show x)
|
||||||
Trace req x -> katipAddContext (sl "traceId" (toText (requestTraceId req))) $
|
Trace req x -> katipAddContext (sl "traceId" (toText (requestTraceId req))) $
|
||||||
logFM InfoS (ls $ show x)
|
logFM InfoS (ls $ show x)
|
||||||
|
|||||||
@@ -5,16 +5,14 @@ module HomeAssistant.Runtime.Connection
|
|||||||
( readerAction
|
( readerAction
|
||||||
, writerAction
|
, writerAction
|
||||||
, encodeService
|
, encodeService
|
||||||
, dedupeBatch
|
, isTriggerEvent
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Control.Concurrent.STM
|
import Control.Concurrent.STM
|
||||||
( TChan
|
( atomically
|
||||||
, atomically
|
|
||||||
, readTChan
|
, readTChan
|
||||||
, readTVar
|
, readTVar
|
||||||
, retry
|
, retry
|
||||||
, tryReadTChan
|
|
||||||
, writeTChan
|
, writeTChan
|
||||||
, writeTVar
|
, writeTVar
|
||||||
)
|
)
|
||||||
@@ -23,11 +21,9 @@ import Control.Concurrent (threadDelay)
|
|||||||
import Control.Exception (onException)
|
import Control.Exception (onException)
|
||||||
import Control.Exception.Annotated (throw)
|
import Control.Exception.Annotated (throw)
|
||||||
import Control.Lens ((^?))
|
import Control.Lens ((^?))
|
||||||
import Control.Monad (forever, forM_)
|
import Control.Monad (forever, forM_, when)
|
||||||
import Data.Aeson (Value, eitherDecode, encode, object, (.=))
|
import Data.Aeson (Value, eitherDecode, encode, object, (.=))
|
||||||
import Data.Aeson.Lens (key, _String)
|
import Data.Aeson.Lens (key, _String)
|
||||||
import Data.List (sort)
|
|
||||||
import qualified Data.Map.Strict as M
|
|
||||||
import qualified Data.Set as S
|
import qualified Data.Set as S
|
||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
import Data.Void (Void)
|
import Data.Void (Void)
|
||||||
@@ -35,15 +31,18 @@ import HomeAssistant.Controller (Service (..), Target (..))
|
|||||||
import HomeAssistant.Runtime.Bus
|
import HomeAssistant.Runtime.Bus
|
||||||
import HomeAssistant.Runtime.Supervisor (Fatal (..))
|
import HomeAssistant.Runtime.Supervisor (Fatal (..))
|
||||||
import qualified Network.WebSockets as WS
|
import qualified Network.WebSockets as WS
|
||||||
import Katip (runKatipContextT, sl, logFM, Severity (..), ls)
|
import Katip (sl, logFM, Severity (..), ls, KatipContext, katipAddNamespace, katipAddContext)
|
||||||
import Data.UUID (toText)
|
import Data.UUID (toText)
|
||||||
import AFRP (Request(..), Event(..))
|
import AFRP (Request(..), Event(..))
|
||||||
|
import Control.Monad.IO.Class (liftIO, MonadIO)
|
||||||
|
import HomeAssistant.Runtime.RateLimit (RateLimited, RateLimiter, runRateLimited)
|
||||||
|
import Control.Monad.Catch (MonadCatch)
|
||||||
|
|
||||||
-- | Connect, authenticate, subscribe, then receive and broadcast forever.
|
-- | Connect, authenticate, subscribe, then receive and broadcast forever.
|
||||||
-- Restarting this action reconnects. All setup sends happen before the
|
-- Restarting this action reconnects. All setup sends happen before the
|
||||||
-- connection is published in the bus, so only the writer sends afterwards.
|
-- connection is published in the bus, so only the writer sends afterwards.
|
||||||
readerAction :: String -> Int -> String -> S.Set T.Text -> Bus -> IO Void
|
readerAction :: MonadIO m => String -> Int -> String -> S.Set T.Text -> Bus -> m Void
|
||||||
readerAction host port token ents bus =
|
readerAction host port token ents bus = liftIO $
|
||||||
WS.runClient host port "/api/websocket" $ \conn -> do
|
WS.runClient host port "/api/websocket" $ \conn -> do
|
||||||
handshake conn token
|
handshake conn token
|
||||||
subscribe bus conn ents
|
subscribe bus conn ents
|
||||||
@@ -69,6 +68,9 @@ expectType expected msg =
|
|||||||
Just t | t == expected -> pure ()
|
Just t | t == expected -> pure ()
|
||||||
_ -> throw (Fatal $ "expected " <> expected <> ", got: " <> T.pack (show msg))
|
_ -> throw (Fatal $ "expected " <> expected <> ", got: " <> T.pack (show msg))
|
||||||
|
|
||||||
|
isTriggerEvent :: Value -> Bool
|
||||||
|
isTriggerEvent v = v ^? key "type" . _String == Just "event"
|
||||||
|
|
||||||
subscribe :: Bus -> WS.Connection -> S.Set T.Text -> IO ()
|
subscribe :: Bus -> WS.Connection -> S.Set T.Text -> IO ()
|
||||||
subscribe bus conn ents =
|
subscribe bus conn ents =
|
||||||
forM_ (S.toList ents) $ \entityId -> do
|
forM_ (S.toList ents) $ \entityId -> do
|
||||||
@@ -96,7 +98,9 @@ receiveLoop bus conn = forever $ do
|
|||||||
Left () -> atomically $ writeTChan (busInbound bus) Tick
|
Left () -> atomically $ writeTChan (busInbound bus) Tick
|
||||||
Right msg -> case eitherDecode msg of
|
Right msg -> case eitherDecode msg of
|
||||||
Left err -> putStrLn $ "[reader] skipping undecodable message: " <> err
|
Left err -> putStrLn $ "[reader] skipping undecodable message: " <> err
|
||||||
Right v -> atomically $ writeTChan (busInbound bus) (Event v)
|
Right v -> do
|
||||||
|
when (isTriggerEvent v) (recordInbound bus)
|
||||||
|
atomically $ writeTChan (busInbound bus) (Event v)
|
||||||
|
|
||||||
receiveJSON :: WS.Connection -> IO Value
|
receiveJSON :: WS.Connection -> IO Value
|
||||||
receiveJSON conn = do
|
receiveJSON conn = do
|
||||||
@@ -105,40 +109,23 @@ receiveJSON conn = do
|
|||||||
Left err -> throw (Fatal $ "Invalid JSON from Home Assistant: " <> T.pack err)
|
Left err -> throw (Fatal $ "Invalid JSON from Home Assistant: " <> T.pack err)
|
||||||
Right x -> pure x
|
Right x -> pure x
|
||||||
|
|
||||||
-- | Floor between sends within a batch: 100ms, so a many-distinct-target
|
|
||||||
-- flood still caps at ~10 sends/sec even after dedupe.
|
|
||||||
minInterval :: Int
|
|
||||||
minInterval = 100000
|
|
||||||
|
|
||||||
-- | Non-blocking drain of everything queued on a channel. Returns items
|
|
||||||
-- oldest-first (FIFO from the channel), so prepending the blocking
|
|
||||||
-- `readTChan` item keeps the whole batch oldest-first for `dedupeBatch`.
|
|
||||||
drainTry :: TChan a -> IO [a]
|
|
||||||
drainTry chan = go []
|
|
||||||
where
|
|
||||||
go acc = do
|
|
||||||
m <- atomically $ tryReadTChan chan
|
|
||||||
case m of
|
|
||||||
Nothing -> pure (reverse acc)
|
|
||||||
Just x -> go (x : acc)
|
|
||||||
|
|
||||||
sendWithId :: Bus -> WS.Connection -> Request -> Service -> IO ()
|
sendWithId :: (MonadIO m, KatipContext m) => Bus -> WS.Connection -> Service -> RateLimited m ()
|
||||||
sendWithId bus conn request svc = do
|
sendWithId bus conn svc = katipAddNamespace "connection" $ do
|
||||||
callId <- generateCallId (busGen bus)
|
callId <- liftIO $ generateCallId (busGen bus)
|
||||||
let textData = encode $ encodeService callId svc
|
let textData = encode $ encodeService callId svc
|
||||||
runKatipContextT (busLogEnv bus) (sl "traceId" (toText (requestTraceId request))) "connection" $
|
logFM DebugS (ls textData)
|
||||||
logFM DebugS (ls textData)
|
liftIO $ recordOutbound bus
|
||||||
WS.sendTextData conn textData
|
liftIO $ WS.sendTextData conn textData
|
||||||
|
|
||||||
writerAction :: Bus -> IO Void
|
writerAction :: (MonadIO m, KatipContext m, MonadCatch m) => RateLimiter -> Bus -> m Void
|
||||||
writerAction bus = forever $ do
|
writerAction rateLimiter bus = forever $ do
|
||||||
first <- atomically $ readTChan (busOutbound bus)
|
(request, svc) <- liftIO $ atomically $ readTChan (busOutbound bus)
|
||||||
rest <- drainTry (busOutbound bus)
|
katipAddContext (sl "traceId" (toText (requestTraceId request))) $ do
|
||||||
let deduped = dedupeBatch (first : rest)
|
conn <- liftIO $ atomically $ readTVar (busConn bus) >>= maybe retry pure
|
||||||
conn <- atomically $ readTVar (busConn bus) >>= maybe retry pure
|
-- runRateLimited rateLimiter $ sendWithId bus conn request svc
|
||||||
forM_ deduped $ \(request, svc) -> do
|
runRateLimited rateLimiter $ sendWithId bus conn svc
|
||||||
sendWithId bus conn request svc
|
|
||||||
threadDelay minInterval
|
|
||||||
|
|
||||||
encodeService :: Int -> Service -> Value
|
encodeService :: Int -> Service -> Value
|
||||||
encodeService callId Service{..} = object $
|
encodeService callId Service{..} = object $
|
||||||
@@ -149,17 +136,6 @@ encodeService callId Service{..} = object $
|
|||||||
, "target" .= targetObject serviceTarget
|
, "target" .= targetObject serviceTarget
|
||||||
] <> maybe [] (\d -> ["service_data" .= d]) serviceData
|
] <> maybe [] (\d -> ["service_data" .= d]) serviceData
|
||||||
|
|
||||||
-- | Collapse a drained batch of outbound calls: the newest call per
|
|
||||||
-- `(domain, service, sorted-targets)` survives; older duplicates are
|
|
||||||
-- dropped. `serviceData` is not part of the key, so a newer `turn_on`
|
|
||||||
-- with different brightness supersedes an older one to the same target.
|
|
||||||
dedupeBatch :: [(Request, Service)] -> [(Request, Service)]
|
|
||||||
dedupeBatch = M.elems . foldl' ins M.empty
|
|
||||||
where
|
|
||||||
ins m (req, svc) = M.insert (dedupeKey svc) (req, svc) m
|
|
||||||
|
|
||||||
dedupeKey :: Service -> (T.Text, T.Text, [Target])
|
|
||||||
dedupeKey Service{..} = (serviceDomain, serviceName, sort serviceTarget)
|
|
||||||
|
|
||||||
-- | A single target encodes as a scalar; multiple encode as a list. Empty
|
-- | A single target encodes as a scalar; multiple encode as a list. Empty
|
||||||
-- lists are omitted so Home Assistant receives only populated keys.
|
-- lists are omitted so Home Assistant receives only populated keys.
|
||||||
|
|||||||
@@ -0,0 +1,90 @@
|
|||||||
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
|
|
||||||
|
module HomeAssistant.Runtime.Flags
|
||||||
|
( Name
|
||||||
|
, Flags(..)
|
||||||
|
, loadFlags
|
||||||
|
, isEnabled
|
||||||
|
, setEnabled
|
||||||
|
, allFlags
|
||||||
|
, validNames
|
||||||
|
) where
|
||||||
|
|
||||||
|
import Control.Concurrent.MVar (MVar, newMVar, withMVar)
|
||||||
|
import Control.Concurrent.STM (TVar, atomically, newTVarIO, readTVarIO, stateTVar)
|
||||||
|
import Control.Exception (IOException, handle)
|
||||||
|
import Control.Monad.IO.Class (MonadIO, liftIO)
|
||||||
|
import qualified Conduit as C
|
||||||
|
import Conduit ((.|))
|
||||||
|
import Data.Aeson (decodeFileStrict', encode)
|
||||||
|
import qualified Data.ByteString.Lazy as BL
|
||||||
|
import Data.Map.Strict (Map)
|
||||||
|
import qualified Data.Map.Strict as M
|
||||||
|
import Data.Set (Set)
|
||||||
|
import qualified Data.Set as S
|
||||||
|
import Data.Text (Text)
|
||||||
|
import System.FilePath ((</>))
|
||||||
|
import System.IO (hPutStrLn, stderr)
|
||||||
|
import System.IO.Error (isDoesNotExistError)
|
||||||
|
|
||||||
|
type Name = Text
|
||||||
|
|
||||||
|
data Flags = Flags
|
||||||
|
{ flagsState :: TVar (Map Name Bool)
|
||||||
|
, flagsLock :: MVar ()
|
||||||
|
, flagsPath :: FilePath
|
||||||
|
, flagsValidNames :: Set Text
|
||||||
|
}
|
||||||
|
|
||||||
|
loadFlags :: MonadIO m => FilePath -> [Name] -> m Flags
|
||||||
|
loadFlags rootPath allNames = liftIO $ do
|
||||||
|
let path = rootPath </> "handlers.json"
|
||||||
|
fileMap <- readFlagsFile path
|
||||||
|
let defaults = M.fromList [(n, True) | n <- allNames]
|
||||||
|
initial = M.unionWith (\fileVal _default -> fileVal) fileMap defaults
|
||||||
|
tv <- newTVarIO initial
|
||||||
|
lock <- newMVar ()
|
||||||
|
pure Flags
|
||||||
|
{ flagsState = tv
|
||||||
|
, flagsLock = lock
|
||||||
|
, flagsPath = path
|
||||||
|
, flagsValidNames = S.fromList allNames
|
||||||
|
}
|
||||||
|
|
||||||
|
readFlagsFile :: FilePath -> IO (Map Name Bool)
|
||||||
|
readFlagsFile path =
|
||||||
|
handle onMissingOrUnreadable $ do
|
||||||
|
decoded <- decodeFileStrict' path
|
||||||
|
case decoded of
|
||||||
|
Nothing -> do
|
||||||
|
hPutStrLn stderr ("handlers.json is unparseable; treating all handlers as enabled: " <> path)
|
||||||
|
pure mempty
|
||||||
|
Just m -> pure (maybe mempty id m)
|
||||||
|
where
|
||||||
|
onMissingOrUnreadable :: IOException -> IO (Map Name Bool)
|
||||||
|
onMissingOrUnreadable e
|
||||||
|
| isDoesNotExistError e = pure mempty
|
||||||
|
| otherwise = do
|
||||||
|
hPutStrLn stderr ("could not read handlers.json (" <> show e <> "); treating all handlers as enabled")
|
||||||
|
pure mempty
|
||||||
|
|
||||||
|
isEnabled :: MonadIO m => Flags -> Name -> m Bool
|
||||||
|
isEnabled flags name = liftIO $
|
||||||
|
M.findWithDefault True name <$> readTVarIO (flagsState flags)
|
||||||
|
|
||||||
|
allFlags :: MonadIO m => Flags -> m (Map Name Bool)
|
||||||
|
allFlags flags = liftIO $ readTVarIO (flagsState flags)
|
||||||
|
|
||||||
|
setEnabled :: MonadIO m => Flags -> Name -> Bool -> m ()
|
||||||
|
setEnabled Flags{flagsState, flagsLock, flagsPath} name val =
|
||||||
|
liftIO $ withMVar flagsLock $ \_ -> do
|
||||||
|
m <- atomically $ stateTVar flagsState $ \cur ->
|
||||||
|
let next = M.insert name val cur in (next, next)
|
||||||
|
safeWrite flagsPath m
|
||||||
|
|
||||||
|
validNames :: Flags -> Set Text
|
||||||
|
validNames = flagsValidNames
|
||||||
|
|
||||||
|
safeWrite :: FilePath -> Map Name Bool -> IO ()
|
||||||
|
safeWrite path m = C.runResourceT $ C.runConduit $
|
||||||
|
C.yieldMany [BL.toStrict (encode m)] .| C.sinkFileCautious path
|
||||||
@@ -0,0 +1,281 @@
|
|||||||
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
|
|
||||||
|
module HomeAssistant.Runtime.Graphing
|
||||||
|
( Range (..)
|
||||||
|
, parseRange
|
||||||
|
, rangeStart
|
||||||
|
, Series (..)
|
||||||
|
, GraphSpec (..)
|
||||||
|
, graphs
|
||||||
|
, lookupGraph
|
||||||
|
, parseNameParam
|
||||||
|
, buildGraphArgs
|
||||||
|
, readProcessBytes
|
||||||
|
, Graph(..)
|
||||||
|
, renderGraph
|
||||||
|
) where
|
||||||
|
|
||||||
|
import Control.Exception (evaluate)
|
||||||
|
import Control.Monad.IO.Class (liftIO, MonadIO)
|
||||||
|
import qualified Data.ByteString as BS
|
||||||
|
import Data.Text (Text)
|
||||||
|
import qualified Data.Text as T
|
||||||
|
import System.Exit (ExitCode (..))
|
||||||
|
import System.IO (hGetContents)
|
||||||
|
import System.Process (CreateProcess (..), StdStream (..), proc, waitForProcess, withCreateProcess)
|
||||||
|
import Servant.API (FromHttpApiData (..), ToHttpApiData (..))
|
||||||
|
|
||||||
|
data Range = TenMinutes | OneHour | OneDay | OneWeek | OneMonth
|
||||||
|
deriving (Eq, Show, Bounded, Enum)
|
||||||
|
|
||||||
|
rangeSuffix :: Range -> String
|
||||||
|
rangeSuffix TenMinutes = "10m"
|
||||||
|
rangeSuffix OneHour = "1h"
|
||||||
|
rangeSuffix OneDay = "24h"
|
||||||
|
rangeSuffix OneWeek = "7d"
|
||||||
|
rangeSuffix OneMonth = "30d"
|
||||||
|
|
||||||
|
parseRange :: Text -> Maybe Range
|
||||||
|
parseRange t = lookup t [(T.pack (rangeSuffix r), r) | r <- [minBound .. maxBound]]
|
||||||
|
|
||||||
|
instance ToHttpApiData Range where
|
||||||
|
toQueryParam = T.pack . rangeSuffix
|
||||||
|
|
||||||
|
instance FromHttpApiData Range where
|
||||||
|
parseQueryParam x = maybe (Left ("Could not parse '" <> x <> "' as Range")) Right $ parseRange x
|
||||||
|
|
||||||
|
rangeStart :: Range -> String
|
||||||
|
rangeStart r = "end-" ++ rangeSuffix r
|
||||||
|
|
||||||
|
data CF = Average | Minimum | Maximum | Last
|
||||||
|
deriving (Eq, Show)
|
||||||
|
|
||||||
|
data Source = Source
|
||||||
|
{ srcDs :: String
|
||||||
|
, srcCF :: CF
|
||||||
|
}
|
||||||
|
deriving (Eq, Show)
|
||||||
|
|
||||||
|
data Transform
|
||||||
|
= Identity
|
||||||
|
| Multiply Double
|
||||||
|
deriving (Eq, Show)
|
||||||
|
|
||||||
|
data Series = Series
|
||||||
|
{ serSource :: Source
|
||||||
|
, serTransform :: Transform
|
||||||
|
, serAlias :: String
|
||||||
|
, serColor :: String
|
||||||
|
, serLabel :: String
|
||||||
|
}
|
||||||
|
deriving (Eq, Show)
|
||||||
|
|
||||||
|
data Plot
|
||||||
|
= Line Int Series
|
||||||
|
| Area Series
|
||||||
|
deriving (Eq, Show)
|
||||||
|
|
||||||
|
data GraphSpec = GraphSpec
|
||||||
|
{ gsName :: Text
|
||||||
|
, gsTitle :: String
|
||||||
|
, gsPlots :: [Plot]
|
||||||
|
, gsIdentifier :: Graph
|
||||||
|
}
|
||||||
|
deriving (Eq, Show)
|
||||||
|
|
||||||
|
average :: String -> Source
|
||||||
|
average ds = Source ds Average
|
||||||
|
|
||||||
|
maximumCF :: String -> Source
|
||||||
|
maximumCF ds = Source ds Maximum
|
||||||
|
|
||||||
|
line :: String -> String -> Source -> Plot
|
||||||
|
line alias color source =
|
||||||
|
Line 2 Series
|
||||||
|
{ serSource = source
|
||||||
|
, serTransform = Identity
|
||||||
|
, serAlias = alias
|
||||||
|
, serColor = color
|
||||||
|
, serLabel = alias
|
||||||
|
}
|
||||||
|
|
||||||
|
area :: String -> String -> Source -> Plot
|
||||||
|
area alias color source =
|
||||||
|
Area Series
|
||||||
|
{ serSource = source
|
||||||
|
, serTransform = Identity
|
||||||
|
, serAlias = alias
|
||||||
|
, serColor = color
|
||||||
|
, serLabel = alias
|
||||||
|
}
|
||||||
|
|
||||||
|
ratelimitGraph :: GraphSpec
|
||||||
|
ratelimitGraph =
|
||||||
|
GraphSpec
|
||||||
|
{ gsName = "ratelimit"
|
||||||
|
, gsTitle = "Rate limiting"
|
||||||
|
, gsPlots =
|
||||||
|
[ Line 2 Series
|
||||||
|
{ serSource = Source "ratelimit_retry" Average
|
||||||
|
, serTransform = Multiply 60
|
||||||
|
, serAlias = "retry"
|
||||||
|
, serColor = "#0066cc"
|
||||||
|
, serLabel = "Requests retried/min"
|
||||||
|
}
|
||||||
|
, Line 2 Series
|
||||||
|
{ serSource = Source "ratelimit_exhausted" Average
|
||||||
|
, serTransform = Multiply 60
|
||||||
|
, serAlias = "exhaust"
|
||||||
|
, serColor = "#cc0000"
|
||||||
|
, serLabel = "Requests exhausted/min"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
, gsIdentifier = RateLimit
|
||||||
|
}
|
||||||
|
|
||||||
|
trafficGraph :: GraphSpec
|
||||||
|
trafficGraph =
|
||||||
|
GraphSpec
|
||||||
|
{ gsName = "traffic"
|
||||||
|
, gsTitle = "Hass triggers in / services out"
|
||||||
|
, gsPlots =
|
||||||
|
[ Line 2 Series
|
||||||
|
{ serSource = Source "hass_trigger_in" Average
|
||||||
|
, serTransform = Multiply 60
|
||||||
|
, serAlias = "in"
|
||||||
|
, serColor = "#0066cc"
|
||||||
|
, serLabel = "Triggers in/min"
|
||||||
|
}
|
||||||
|
, Line 2 Series
|
||||||
|
{ serSource = Source "hass_service_out" Average
|
||||||
|
, serTransform = Multiply 60
|
||||||
|
, serAlias = "out"
|
||||||
|
, serColor = "#cc0000"
|
||||||
|
, serLabel = "Service calls out/min"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
, gsIdentifier = Traffic
|
||||||
|
}
|
||||||
|
|
||||||
|
memoryGraph :: GraphSpec
|
||||||
|
memoryGraph =
|
||||||
|
GraphSpec
|
||||||
|
"memory"
|
||||||
|
"Memory use"
|
||||||
|
[ area "live" "#99ccff" (average "rts_gc_current__a7a")
|
||||||
|
, line "peak" "#cc0000" (maximumCF "rts_gc_current__a7a")
|
||||||
|
]
|
||||||
|
Memory
|
||||||
|
|
||||||
|
graphs :: [GraphSpec]
|
||||||
|
graphs = [memoryGraph, ratelimitGraph, trafficGraph]
|
||||||
|
|
||||||
|
-- XXX: Proper lookup..?
|
||||||
|
lookupGraph :: [GraphSpec] -> Graph -> Maybe GraphSpec
|
||||||
|
lookupGraph specs graphMode = case [g | g <- specs, gsIdentifier g == graphMode] of
|
||||||
|
(g : _) -> Just g
|
||||||
|
[] -> Nothing
|
||||||
|
|
||||||
|
parseNameParam :: [GraphSpec] -> Text -> Maybe GraphSpec
|
||||||
|
parseNameParam specs t = rightToMaybe (parseQueryParam t) >>= lookupGraph specs
|
||||||
|
where
|
||||||
|
rightToMaybe = either (const Nothing) Just
|
||||||
|
|
||||||
|
cfName :: CF -> String
|
||||||
|
cfName = \case
|
||||||
|
Average -> "AVERAGE"
|
||||||
|
Minimum -> "MIN"
|
||||||
|
Maximum -> "MAX"
|
||||||
|
Last -> "LAST"
|
||||||
|
|
||||||
|
defArgs :: FilePath -> Series -> [String]
|
||||||
|
defArgs rrdPath series =
|
||||||
|
[ "DEF:" ++ rawF ++ "=" ++ rrdPath ++ ":"
|
||||||
|
++ srcDs source ++ ":" ++ cfName (srcCF source)
|
||||||
|
]
|
||||||
|
++ transformArgs
|
||||||
|
where
|
||||||
|
source = serSource series
|
||||||
|
alias = serAlias series
|
||||||
|
rawF = alias ++ "_raw"
|
||||||
|
|
||||||
|
transformArgs = case serTransform series of
|
||||||
|
Identity ->
|
||||||
|
[ "CDEF:" ++ alias ++ "=" ++ rawF ]
|
||||||
|
|
||||||
|
Multiply factor ->
|
||||||
|
[ "CDEF:" ++ alias ++ "=" ++ rawF
|
||||||
|
++ "," ++ show factor ++ ",*"
|
||||||
|
]
|
||||||
|
|
||||||
|
plotArg :: Plot -> String
|
||||||
|
plotArg = \case
|
||||||
|
Line width series ->
|
||||||
|
"LINE" ++ show width ++ ":" ++ serAlias series
|
||||||
|
++ serColor series ++ ":" ++ serLabel series
|
||||||
|
|
||||||
|
Area series ->
|
||||||
|
"AREA:" ++ serAlias series
|
||||||
|
++ serColor series ++ ":" ++ serLabel series
|
||||||
|
|
||||||
|
plotSeries :: Plot -> Series
|
||||||
|
plotSeries = \case
|
||||||
|
Line _ series -> series
|
||||||
|
Area series -> series
|
||||||
|
|
||||||
|
buildGraphArgs :: FilePath -> GraphSpec -> Range -> [String]
|
||||||
|
buildGraphArgs rrdPath spec range =
|
||||||
|
[ "graph"
|
||||||
|
, "-"
|
||||||
|
, "--start", rangeStart range
|
||||||
|
, "--title", gsTitle spec
|
||||||
|
, "--width", "800"
|
||||||
|
, "--height", "250"
|
||||||
|
, "--imgformat", "PNG"
|
||||||
|
]
|
||||||
|
++ concatMap (defArgs rrdPath . plotSeries) (gsPlots spec)
|
||||||
|
++ map plotArg (gsPlots spec)
|
||||||
|
|
||||||
|
|
||||||
|
readProcessBytes :: FilePath -> [String] -> IO (ExitCode, BS.ByteString, String)
|
||||||
|
readProcessBytes cmd args =
|
||||||
|
withCreateProcess (proc cmd args) { std_out = CreatePipe, std_err = CreatePipe } $
|
||||||
|
\_ mout merr ph ->
|
||||||
|
case (mout, merr) of
|
||||||
|
(Just hout, Just herr) -> do
|
||||||
|
out <- BS.hGetContents hout
|
||||||
|
err <- hGetContents herr
|
||||||
|
code <- waitForProcess ph
|
||||||
|
_ <- evaluate (length err)
|
||||||
|
pure (code, out, err)
|
||||||
|
_ -> pure (ExitFailure 1, BS.empty, "failed to create pipes")
|
||||||
|
|
||||||
|
renderGraph :: MonadIO m => FilePath -> FilePath -> GraphSpec -> Range -> m (Either Text BS.ByteString)
|
||||||
|
renderGraph rrdPath rrdtool spec range = do
|
||||||
|
(code, out, err) <- liftIO (readProcessBytes rrdtool (buildGraphArgs rrdPath spec range))
|
||||||
|
case code of
|
||||||
|
ExitSuccess
|
||||||
|
| not (BS.null out) -> pure $ Right out
|
||||||
|
| otherwise -> pure $ Left "empty graph"
|
||||||
|
_ -> pure $ Left (T.pack $ take 200 err)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
data Graph
|
||||||
|
= Memory
|
||||||
|
| Traffic
|
||||||
|
| RateLimit
|
||||||
|
deriving (Show, Eq, Enum, Bounded)
|
||||||
|
|
||||||
|
instance ToHttpApiData Graph where
|
||||||
|
toQueryParam Memory = "memory.png"
|
||||||
|
toQueryParam Traffic = "traffic.png"
|
||||||
|
toQueryParam RateLimit = "ratelimit.png"
|
||||||
|
|
||||||
|
instance FromHttpApiData Graph where
|
||||||
|
parseQueryParam "memory.png" = Right Memory
|
||||||
|
parseQueryParam "traffic.png" = Right Traffic
|
||||||
|
parseQueryParam "ratelimit.png" = Right RateLimit
|
||||||
|
parseQueryParam x = Left ("Can't parse '" <> x <> "' as Graph")
|
||||||
|
|
||||||
@@ -8,6 +8,10 @@ module HomeAssistant.Runtime.Metrics
|
|||||||
, buildSchema
|
, buildSchema
|
||||||
, buildCreateArgs
|
, buildCreateArgs
|
||||||
, buildUpdateArgs
|
, buildUpdateArgs
|
||||||
|
, parseInfoDs
|
||||||
|
, schemaMatches
|
||||||
|
, AppMetrics (..)
|
||||||
|
, registerAppMetrics
|
||||||
, ensureRrd
|
, ensureRrd
|
||||||
, sampleAndUpdate
|
, sampleAndUpdate
|
||||||
, metricsAction
|
, metricsAction
|
||||||
@@ -17,19 +21,26 @@ import Control.Concurrent (threadDelay)
|
|||||||
import Control.Monad (forever, unless)
|
import Control.Monad (forever, unless)
|
||||||
import Data.Char (ord)
|
import Data.Char (ord)
|
||||||
import Data.Int (Int64)
|
import Data.Int (Int64)
|
||||||
import Data.List (intercalate, sortBy)
|
import Data.List (intercalate, sort, sortBy, stripPrefix)
|
||||||
|
import Data.Maybe (mapMaybe)
|
||||||
import Data.Ord (comparing)
|
import Data.Ord (comparing)
|
||||||
import Data.Text (Text)
|
import Data.Text (Text)
|
||||||
|
import Data.Time (defaultTimeLocale, formatTime, getCurrentTime)
|
||||||
import Data.Void (Void)
|
import Data.Void (Void)
|
||||||
import Numeric (showHex)
|
import Numeric (showHex)
|
||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
import qualified Data.HashMap.Strict as HM
|
import qualified Data.HashMap.Strict as HM
|
||||||
import qualified System.Metrics as M (Value (..), Sample, Store, sampleAll)
|
import qualified System.Metrics as M (Value (..), Sample, Store, createCounter, sampleAll)
|
||||||
import System.Directory (doesFileExist)
|
import System.Metrics.Counter (Counter)
|
||||||
import System.Process (callProcess)
|
import System.Directory (doesFileExist, renameFile)
|
||||||
|
import System.Exit (ExitCode (..))
|
||||||
|
import System.Process (callProcess, readProcessWithExitCode)
|
||||||
|
import Control.Monad.IO.Class (MonadIO, liftIO)
|
||||||
|
import Control.Exception.Annotated (checkpointCallStack)
|
||||||
|
import Control.Monad.Catch (MonadCatch)
|
||||||
|
|
||||||
data DsType = Derive | Gauge
|
data DsType = Derive | Gauge
|
||||||
deriving (Eq, Show)
|
deriving (Eq, Ord, Show)
|
||||||
|
|
||||||
data DsSpec = DsSpec
|
data DsSpec = DsSpec
|
||||||
{ dsEkgName :: Text
|
{ dsEkgName :: Text
|
||||||
@@ -62,6 +73,19 @@ buildSchema sample =
|
|||||||
, Just dt <- [dsTypeOf val]
|
, Just dt <- [dsTypeOf val]
|
||||||
]
|
]
|
||||||
|
|
||||||
|
data AppMetrics = AppMetrics
|
||||||
|
{ amTriggersIn :: Counter
|
||||||
|
, amServicesOut :: Counter
|
||||||
|
}
|
||||||
|
|
||||||
|
registerAppMetrics :: M.Store -> IO AppMetrics
|
||||||
|
registerAppMetrics store =
|
||||||
|
AppMetrics
|
||||||
|
<$> M.createCounter "hass.trigger.in" store
|
||||||
|
<*> M.createCounter "hass.service.out" store
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
buildCreateArgs :: FilePath -> Int -> [(String, DsType)] -> [String]
|
buildCreateArgs :: FilePath -> Int -> [(String, DsType)] -> [String]
|
||||||
buildCreateArgs path step specs =
|
buildCreateArgs path step specs =
|
||||||
["create", path, "--step", show step]
|
["create", path, "--step", show step]
|
||||||
@@ -89,31 +113,70 @@ buildUpdateArgs path names values =
|
|||||||
renderValue Nothing = "U"
|
renderValue Nothing = "U"
|
||||||
renderValue (Just n) = show n
|
renderValue (Just n) = show n
|
||||||
|
|
||||||
|
dsTypeFromString :: String -> Maybe DsType
|
||||||
|
dsTypeFromString "DERIVE" = Just Derive
|
||||||
|
dsTypeFromString "GAUGE" = Just Gauge
|
||||||
|
dsTypeFromString _ = Nothing
|
||||||
|
|
||||||
|
parseInfoDs :: String -> [(String, DsType)]
|
||||||
|
parseInfoDs = mapMaybe parseLine . lines
|
||||||
|
where
|
||||||
|
parseLine line = do
|
||||||
|
rest0 <- stripPrefix "ds[" line
|
||||||
|
let (name, rest1) = break (== ']') rest0
|
||||||
|
rest2 <- stripPrefix "].type = \"" rest1
|
||||||
|
let (ty, _) = break (== '"') rest2
|
||||||
|
dt <- dsTypeFromString ty
|
||||||
|
pure (name, dt)
|
||||||
|
|
||||||
|
schemaMatches :: [DsSpec] -> [(String, DsType)] -> Bool
|
||||||
|
schemaMatches schema existing =
|
||||||
|
sort (map (\s -> (dsName s, dsType s)) schema) == sort existing
|
||||||
|
|
||||||
lookupValue :: M.Sample -> Text -> Maybe Int64
|
lookupValue :: M.Sample -> Text -> Maybe Int64
|
||||||
lookupValue sample name = case HM.lookup name sample of
|
lookupValue sample name = case HM.lookup name sample of
|
||||||
Just (M.Counter n) -> Just n
|
Just (M.Counter n) -> Just n
|
||||||
Just (M.Gauge n) -> Just n
|
Just (M.Gauge n) -> Just n
|
||||||
_ -> Nothing
|
_ -> Nothing
|
||||||
|
|
||||||
ensureRrd :: FilePath -> FilePath -> [DsSpec] -> IO ()
|
readExistingDs :: (MonadCatch m, MonadIO m) => FilePath -> FilePath -> m [(String, DsType)]
|
||||||
ensureRrd rrdtool rrdPath schema = do
|
readExistingDs rrdtool rrdPath = checkpointCallStack $ do
|
||||||
exists <- doesFileExist rrdPath
|
(code, out, _) <- liftIO $ readProcessWithExitCode rrdtool ["info", rrdPath] ""
|
||||||
unless exists $
|
pure $ case code of
|
||||||
callProcess rrdtool (buildCreateArgs rrdPath 10 (map toPair schema))
|
ExitSuccess -> parseInfoDs out
|
||||||
|
_ -> []
|
||||||
|
|
||||||
|
backupPath :: MonadIO m => FilePath -> m FilePath
|
||||||
|
backupPath path = liftIO $ do
|
||||||
|
now <- getCurrentTime
|
||||||
|
pure $ path ++ ".bak-" ++ formatTime defaultTimeLocale "%Y%m%dT%H%M%S" now
|
||||||
|
|
||||||
|
ensureRrd :: (MonadCatch m, MonadIO m) => FilePath -> FilePath -> [DsSpec] -> m ()
|
||||||
|
ensureRrd rrdtool rrdPath schema = checkpointCallStack $ do
|
||||||
|
exists <- liftIO $ doesFileExist rrdPath
|
||||||
|
if not exists
|
||||||
|
then create
|
||||||
|
else do
|
||||||
|
current <- readExistingDs rrdtool rrdPath
|
||||||
|
unless (schemaMatches schema current) $ do
|
||||||
|
backup <- backupPath rrdPath
|
||||||
|
liftIO $ renameFile rrdPath backup
|
||||||
|
create
|
||||||
where
|
where
|
||||||
|
create = liftIO $ callProcess rrdtool (buildCreateArgs rrdPath 10 (map toPair schema))
|
||||||
toPair s = (dsName s, dsType s)
|
toPair s = (dsName s, dsType s)
|
||||||
|
|
||||||
sampleAndUpdate :: M.Store -> FilePath -> FilePath -> [DsSpec] -> IO ()
|
sampleAndUpdate :: (MonadCatch m, MonadIO m) => M.Store -> FilePath -> FilePath -> [DsSpec] -> m ()
|
||||||
sampleAndUpdate store rrdtool rrdPath schema = do
|
sampleAndUpdate store rrdtool rrdPath schema = checkpointCallStack $ do
|
||||||
sample <- M.sampleAll store
|
sample <- liftIO $ M.sampleAll store
|
||||||
let names = map dsName schema
|
let names = map dsName schema
|
||||||
values = map (lookupValue sample . dsEkgName) schema
|
values = map (lookupValue sample . dsEkgName) schema
|
||||||
callProcess rrdtool (buildUpdateArgs rrdPath names values)
|
liftIO $ callProcess rrdtool (buildUpdateArgs rrdPath names values)
|
||||||
|
|
||||||
metricsAction :: M.Store -> FilePath -> FilePath -> IO Void
|
metricsAction :: (MonadCatch m, MonadIO m) => M.Store -> FilePath -> FilePath -> m Void
|
||||||
metricsAction store rrdPath rrdtool = do
|
metricsAction store rrdPath rrdtool = checkpointCallStack $ do
|
||||||
schema <- buildSchema <$> M.sampleAll store
|
schema <- buildSchema <$> liftIO (M.sampleAll store)
|
||||||
ensureRrd rrdtool rrdPath schema
|
ensureRrd rrdtool rrdPath schema
|
||||||
forever $ do
|
forever $ do
|
||||||
sampleAndUpdate store rrdtool rrdPath schema
|
sampleAndUpdate store rrdtool rrdPath schema
|
||||||
threadDelay 10000000
|
liftIO $ threadDelay 10000000
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
{-# Language DerivingVia #-}
|
||||||
|
{-# Language OverloadedStrings #-}
|
||||||
|
module HomeAssistant.Runtime.RateLimit where
|
||||||
|
import Control.Monad.IO.Class (MonadIO, liftIO)
|
||||||
|
import Data.Time (NominalDiffTime)
|
||||||
|
import Control.Concurrent.STM (newTVarIO, atomically, readTVar, writeTVar)
|
||||||
|
import Data.Time.Clock.POSIX (getPOSIXTime)
|
||||||
|
import Katip.Monadic (KatipContext(..))
|
||||||
|
import Control.Retry (fullJitterBackoff, limitRetries, retrying, RetryStatus (..))
|
||||||
|
import Data.Maybe (isNothing)
|
||||||
|
import Control.Exception.Annotated (Exception, checkpointCallStack, throw)
|
||||||
|
import Control.Monad.Catch (MonadCatch)
|
||||||
|
import Katip (Katip, Severity (WarningS), logFM, ls)
|
||||||
|
import Control.Monad (when)
|
||||||
|
import System.Metrics.Counter (Counter)
|
||||||
|
import qualified System.Metrics.Counter as Counter
|
||||||
|
import System.Metrics (Store, createCounter)
|
||||||
|
|
||||||
|
data RateLimitMetrics = RateLimitMetrics
|
||||||
|
{ rlRetries :: Counter
|
||||||
|
, rlRejects :: Counter
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
registerRateLimitMetrics :: MonadIO m => Store -> m RateLimitMetrics
|
||||||
|
registerRateLimitMetrics store = liftIO $ RateLimitMetrics
|
||||||
|
<$> createCounter "ratelimit.retry" store
|
||||||
|
<*> createCounter "ratelimit.exhausted" store
|
||||||
|
|
||||||
|
newtype RateLimited m a = RateLimited (m a)
|
||||||
|
deriving (Functor, Applicative, Monad, MonadIO, KatipContext, Katip) via m
|
||||||
|
|
||||||
|
|
||||||
|
data RateLimiter = RateLimiter
|
||||||
|
{ metrics :: RateLimitMetrics
|
||||||
|
, limiter :: forall m a. (KatipContext m, MonadIO m) => RateLimited m a -> m (Maybe a)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
slidingWindowLimiter
|
||||||
|
:: RateLimitMetrics
|
||||||
|
-> NominalDiffTime -- ^ Within how many seconds
|
||||||
|
-> Int -- ^ How many requests we can make
|
||||||
|
-> IO RateLimiter
|
||||||
|
slidingWindowLimiter metrics timeWindow size = do
|
||||||
|
window <- newTVarIO []
|
||||||
|
return $ RateLimiter metrics $ \(RateLimited action) -> do
|
||||||
|
now <- liftIO getPOSIXTime
|
||||||
|
allowed <- liftIO $ atomically $ do
|
||||||
|
elements <- takeWhile (\x -> x > now - timeWindow) <$> readTVar window
|
||||||
|
if length elements >= size
|
||||||
|
then pure False
|
||||||
|
else True <$ writeTVar window (now : elements)
|
||||||
|
if allowed
|
||||||
|
then Just <$> action
|
||||||
|
else pure Nothing
|
||||||
|
|
||||||
|
data RateLimitException = RateLimitExhausted
|
||||||
|
deriving Show
|
||||||
|
|
||||||
|
instance Exception RateLimitException
|
||||||
|
|
||||||
|
runRateLimited :: (MonadIO m, KatipContext m, MonadCatch m) => RateLimiter -> RateLimited m a -> m a
|
||||||
|
runRateLimited (RateLimiter metrics run) action = checkpointCallStack $ do
|
||||||
|
x <- retrying retryPolicy (const $ pure . isNothing) $ \RetryStatus{rsIterNumber} -> do
|
||||||
|
when (rsIterNumber > 0) $ do
|
||||||
|
logFM WarningS $ ls $ "Rate limited, attempt no. " <> show rsIterNumber
|
||||||
|
incRetry
|
||||||
|
run action
|
||||||
|
maybe (incReject >> throw RateLimitExhausted) pure x
|
||||||
|
where
|
||||||
|
retryPolicy = fullJitterBackoff 500_000 <> limitRetries 5
|
||||||
|
incRetry = liftIO $ Counter.inc (rlRetries metrics)
|
||||||
|
incReject = liftIO $ Counter.inc (rlRejects metrics)
|
||||||
@@ -2,26 +2,23 @@
|
|||||||
|
|
||||||
module HomeAssistant.Runtime.Supervisor
|
module HomeAssistant.Runtime.Supervisor
|
||||||
( supervised
|
( supervised
|
||||||
, Backoff(..)
|
|
||||||
, defaultBackoff
|
|
||||||
, backoffDelay
|
|
||||||
, nextAttempt
|
|
||||||
, Fatal(..)
|
, Fatal(..)
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Control.Concurrent (threadDelay)
|
|
||||||
import Control.Exception.Annotated
|
import Control.Exception.Annotated
|
||||||
( Exception
|
( Exception
|
||||||
, Handler (..)
|
, Handler (..)
|
||||||
, SomeException
|
, SomeException
|
||||||
, catches
|
, checkpoint, Annotation (..)
|
||||||
, displayException
|
|
||||||
, throw
|
|
||||||
)
|
)
|
||||||
import Data.Text (Text)
|
import Data.Text (Text)
|
||||||
import qualified Data.Text as T
|
|
||||||
import Data.Time (NominalDiffTime, diffUTCTime, getCurrentTime)
|
|
||||||
import Data.Void (Void)
|
import Data.Void (Void)
|
||||||
|
import Control.Monad.IO.Class (MonadIO)
|
||||||
|
import Control.Monad.Catch (MonadCatch, MonadMask)
|
||||||
|
import Katip (KatipContext, logFM, ls)
|
||||||
|
import Control.Retry (RetryStatus(..), recovering, fullJitterBackoff, capDelay, skipAsyncExceptions)
|
||||||
|
import Control.Monad (when)
|
||||||
|
import Katip.Core (Severity(..))
|
||||||
|
|
||||||
-- | A failure that cannot be fixed by restarting; propagates out of
|
-- | A failure that cannot be fixed by restarting; propagates out of
|
||||||
-- 'supervised' and terminates the process.
|
-- 'supervised' and terminates the process.
|
||||||
@@ -30,28 +27,11 @@ newtype Fatal = Fatal Text
|
|||||||
|
|
||||||
instance Exception Fatal
|
instance Exception Fatal
|
||||||
|
|
||||||
data Backoff = Backoff
|
|
||||||
{ backoffBase :: NominalDiffTime -- ^ Delay before the first restart
|
|
||||||
, backoffCap :: NominalDiffTime -- ^ Maximum delay between restarts
|
|
||||||
, backoffQuiet :: NominalDiffTime -- ^ Uptime after which the delay resets
|
|
||||||
}
|
|
||||||
deriving (Eq, Show)
|
|
||||||
|
|
||||||
defaultBackoff :: Backoff
|
data SupervisorException = RetriesExhausted
|
||||||
defaultBackoff = Backoff 0.1 30 30
|
deriving Show
|
||||||
|
|
||||||
-- | Delay before the @attempt@-th restart: doubles from base, clamped at cap.
|
instance Exception SupervisorException
|
||||||
backoffDelay :: Backoff -> Int -> NominalDiffTime
|
|
||||||
backoffDelay (Backoff base cap _) attempt = go (max 0 (attempt - 1)) base
|
|
||||||
where
|
|
||||||
go 0 d = d
|
|
||||||
go n d = go (n - 1) (min cap (d * 2))
|
|
||||||
|
|
||||||
-- | Attempt number to use after a crash that ran for the given uptime.
|
|
||||||
nextAttempt :: Backoff -> NominalDiffTime -> Int -> Int
|
|
||||||
nextAttempt (Backoff _ _ quiet) uptime attempt
|
|
||||||
| uptime >= quiet = 1
|
|
||||||
| otherwise = attempt + 1
|
|
||||||
|
|
||||||
-- | Runs the action forever, restarting it with backoff after synchronous
|
-- | Runs the action forever, restarting it with backoff after synchronous
|
||||||
-- exceptions; async exceptions propagate. 'Fatal' is rethrown (crashing the
|
-- exceptions; async exceptions propagate. 'Fatal' is rethrown (crashing the
|
||||||
@@ -59,22 +39,14 @@ nextAttempt (Backoff _ _ quiet) uptime attempt
|
|||||||
-- must not be wrapped in checkpoints around 'Fatal'-throwing code: a
|
-- must not be wrapped in checkpoints around 'Fatal'-throwing code: a
|
||||||
-- doubly-wrapped 'Fatal' is indistinguishable from a crash and would be
|
-- doubly-wrapped 'Fatal' is indistinguishable from a crash and would be
|
||||||
-- restarted instead of escalated.
|
-- restarted instead of escalated.
|
||||||
supervised :: Text -> Backoff -> IO Void -> IO Void
|
supervised :: (MonadIO m, MonadCatch m, KatipContext m, MonadMask m) => Text -> m Void -> m Void
|
||||||
supervised name backoff action = go 1
|
supervised name action = checkpoint (Annotation name) $ do
|
||||||
|
recovering retryPolicy (skipAsyncExceptions ++ handlers) $ \RetryStatus{rsIterNumber} -> do
|
||||||
|
when (rsIterNumber > 0) $ logFM WarningS $ ls $ "Child crashed, retry no " <> show rsIterNumber
|
||||||
|
action
|
||||||
where
|
where
|
||||||
go attempt = do
|
handlers =
|
||||||
start <- getCurrentTime
|
[ \_retryStatus -> Handler $ \(_ :: Fatal) -> pure False
|
||||||
outcome <- (action >> pure (Nothing :: Maybe (Either Fatal SomeException))) `catches`
|
, \_retryStatus -> Handler $ \(_ :: SomeException) -> pure True
|
||||||
[ Handler $ \(f :: Fatal) -> pure (Just (Left f))
|
|
||||||
, Handler $ \(e :: SomeException) -> pure (Just (Right e))
|
|
||||||
]
|
]
|
||||||
end <- getCurrentTime
|
retryPolicy = capDelay (15 * 1_000_000) (fullJitterBackoff 50_000)
|
||||||
case outcome of
|
|
||||||
Nothing -> error "unreachable: supervised action returned"
|
|
||||||
Just (Left f) -> throw f
|
|
||||||
Just (Right e) -> do
|
|
||||||
putStrLn $ "[" <> T.unpack name <> "] attempt " <> show attempt <> " crashed: " <> displayException e
|
|
||||||
let delay = backoffDelay backoff attempt
|
|
||||||
putStrLn $ "[" <> T.unpack name <> "] restarting in " <> show delay
|
|
||||||
threadDelay (round (realToFrac delay * 1000000 :: Double))
|
|
||||||
go (nextAttempt backoff (diffUTCTime end start) attempt)
|
|
||||||
@@ -0,0 +1,87 @@
|
|||||||
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
|
{-# LANGUAGE DerivingVia #-}
|
||||||
|
module HttpServer where
|
||||||
|
|
||||||
|
import Control.Monad (forever)
|
||||||
|
import Control.Monad.IO.Class (liftIO, MonadIO)
|
||||||
|
import qualified Data.ByteString as BS
|
||||||
|
import Data.Void (Void)
|
||||||
|
import Network.Wai.Handler.Warp (run)
|
||||||
|
import Servant.API ((:>), Capture, Get, QueryParam, (:-), MimeRender (..), Accept (..), NamedRoutes, JSON, ReqBody, Put)
|
||||||
|
import Data.ByteString (ByteString)
|
||||||
|
import GHC.Generics (Generic)
|
||||||
|
import Servant (Handler, err404)
|
||||||
|
import Control.Monad.Catch (throwM, catch, MonadCatch, MonadThrow, SomeException)
|
||||||
|
import Servant.Server (err500, ServerT)
|
||||||
|
import Data.Maybe (fromMaybe)
|
||||||
|
import Data.Map.Strict (Map)
|
||||||
|
import qualified Data.Set as S
|
||||||
|
import Data.Text (Text)
|
||||||
|
import HomeAssistant.Runtime.Flags (Flags, allFlags, setEnabled, validNames)
|
||||||
|
import Control.Exception.Annotated (checkpoint, Annotation (Annotation))
|
||||||
|
import Servant.Server.Generic (genericServeT)
|
||||||
|
import Network.Wai (Application)
|
||||||
|
import Network.HTTP.Media ((//))
|
||||||
|
import HomeAssistant.Runtime.Graphing (Graph, Range (..), lookupGraph, graphs, renderGraph)
|
||||||
|
import Katip (KatipContextT, Katip, runKatipContextT, LogEnv, KatipContext, ls, Severity (..), logFM)
|
||||||
|
|
||||||
|
data ImagePng
|
||||||
|
|
||||||
|
instance MimeRender ImagePng ByteString where
|
||||||
|
mimeRender _ = BS.fromStrict
|
||||||
|
|
||||||
|
instance Accept ImagePng where
|
||||||
|
contentType _ = "image" // "png"
|
||||||
|
|
||||||
|
|
||||||
|
data API mode = API
|
||||||
|
{ getMetrics :: mode :- "metrics" :> Capture "graph" Graph :> QueryParam "range" Range :> Get '[ImagePng] ByteString
|
||||||
|
, getFlags :: mode :- "flags" :> Get '[JSON] (Map Text Bool)
|
||||||
|
, putFlag :: mode :- "flags" :> Capture "name" Text :> ReqBody '[JSON] Bool :> Put '[JSON] (Map Text Bool)
|
||||||
|
}
|
||||||
|
deriving Generic
|
||||||
|
|
||||||
|
|
||||||
|
getFlagsHandler :: Flags -> LoggingHandler (Map Text Bool)
|
||||||
|
getFlagsHandler flags = allFlags flags
|
||||||
|
|
||||||
|
putFlagHandler :: Flags -> Text -> Bool -> LoggingHandler (Map Text Bool)
|
||||||
|
putFlagHandler flags name val
|
||||||
|
| name `S.notMember` validNames flags = throwM err404
|
||||||
|
| otherwise = do
|
||||||
|
setEnabled flags name val `catch` \e -> do
|
||||||
|
logFM ErrorS (ls ("failed to persist handler flag: " <> show (e :: SomeException)))
|
||||||
|
throwM err500
|
||||||
|
allFlags flags
|
||||||
|
|
||||||
|
|
||||||
|
getMetricsHandler :: FilePath -> FilePath -> Graph -> Maybe Range -> LoggingHandler ByteString
|
||||||
|
getMetricsHandler rrdPath rrdtool graphMode mRange = checkpoint (Annotation (graphMode, mRange)) $ do
|
||||||
|
case lookupGraph graphs graphMode of
|
||||||
|
Nothing -> throwM err404
|
||||||
|
Just spec -> do
|
||||||
|
let range = fromMaybe OneDay mRange
|
||||||
|
g <- renderGraph rrdPath rrdtool spec range
|
||||||
|
either (\e -> logFM ErrorS (ls e) >> throwM err500) pure g
|
||||||
|
|
||||||
|
-- server :: FilePath -> FilePath -> API AsServer
|
||||||
|
server :: FilePath -> FilePath -> Flags -> ServerT (NamedRoutes API) LoggingHandler
|
||||||
|
server rrdPath rrdtool flags = API
|
||||||
|
{ getMetrics = getMetricsHandler rrdPath rrdtool
|
||||||
|
, getFlags = getFlagsHandler flags
|
||||||
|
, putFlag = putFlagHandler flags
|
||||||
|
}
|
||||||
|
|
||||||
|
servantApp :: LogEnv -> FilePath -> FilePath -> Flags -> Application
|
||||||
|
servantApp le rrdPath rrdtool flags = genericServeT (toHandler le) (server rrdPath rrdtool flags)
|
||||||
|
|
||||||
|
runHttpServer :: MonadIO m => LogEnv -> FilePath -> FilePath -> Flags -> Int -> m Void
|
||||||
|
runHttpServer le rrdPath rrdtool flags port = liftIO $ forever $ run port (servantApp le rrdPath rrdtool flags)
|
||||||
|
|
||||||
|
newtype LoggingHandler a = LoggingHandler (KatipContextT Handler a)
|
||||||
|
deriving (Functor, Applicative, Monad, MonadIO, Katip, KatipContext, MonadCatch, MonadThrow) via (KatipContextT Handler)
|
||||||
|
|
||||||
|
toHandler :: LogEnv -> (forall a. LoggingHandler a -> Handler a)
|
||||||
|
toHandler le (LoggingHandler k) = runKatipContextT le () "http" k
|
||||||
|
|
||||||
|
|
||||||
+2
-2
@@ -21,7 +21,7 @@ import Test.Hspec
|
|||||||
import Test.Hspec.Hedgehog
|
import Test.Hspec.Hedgehog
|
||||||
|
|
||||||
fakeRequest :: Request
|
fakeRequest :: Request
|
||||||
fakeRequest = Request (sec 0) utc nil
|
fakeRequest = Request (sec 0) utc nil "test"
|
||||||
|
|
||||||
sec :: Integer -> UTCTime
|
sec :: Integer -> UTCTime
|
||||||
sec n = UTCTime (toEnum 0) (fromIntegral n)
|
sec n = UTCTime (toEnum 0) (fromIntegral n)
|
||||||
@@ -42,7 +42,7 @@ runTimed m = go (runMealy m id)
|
|||||||
where
|
where
|
||||||
go _ [] = []
|
go _ [] = []
|
||||||
go w ((s, a) : as) =
|
go w ((s, a) : as) =
|
||||||
case runIdentity (stepAuto w (Request (sec s) utc nil) a) of
|
case runIdentity (stepAuto w (Request (sec s) utc nil "test") a) of
|
||||||
(b, w') -> b : go w' as
|
(b, w') -> b : go w' as
|
||||||
|
|
||||||
-- | A minimal State monad for observing effectful arrows (e.g. whenA gating).
|
-- | A minimal State monad for observing effectful arrows (e.g. whenA gating).
|
||||||
|
|||||||
@@ -1,21 +0,0 @@
|
|||||||
module BackoffProp (spec) where
|
|
||||||
|
|
||||||
import Data.Maybe (listToMaybe)
|
|
||||||
import Data.Time (NominalDiffTime)
|
|
||||||
import qualified Hedgehog.Gen as Gen
|
|
||||||
import qualified Hedgehog.Range as Range
|
|
||||||
import HomeAssistant.Runtime.Supervisor (Backoff (..), backoffDelay)
|
|
||||||
import Test.Hspec (Spec, describe, it)
|
|
||||||
import Test.Hspec.Hedgehog (hedgehog, forAll, (===))
|
|
||||||
|
|
||||||
spec :: Spec
|
|
||||||
spec = describe "backoffDelay" $
|
|
||||||
it "doubles from base, clamped at cap" $ hedgehog $ do
|
|
||||||
baseD <- forAll $ Gen.double (Range.constant 0.0001 10)
|
|
||||||
ratio <- forAll $ Gen.double (Range.constant 1 100)
|
|
||||||
let base = realToFrac baseD :: NominalDiffTime
|
|
||||||
cap = realToFrac (baseD * ratio) :: NominalDiffTime
|
|
||||||
backoff = Backoff base cap 1
|
|
||||||
delays = map (backoffDelay backoff) [1 .. 100 :: Int]
|
|
||||||
listToMaybe delays === Just (min cap base)
|
|
||||||
mapM_ (\(a, b) -> b === min cap (a * 2)) (zip delays (drop 1 delays))
|
|
||||||
+18
-17
@@ -5,7 +5,7 @@ module BedroomSpec (spec) where
|
|||||||
import AFRP (Event (..), entities)
|
import AFRP (Event (..), entities)
|
||||||
import qualified Data.Set as S
|
import qualified Data.Set as S
|
||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
import Data.Aeson (Value)
|
import Data.Aeson (Value, object, (.=))
|
||||||
import HomeAssistant.Controller
|
import HomeAssistant.Controller
|
||||||
import HomeAssistant.Controller.Bedroom
|
import HomeAssistant.Controller.Bedroom
|
||||||
import Support
|
import Support
|
||||||
@@ -45,6 +45,7 @@ entitySpec = describe "entities" $ do
|
|||||||
`shouldBe` S.fromList
|
`shouldBe` S.fromList
|
||||||
[ "event.bedroom_quick_remote_masse_action"
|
[ "event.bedroom_quick_remote_masse_action"
|
||||||
, "event.bedroom_quick_jemina_action"
|
, "event.bedroom_quick_jemina_action"
|
||||||
|
, "event.bedroom_remote_action"
|
||||||
]
|
]
|
||||||
|
|
||||||
it "bedroomPresenceController subscribes to the presence sensor" $
|
it "bedroomPresenceController subscribes to the presence sensor" $
|
||||||
@@ -89,36 +90,36 @@ buttonSpec = describe "bedroomButtonController" $ do
|
|||||||
let area = AreaId "makuuhuone"
|
let area = AreaId "makuuhuone"
|
||||||
|
|
||||||
it "Masse single click turns on his nightstand scene (lowest)" $
|
it "Masse single click turns on his nightstand scene (lowest)" $
|
||||||
services (runHASS bedroomButtonController [masseButton "1_short_release"])
|
concat (services (runHASS bedroomButtonController [masseButton "1_short_release"]))
|
||||||
`shouldBe` [[activateScene "scene.makuuhuone_masse"]]
|
`shouldContain` [activateSceneWith "scene.makuuhuone_masse" (Just $ object ["transition" .= (5 :: Int)])]
|
||||||
|
|
||||||
it "Masse double click turns on the middle scene" $
|
it "Masse double click turns on the middle scene" $
|
||||||
services (runHASS bedroomButtonController [masseButton "1_double_press"])
|
concat (services (runHASS bedroomButtonController [masseButton "1_double_press"]))
|
||||||
`shouldBe` [[activateScene "scene.makuuhuone_keski"]]
|
`shouldContain` [activateSceneWith "scene.makuuhuone_keski" (Just $ object ["transition" .= (5 :: Int)])]
|
||||||
|
|
||||||
it "Masse long click turns on the bright scene" $
|
it "Masse long click turns on the bright scene" $
|
||||||
services (runHASS bedroomButtonController [masseButton "1_long_press"])
|
concat (services (runHASS bedroomButtonController [masseButton "1_long_press"]))
|
||||||
`shouldBe` [[activateScene "scene.makuuhuone_kirkas"]]
|
`shouldContain` [activateSceneWith "scene.makuuhuone_kirkas" (Just $ object ["transition" .= (5 :: Int)])]
|
||||||
|
|
||||||
it "Masse off click turns the bedroom lights off" $
|
it "Masse off click turns the bedroom lights off" $
|
||||||
services (runHASS bedroomButtonController [masseButton "2_short_release"])
|
concat (services (runHASS bedroomButtonController [masseButton "2_short_release"]))
|
||||||
`shouldBe` [[light [area] Off]]
|
`shouldContain` [light [area] Off]
|
||||||
|
|
||||||
it "Enishen single click turns on her nightstand scene (lowest)" $
|
it "Enishen single click turns on her nightstand scene (lowest)" $
|
||||||
services (runHASS bedroomButtonController [enishenButton "1_short_release"])
|
concat (services (runHASS bedroomButtonController [enishenButton "1_short_release"]))
|
||||||
`shouldBe` [[activateScene "scene.makuuhuone_jemina"]]
|
`shouldContain` [activateSceneWith "scene.makuuhuone_jemina" (Just $ object ["transition" .= (5 :: Int)])]
|
||||||
|
|
||||||
it "Enishen double click turns on the middle scene" $
|
it "Enishen double click turns on the middle scene" $
|
||||||
services (runHASS bedroomButtonController [enishenButton "1_double_press"])
|
concat (services (runHASS bedroomButtonController [enishenButton "1_double_press"]))
|
||||||
`shouldBe` [[activateScene "scene.makuuhuone_keski"]]
|
`shouldContain` [activateSceneWith "scene.makuuhuone_keski" (Just $ object ["transition" .= (5 :: Int)])]
|
||||||
|
|
||||||
it "Enishen long click turns on the bright scene" $
|
it "Enishen long click turns on the bright scene" $
|
||||||
services (runHASS bedroomButtonController [enishenButton "1_long_press"])
|
concat (services (runHASS bedroomButtonController [enishenButton "1_long_press"]))
|
||||||
`shouldBe` [[activateScene "scene.makuuhuone_kirkas"]]
|
`shouldContain` [activateSceneWith "scene.makuuhuone_kirkas" (Just $ object ["transition" .= (5 :: Int)])]
|
||||||
|
|
||||||
it "Enishen off click turns the bedroom lights off" $
|
it "Enishen off click turns the bedroom lights off" $
|
||||||
services (runHASS bedroomButtonController [enishenButton "2_short_release"])
|
concat (services (runHASS bedroomButtonController [enishenButton "2_short_release"]))
|
||||||
`shouldBe` [[light [area] Off]]
|
`shouldContain` [light [area] Off]
|
||||||
|
|
||||||
it "ignores the initial press (scene only fires on release)" $
|
it "ignores the initial press (scene only fires on release)" $
|
||||||
services (runHASS bedroomButtonController [masseButton "1_initial_press"])
|
services (runHASS bedroomButtonController [masseButton "1_initial_press"])
|
||||||
|
|||||||
+48
-3
@@ -6,20 +6,26 @@ import AFRP (Request (..), Event (..))
|
|||||||
import Control.Concurrent.STM
|
import Control.Concurrent.STM
|
||||||
( atomically
|
( atomically
|
||||||
, dupTChan
|
, dupTChan
|
||||||
|
, isEmptyTChan
|
||||||
, readTChan
|
, readTChan
|
||||||
, writeTChan
|
, writeTChan
|
||||||
)
|
)
|
||||||
import Data.Aeson (Value (..))
|
import Data.Aeson (Value (..))
|
||||||
|
import qualified Data.HashMap.Strict as HM
|
||||||
import Data.Time (UTCTime (..), utc)
|
import Data.Time (UTCTime (..), utc)
|
||||||
import Data.UUID (nil)
|
import Data.UUID (nil)
|
||||||
import HomeAssistant.Controller (HASSEff (..), Service (..), Target(..))
|
import HomeAssistant.Controller (HASSEff (..), Service (..), Target(..))
|
||||||
import HomeAssistant.Runtime.Bus
|
import HomeAssistant.Runtime.Bus
|
||||||
|
import HomeAssistant.Runtime.Flags (loadFlags, setEnabled)
|
||||||
|
import HomeAssistant.Runtime.Metrics (registerAppMetrics)
|
||||||
import Katip (Namespace (Namespace), runKatipContextT, Severity (..))
|
import Katip (Namespace (Namespace), runKatipContextT, Severity (..))
|
||||||
|
import qualified System.Metrics as Metrics
|
||||||
|
import System.IO.Temp (withTempDirectory)
|
||||||
import Test.Hspec
|
import Test.Hspec
|
||||||
|
|
||||||
spec :: Spec
|
spec :: Spec
|
||||||
spec = describe "Bus" $ do
|
spec = describe "Bus" $ do
|
||||||
it "broadcasts inbound messages to every dup'd channel in order" $ withBus InfoS $ \bus -> do
|
it "broadcasts inbound messages to every dup'd channel in order" $ withTestBus $ \bus -> do
|
||||||
p1 <- atomically $ dupTChan (busInbound bus)
|
p1 <- atomically $ dupTChan (busInbound bus)
|
||||||
p2 <- atomically $ dupTChan (busInbound bus)
|
p2 <- atomically $ dupTChan (busInbound bus)
|
||||||
atomically $ writeTChan (busInbound bus) (Event (Number 1))
|
atomically $ writeTChan (busInbound bus) (Event (Number 1))
|
||||||
@@ -29,17 +35,56 @@ spec = describe "Bus" $ do
|
|||||||
r1 `shouldBe` (Event (Number 1), Event (Number 2))
|
r1 `shouldBe` (Event (Number 1), Event (Number 2))
|
||||||
r2 `shouldBe` (Event (Number 1), Event (Number 2))
|
r2 `shouldBe` (Event (Number 1), Event (Number 2))
|
||||||
|
|
||||||
it "channelHassEval writes CallService to the outbound channel" $ withBus InfoS $ \bus -> do
|
it "channelHassEval writes CallService to the outbound channel" $ withTestBus $ \bus -> do
|
||||||
let req = Request (UTCTime (toEnum 0) 0) utc nil
|
let req = Request (UTCTime (toEnum 0) 0) utc nil "test"
|
||||||
svc = Service "light" "turn_on" Nothing [EntityId "light.bedroom_masse"]
|
svc = Service "light" "turn_on" Nothing [EntityId "light.bedroom_masse"]
|
||||||
runKatipContextT (busLogEnv bus) () (Namespace ["test"]) $
|
runKatipContextT (busLogEnv bus) () (Namespace ["test"]) $
|
||||||
channelHassEval bus (CallService req svc)
|
channelHassEval bus (CallService req svc)
|
||||||
(_, svc') <- atomically $ readTChan (busOutbound bus)
|
(_, svc') <- atomically $ readTChan (busOutbound bus)
|
||||||
svc' `shouldBe` svc
|
svc' `shouldBe` svc
|
||||||
|
|
||||||
|
it "channelHassEval drops CallService when the handler is disabled" $
|
||||||
|
withTestFlagsBus $ \bus -> do
|
||||||
|
setEnabled (busFlags bus) "test-handler" False
|
||||||
|
let req = Request (UTCTime (toEnum 0) 0) utc nil "test-handler"
|
||||||
|
svc = Service "light" "turn_on" Nothing [EntityId "light.test"]
|
||||||
|
runKatipContextT (busLogEnv bus) () (Namespace ["test"]) $
|
||||||
|
channelHassEval bus (CallService req svc)
|
||||||
|
atomically (isEmptyTChan (busOutbound bus)) `shouldReturn` True
|
||||||
|
|
||||||
|
|
||||||
it "generates unique sequential call ids" $ do
|
it "generates unique sequential call ids" $ do
|
||||||
gen <- mkCallIdGen 0
|
gen <- mkCallIdGen 0
|
||||||
a <- generateCallId gen
|
a <- generateCallId gen
|
||||||
b <- generateCallId gen
|
b <- generateCallId gen
|
||||||
(a, b) `shouldBe` (1, 2)
|
(a, b) `shouldBe` (1, 2)
|
||||||
|
|
||||||
|
describe "recordInbound / recordOutbound" $ do
|
||||||
|
it "increments the trigger and service counters" $ do
|
||||||
|
store <- Metrics.newStore
|
||||||
|
m <- registerAppMetrics store
|
||||||
|
withTempDirectory "/tmp" "bus-spec" $ \dir -> do
|
||||||
|
flags <- loadFlags dir []
|
||||||
|
withBus InfoS m flags $ \bus -> do
|
||||||
|
recordInbound bus
|
||||||
|
recordInbound bus
|
||||||
|
recordOutbound bus
|
||||||
|
sample <- Metrics.sampleAll store
|
||||||
|
HM.lookup "hass.trigger.in" sample `shouldBe` Just (Metrics.Counter 2)
|
||||||
|
HM.lookup "hass.service.out" sample `shouldBe` Just (Metrics.Counter 1)
|
||||||
|
|
||||||
|
withTestBus :: (Bus -> IO a) -> IO a
|
||||||
|
withTestBus action = do
|
||||||
|
store <- Metrics.newStore
|
||||||
|
m <- registerAppMetrics store
|
||||||
|
withTempDirectory "/tmp" "bus-spec" $ \dir -> do
|
||||||
|
flags <- loadFlags dir []
|
||||||
|
withBus InfoS m flags action
|
||||||
|
|
||||||
|
withTestFlagsBus :: (Bus -> IO a) -> IO a
|
||||||
|
withTestFlagsBus action = do
|
||||||
|
store <- Metrics.newStore
|
||||||
|
m <- registerAppMetrics store
|
||||||
|
withTempDirectory "/tmp" "bus-spec" $ \dir -> do
|
||||||
|
flags <- loadFlags dir ["test-handler"]
|
||||||
|
withBus InfoS m flags action
|
||||||
+9
-29
@@ -9,7 +9,7 @@ import Data.Text (Text)
|
|||||||
import Data.Time (UTCTime (..), utc)
|
import Data.Time (UTCTime (..), utc)
|
||||||
import Data.UUID (fromString)
|
import Data.UUID (fromString)
|
||||||
import HomeAssistant.Controller (Service (..), Target(..))
|
import HomeAssistant.Controller (Service (..), Target(..))
|
||||||
import HomeAssistant.Runtime.Connection (encodeService, dedupeBatch)
|
import HomeAssistant.Runtime.Connection (encodeService, isTriggerEvent)
|
||||||
import Test.Hspec
|
import Test.Hspec
|
||||||
|
|
||||||
spec :: Spec
|
spec :: Spec
|
||||||
@@ -36,38 +36,18 @@ spec = do
|
|||||||
, "service_data" .= object ["brightness" .= (200 :: Int)]
|
, "service_data" .= object ["brightness" .= (200 :: Int)]
|
||||||
]
|
]
|
||||||
|
|
||||||
describe "dedupeBatch" $ do
|
describe "isTriggerEvent" $ do
|
||||||
it "collapses identical calls to one" $
|
it "is true for event frames" $
|
||||||
let batch = [ (req 1, lightOn [AreaId "x"])
|
isTriggerEvent (object ["type" .= ("event" :: Text)]) `shouldBe` True
|
||||||
, (req 2, lightOn [AreaId "x"])
|
it "is false for result frames" $
|
||||||
, (req 3, lightOn [AreaId "x"])
|
isTriggerEvent (object ["type" .= ("result" :: Text)]) `shouldBe` False
|
||||||
]
|
it "is false when there is no type" $
|
||||||
in dedupeBatch batch `shouldBe` [(req 3, lightOn [AreaId "x"])]
|
isTriggerEvent (object ["id" .= (1 :: Int)]) `shouldBe` False
|
||||||
|
|
||||||
it "keeps same-target different-service calls separate" $
|
|
||||||
let batch = [ (req 1, lightOn [AreaId "x"])
|
|
||||||
, (req 2, lightOff [AreaId "x"])
|
|
||||||
]
|
|
||||||
result = dedupeBatch batch
|
|
||||||
in length result `shouldBe` 2
|
|
||||||
|
|
||||||
it "newest call wins for the same key" $
|
|
||||||
let batch = [ (req 1, lightOn [AreaId "x"])
|
|
||||||
, (req 2, lightOn [AreaId "x"])
|
|
||||||
, (req 3, lightOn [AreaId "x"])
|
|
||||||
]
|
|
||||||
in map requestTraceId (map fst (dedupeBatch batch)) `shouldBe`
|
|
||||||
[fromJust (fromString "00000000-0000-0000-0000-000000000003")]
|
|
||||||
|
|
||||||
it "treats target lists in different order as the same key" $
|
|
||||||
let batch = [ (req 1, lightOn [EntityId "a", EntityId "b"])
|
|
||||||
, (req 2, lightOn [EntityId "b", EntityId "a"])
|
|
||||||
]
|
|
||||||
in length (dedupeBatch batch) `shouldBe` 1
|
|
||||||
|
|
||||||
req :: Int -> Request
|
req :: Int -> Request
|
||||||
req n = Request (UTCTime (toEnum 0) (fromIntegral (0 :: Int))) utc
|
req n = Request (UTCTime (toEnum 0) (fromIntegral (0 :: Int))) utc
|
||||||
(fromJust (fromString uuid))
|
(fromJust (fromString uuid)) "test"
|
||||||
where
|
where
|
||||||
pad i = replicate (12 - length (show i)) '0' <> show i
|
pad i = replicate (12 - length (show i)) '0' <> show i
|
||||||
uuid = "00000000-0000-0000-0000-" <> pad n
|
uuid = "00000000-0000-0000-0000-" <> pad n
|
||||||
|
|||||||
@@ -0,0 +1,75 @@
|
|||||||
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
|
|
||||||
|
module FlagsSpec (spec) where
|
||||||
|
|
||||||
|
import Control.Concurrent.Async (mapConcurrently_)
|
||||||
|
import Control.Monad.IO.Class (liftIO)
|
||||||
|
import Data.Aeson (decodeFileStrict)
|
||||||
|
import Data.Map.Strict (Map)
|
||||||
|
import qualified Data.Map.Strict as M
|
||||||
|
import Data.Text (Text)
|
||||||
|
import qualified Hedgehog.Gen as Gen
|
||||||
|
import qualified Hedgehog.Range as Range
|
||||||
|
import System.IO.Temp (withTempDirectory)
|
||||||
|
import Test.Hspec
|
||||||
|
import Test.Hspec.Hedgehog
|
||||||
|
|
||||||
|
import HomeAssistant.Runtime.Flags
|
||||||
|
|
||||||
|
spec :: Spec
|
||||||
|
spec = describe "Flags" $ do
|
||||||
|
describe "loadFlags" $ do
|
||||||
|
it "defaults all names to True when the file is missing (opt-out)" $
|
||||||
|
withTempDirectory "/tmp" "flags-spec" $ \dir -> do
|
||||||
|
flags <- loadFlags dir ["a", "b"]
|
||||||
|
m <- allFlags flags
|
||||||
|
m `shouldBe` M.fromList [("a" :: Text, True), ("b", True)]
|
||||||
|
|
||||||
|
it "lets the file override the True default, missing keys stay True" $
|
||||||
|
withTempDirectory "/tmp" "flags-spec" $ \dir -> do
|
||||||
|
let path = dir <> "/handlers.json"
|
||||||
|
writeFile path "{\"b\": false}"
|
||||||
|
flags <- loadFlags dir ["a", "b"]
|
||||||
|
m <- allFlags flags
|
||||||
|
m `shouldBe` M.fromList [("a" :: Text, True), ("b" :: Text, False)]
|
||||||
|
|
||||||
|
it "treats a corrupt file as empty (all-enabled) without throwing" $
|
||||||
|
withTempDirectory "/tmp" "flags-spec" $ \dir -> do
|
||||||
|
let path = dir <> "/handlers.json"
|
||||||
|
writeFile path "not json at all"
|
||||||
|
flags <- loadFlags dir ["a"]
|
||||||
|
m <- allFlags flags
|
||||||
|
m `shouldBe` M.fromList [("a" :: Text, True)]
|
||||||
|
|
||||||
|
describe "isEnabled" $ do
|
||||||
|
it "returns the stored value" $
|
||||||
|
withTempDirectory "/tmp" "flags-spec" $ \dir -> do
|
||||||
|
let path = dir <> "/handlers.json"
|
||||||
|
writeFile path "{\"a\": false}"
|
||||||
|
flags <- loadFlags dir ["a"]
|
||||||
|
isEnabled flags "a" `shouldReturn` False
|
||||||
|
|
||||||
|
it "defaults unknown names to True" $
|
||||||
|
withTempDirectory "/tmp" "flags-spec" $ \dir -> do
|
||||||
|
flags <- loadFlags dir []
|
||||||
|
isEnabled flags "unknown" `shouldReturn` True
|
||||||
|
|
||||||
|
describe "setEnabled" $ do
|
||||||
|
it "updates the in-memory map and persists to disk" $
|
||||||
|
withTempDirectory "/tmp" "flags-spec" $ \dir -> do
|
||||||
|
flags <- loadFlags dir ["a"]
|
||||||
|
setEnabled flags "a" False
|
||||||
|
isEnabled flags "a" `shouldReturn` False
|
||||||
|
-- reload from disk to confirm persistence
|
||||||
|
flags' <- loadFlags dir ["a"]
|
||||||
|
isEnabled flags' "a" `shouldReturn` False
|
||||||
|
|
||||||
|
it "concurrent writes leave the file consistent with the TVar" $
|
||||||
|
hedgehog $ do
|
||||||
|
n <- forAll $ Gen.int (Range.linear 1 50)
|
||||||
|
liftIO $ withTempDirectory "/tmp" "flags-spec" $ \dir -> do
|
||||||
|
flags <- loadFlags dir ["x"]
|
||||||
|
mapConcurrently_ (\i -> setEnabled flags "x" (even i)) [1 .. n]
|
||||||
|
m <- allFlags flags
|
||||||
|
decoded <- decodeFileStrict (dir <> "/handlers.json")
|
||||||
|
(decoded :: Maybe (Map Text Bool)) `shouldBe` Just m
|
||||||
@@ -0,0 +1,144 @@
|
|||||||
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
|
|
||||||
|
module GraphingSpec (spec) where
|
||||||
|
|
||||||
|
import Control.Exception (SomeException, finally, try)
|
||||||
|
import qualified Data.ByteString as BS
|
||||||
|
import HomeAssistant.Runtime.Graphing
|
||||||
|
( GraphSpec (..)
|
||||||
|
, Range (..)
|
||||||
|
, buildGraphArgs
|
||||||
|
, graphs
|
||||||
|
, lookupGraph
|
||||||
|
, parseNameParam
|
||||||
|
, parseRange
|
||||||
|
, rangeStart
|
||||||
|
, readProcessBytes
|
||||||
|
)
|
||||||
|
import qualified HomeAssistant.Runtime.Metrics as Metrics
|
||||||
|
import System.Directory (findExecutable, getTemporaryDirectory, removeFile)
|
||||||
|
import System.Exit (ExitCode (..))
|
||||||
|
import System.Process (readProcessWithExitCode)
|
||||||
|
import Test.Hspec
|
||||||
|
import Servant.API (FromHttpApiData(..), ToHttpApiData (..))
|
||||||
|
import HomeAssistant.Runtime.Graphing (Graph(..))
|
||||||
|
|
||||||
|
traffic :: GraphSpec
|
||||||
|
traffic = case lookupGraph graphs Traffic of
|
||||||
|
Just s -> s
|
||||||
|
Nothing -> error "traffic graph is missing from the table"
|
||||||
|
|
||||||
|
spec :: Spec
|
||||||
|
spec = do
|
||||||
|
describe "Graph HttpApiData" $ do
|
||||||
|
-- There's an enumerable amount of graphs, we can just enumerate through them all
|
||||||
|
it "satisfies roundtrip property" $ do
|
||||||
|
let modes = [minBound..maxBound]
|
||||||
|
let got = map (parseQueryParam . toQueryParam @Graph) modes
|
||||||
|
let wanted = map Right modes
|
||||||
|
got `shouldBe` wanted
|
||||||
|
describe "parseRange" $ do
|
||||||
|
-- Fully enumerable
|
||||||
|
it "the http-api-data satisfies roundtrip property" $ do
|
||||||
|
let rs = [minBound..maxBound]
|
||||||
|
let got = map (parseQueryParam . toQueryParam @Range) rs
|
||||||
|
let wanted = map Right rs
|
||||||
|
got `shouldBe` wanted
|
||||||
|
it "accepts every suffix in the closed set" $
|
||||||
|
map parseRange ["10m", "1h", "24h", "7d", "30d"]
|
||||||
|
`shouldBe` [ Just TenMinutes
|
||||||
|
, Just OneHour
|
||||||
|
, Just OneDay
|
||||||
|
, Just OneWeek
|
||||||
|
, Just OneMonth
|
||||||
|
]
|
||||||
|
|
||||||
|
it "rejects everything outside the closed set" $
|
||||||
|
map parseRange
|
||||||
|
[ ""
|
||||||
|
, "1h "
|
||||||
|
, " 1h"
|
||||||
|
, "1h; rm -rf /"
|
||||||
|
, "end-1h"
|
||||||
|
, "../../etc/passwd"
|
||||||
|
, "1y"
|
||||||
|
, "30m"
|
||||||
|
, "1H"
|
||||||
|
]
|
||||||
|
`shouldBe` replicate 9 Nothing
|
||||||
|
|
||||||
|
describe "rangeStart" $ do
|
||||||
|
it "renders rrdtool end- relative starts" $
|
||||||
|
map rangeStart [TenMinutes, OneHour, OneDay, OneWeek, OneMonth]
|
||||||
|
`shouldBe` ["end-10m", "end-1h", "end-24h", "end-7d", "end-30d"]
|
||||||
|
|
||||||
|
describe "parseNameParam" $ do
|
||||||
|
it "resolves the traffic graph only with a .png suffix" $ do
|
||||||
|
fmap gsName (parseNameParam graphs "traffic.png") `shouldBe` Just "traffic"
|
||||||
|
parseNameParam graphs "traffic" `shouldBe` Nothing
|
||||||
|
|
||||||
|
it "rejects unknown, malformed, and traversing names" $
|
||||||
|
map (parseNameParam graphs)
|
||||||
|
[ "unknown.png"
|
||||||
|
, "../traffic.png"
|
||||||
|
, "traffic.png.png"
|
||||||
|
, ""
|
||||||
|
, "nope"
|
||||||
|
]
|
||||||
|
`shouldBe` replicate 5 Nothing
|
||||||
|
|
||||||
|
describe "buildGraphArgs" $ do
|
||||||
|
it "builds the traffic argv for a range" $
|
||||||
|
buildGraphArgs "hass-controller.rrd" traffic OneHour
|
||||||
|
`shouldBe`
|
||||||
|
[ "graph"
|
||||||
|
, "-"
|
||||||
|
, "--start", "end-1h"
|
||||||
|
, "--title", "Hass triggers in / services out"
|
||||||
|
, "--width", "800"
|
||||||
|
, "--height", "250"
|
||||||
|
, "--imgformat", "PNG"
|
||||||
|
, "DEF:in_raw=hass-controller.rrd:hass_trigger_in:AVERAGE"
|
||||||
|
, "CDEF:in=in_raw,60.0,*"
|
||||||
|
, "DEF:out_raw=hass-controller.rrd:hass_service_out:AVERAGE"
|
||||||
|
, "CDEF:out=out_raw,60.0,*"
|
||||||
|
, "LINE2:in#0066cc:Triggers in/min"
|
||||||
|
, "LINE2:out#cc0000:Service calls out/min"
|
||||||
|
]
|
||||||
|
|
||||||
|
describe "end-to-end (rrdtool-gated)" $ do
|
||||||
|
it "renders a PNG for the traffic graph" $ do
|
||||||
|
mRrdtool <- findExecutable "rrdtool"
|
||||||
|
case mRrdtool of
|
||||||
|
Nothing -> pendingWith "rrdtool not on PATH"
|
||||||
|
Just rrdtool -> do
|
||||||
|
tmp <- getTemporaryDirectory
|
||||||
|
let rrdPath = tmp ++ "/hass-controller-graph-test.rrd"
|
||||||
|
cleanup = try (removeFile rrdPath) :: IO (Either SomeException ())
|
||||||
|
schema =
|
||||||
|
[ ("hass_trigger_in", Metrics.Derive)
|
||||||
|
, ("hass_service_out", Metrics.Derive)
|
||||||
|
]
|
||||||
|
pngMagic = BS.pack [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]
|
||||||
|
_ <- cleanup
|
||||||
|
( do
|
||||||
|
(createCode, _, _) <-
|
||||||
|
readProcessWithExitCode rrdtool (Metrics.buildCreateArgs rrdPath 10 schema) ""
|
||||||
|
createCode `shouldBe` ExitSuccess
|
||||||
|
(updateCode, _, _) <-
|
||||||
|
readProcessWithExitCode
|
||||||
|
rrdtool
|
||||||
|
(Metrics.buildUpdateArgs
|
||||||
|
rrdPath
|
||||||
|
["hass_trigger_in", "hass_service_out"]
|
||||||
|
[Just 5, Just 3])
|
||||||
|
""
|
||||||
|
updateCode `shouldBe` ExitSuccess
|
||||||
|
(code, out, _) <- readProcessBytes rrdtool (buildGraphArgs rrdPath traffic OneHour)
|
||||||
|
code `shouldBe` ExitSuccess
|
||||||
|
out `shouldSatisfy` BS.isPrefixOf pngMagic
|
||||||
|
)
|
||||||
|
`finally` cleanup
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
|
|
||||||
|
module KitchenSpec (spec) where
|
||||||
|
|
||||||
|
import AFRP (Event (..), entities)
|
||||||
|
import qualified Data.Set as S
|
||||||
|
import qualified Data.Text as T
|
||||||
|
import Data.Aeson (Value)
|
||||||
|
import HomeAssistant.Controller
|
||||||
|
import HomeAssistant.Controller.Kitchen
|
||||||
|
import Support
|
||||||
|
import Test.Hspec
|
||||||
|
|
||||||
|
-- Helper functions for creating test events
|
||||||
|
testDinnerLightLevel :: Int -> Event Value
|
||||||
|
testDinnerLightLevel level = Event (stateEvent "sensor.sijainti_keittio_light_level" (T.pack $ show level))
|
||||||
|
|
||||||
|
testDinnerPresence :: T.Text -> Event Value
|
||||||
|
testDinnerPresence state = Event (stateEvent "binary_sensor.sijainti_keittio_occupancy" state)
|
||||||
|
|
||||||
|
spec :: Spec
|
||||||
|
spec = describe "Kitchen" $ do
|
||||||
|
dinnerTableSpec
|
||||||
|
|
||||||
|
dinnerTableSpec :: Spec
|
||||||
|
dinnerTableSpec = describe "dinnerTableController" $ do
|
||||||
|
it "subscribes to the light level sensor" $ do
|
||||||
|
entities dinnerTableController
|
||||||
|
`shouldBe` S.fromList ["sensor.sijainti_keittio_light_level", "binary_sensor.sijainti_keittio_occupancy"]
|
||||||
|
|
||||||
|
it "subscribes to the presence sensor" $ do
|
||||||
|
entities dinnerTableController
|
||||||
|
`shouldBe` S.fromList ["sensor.sijainti_keittio_light_level", "binary_sensor.sijainti_keittio_occupancy"]
|
||||||
|
|
||||||
|
it "turns lights on when presence is occupied and light level is low" $ do
|
||||||
|
services (runHASS dinnerTableController [testDinnerLightLevel 10, testDinnerPresence "on"])
|
||||||
|
`shouldBe` [[switch [EntityId "switch.adaptive_lighting_ruokapoydan_valot_sleep_mode"] True], [switch [EntityId "switch.adaptive_lighting_ruokapoydan_valot_sleep_mode"] False, light [EntityId "light.kitchen_dining_large"] (On Nothing)]]
|
||||||
|
|
||||||
|
it "turns lights off when presence is unoccupied" $ do
|
||||||
|
services (runHASS dinnerTableController [testDinnerLightLevel 10, testDinnerPresence "off"])
|
||||||
|
`shouldBe` [[switch [EntityId "switch.adaptive_lighting_ruokapoydan_valot_sleep_mode"] True], []]
|
||||||
|
|
||||||
|
it "does nothing on Tick" $ do
|
||||||
|
services (runHASS dinnerTableController [Tick])
|
||||||
|
`shouldBe` [[switch [EntityId "switch.adaptive_lighting_ruokapoydan_valot_sleep_mode"] True]]
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
|
|
||||||
|
module LivingroomSpec (spec) where
|
||||||
|
|
||||||
|
import AFRP (Event (..))
|
||||||
|
import Data.Time (UTCTime (..), fromGregorian)
|
||||||
|
import HomeAssistant.Controller
|
||||||
|
import HomeAssistant.Controller.Livingroom
|
||||||
|
import Support
|
||||||
|
import Test.Hspec
|
||||||
|
|
||||||
|
-- | A UTCTime at a given hour on March 15, 2025 (Spring).
|
||||||
|
springAt :: Int -> UTCTime
|
||||||
|
springAt h = UTCTime (fromGregorian 2025 3 15) (fromIntegral (h * 3600))
|
||||||
|
|
||||||
|
-- | A UTCTime at a given hour on July 15, 2025 (Summer).
|
||||||
|
summerAt :: Int -> UTCTime
|
||||||
|
summerAt h = UTCTime (fromGregorian 2025 7 15) (fromIntegral (h * 3600))
|
||||||
|
|
||||||
|
spec :: Spec
|
||||||
|
spec = describe "livingroomPlantLights" $ do
|
||||||
|
it "activates the Growth scene at 10:00 in Spring" $ do
|
||||||
|
let result = runHASSTimed livingroomPlantLights
|
||||||
|
[ (springAt 9, Tick)
|
||||||
|
, (springAt 10, Tick)
|
||||||
|
]
|
||||||
|
services result
|
||||||
|
`shouldBe` [ [], [activateScene "scene.olohuoneen_kasvivalo"] ]
|
||||||
|
|
||||||
|
it "activates the Ambience scene at 20:00 in Spring" $ do
|
||||||
|
let result = runHASSTimed livingroomPlantLights
|
||||||
|
[ (springAt 19, Tick)
|
||||||
|
, (springAt 20, Tick)
|
||||||
|
]
|
||||||
|
services result
|
||||||
|
`shouldBe` [ [], [activateScene "scene.kasvivalo_iltatila"] ]
|
||||||
|
|
||||||
|
it "does not activate Growth in Summer" $ do
|
||||||
|
let result = runHASSTimed livingroomPlantLights
|
||||||
|
[ (summerAt 9, Tick)
|
||||||
|
, (summerAt 10, Tick)
|
||||||
|
]
|
||||||
|
services result `shouldBe` [ [], [] ]
|
||||||
+8
-4
@@ -3,13 +3,15 @@ module Main (main) where
|
|||||||
import Test.Hspec (hspec)
|
import Test.Hspec (hspec)
|
||||||
import qualified AFRPLawsSpec
|
import qualified AFRPLawsSpec
|
||||||
import qualified AFRPSpec
|
import qualified AFRPSpec
|
||||||
import qualified BackoffProp
|
|
||||||
import qualified BedroomSpec
|
import qualified BedroomSpec
|
||||||
import qualified BusSpec
|
import qualified BusSpec
|
||||||
import qualified ConnectionSpec
|
import qualified ConnectionSpec
|
||||||
|
import qualified FlagsSpec
|
||||||
|
import qualified GraphingSpec
|
||||||
|
import qualified KitchenSpec
|
||||||
|
import qualified LivingroomSpec
|
||||||
import qualified MetricsSpec
|
import qualified MetricsSpec
|
||||||
import qualified RuntimeSpec
|
import qualified RuntimeSpec
|
||||||
import qualified SupervisorSpec
|
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = hspec $ do
|
main = hspec $ do
|
||||||
@@ -18,7 +20,9 @@ main = hspec $ do
|
|||||||
BedroomSpec.spec
|
BedroomSpec.spec
|
||||||
BusSpec.spec
|
BusSpec.spec
|
||||||
ConnectionSpec.spec
|
ConnectionSpec.spec
|
||||||
|
FlagsSpec.spec
|
||||||
|
GraphingSpec.spec
|
||||||
|
KitchenSpec.spec
|
||||||
|
LivingroomSpec.spec
|
||||||
MetricsSpec.spec
|
MetricsSpec.spec
|
||||||
RuntimeSpec.spec
|
RuntimeSpec.spec
|
||||||
SupervisorSpec.spec
|
|
||||||
BackoffProp.spec
|
|
||||||
+80
-4
@@ -4,8 +4,9 @@ module MetricsSpec (spec) where
|
|||||||
|
|
||||||
import Data.HashMap.Strict (HashMap)
|
import Data.HashMap.Strict (HashMap)
|
||||||
import qualified Data.HashMap.Strict as HM
|
import qualified Data.HashMap.Strict as HM
|
||||||
import Data.Int (Int64)
|
import Data.List (isPrefixOf)
|
||||||
import Data.Text (Text)
|
import Data.Text (Text)
|
||||||
|
import qualified Data.Text as T
|
||||||
import HomeAssistant.Runtime.Metrics
|
import HomeAssistant.Runtime.Metrics
|
||||||
( DsType (..)
|
( DsType (..)
|
||||||
, DsSpec (..)
|
, DsSpec (..)
|
||||||
@@ -16,12 +17,16 @@ import HomeAssistant.Runtime.Metrics
|
|||||||
, buildUpdateArgs
|
, buildUpdateArgs
|
||||||
, ensureRrd
|
, ensureRrd
|
||||||
, sampleAndUpdate
|
, sampleAndUpdate
|
||||||
, metricsAction
|
, parseInfoDs
|
||||||
|
, schemaMatches
|
||||||
|
, AppMetrics (..)
|
||||||
|
, registerAppMetrics
|
||||||
)
|
)
|
||||||
import qualified System.Metrics as M (Value (..))
|
import qualified System.Metrics as M (Value (..))
|
||||||
import Test.Hspec
|
import Test.Hspec
|
||||||
import Control.Exception (try, SomeException)
|
import Control.Exception (try, SomeException, finally)
|
||||||
import System.Directory (findExecutable, getTemporaryDirectory, removeFile)
|
import Control.Monad (forM_)
|
||||||
|
import System.Directory (findExecutable, getTemporaryDirectory, listDirectory, removeFile)
|
||||||
import System.Exit (ExitCode (ExitSuccess))
|
import System.Exit (ExitCode (ExitSuccess))
|
||||||
import System.Process (readProcessWithExitCode)
|
import System.Process (readProcessWithExitCode)
|
||||||
import qualified System.Metrics as Metrics
|
import qualified System.Metrics as Metrics
|
||||||
@@ -126,6 +131,49 @@ spec = do
|
|||||||
, "N:U"
|
, "N:U"
|
||||||
]
|
]
|
||||||
|
|
||||||
|
describe "parseInfoDs" $ do
|
||||||
|
it "extracts DS names and types from rrdtool info output" $
|
||||||
|
let info = unlines
|
||||||
|
[ "filename = \"test.rrd\""
|
||||||
|
, "step = 10"
|
||||||
|
, "ds[foo].index = 0"
|
||||||
|
, "ds[foo].type = \"DERIVE\""
|
||||||
|
, "ds[bar].type = \"GAUGE\""
|
||||||
|
, "rra[0].cf = \"AVERAGE\""
|
||||||
|
]
|
||||||
|
in parseInfoDs info `shouldBe` [("foo", Derive), ("bar", Gauge)]
|
||||||
|
|
||||||
|
it "ignores lines that are not ds type declarations" $
|
||||||
|
parseInfoDs "step = 10\nrra[0].cf = \"AVERAGE\"\n" `shouldBe` []
|
||||||
|
|
||||||
|
describe "schemaMatches" $ do
|
||||||
|
let ds name ty = DsSpec (T.pack name) name ty
|
||||||
|
it "is true for identical schemas regardless of order" $
|
||||||
|
schemaMatches [ds "b" Gauge, ds "a" Derive] [("a", Derive), ("b", Gauge)]
|
||||||
|
`shouldBe` True
|
||||||
|
|
||||||
|
it "is false when the derived schema has an added DS" $
|
||||||
|
schemaMatches [ds "a" Derive, ds "b" Gauge] [("a", Derive)]
|
||||||
|
`shouldBe` False
|
||||||
|
|
||||||
|
it "is false when the derived schema has a removed DS" $
|
||||||
|
schemaMatches [ds "a" Derive] [("a", Derive), ("b", Gauge)]
|
||||||
|
`shouldBe` False
|
||||||
|
|
||||||
|
it "is false when a DS type changed" $
|
||||||
|
schemaMatches [ds "a" Derive] [("a", Gauge)]
|
||||||
|
`shouldBe` False
|
||||||
|
|
||||||
|
describe "registerAppMetrics" $ do
|
||||||
|
it "registers both counters with the store" $ do
|
||||||
|
store <- Metrics.newStore
|
||||||
|
m <- registerAppMetrics store
|
||||||
|
Counter.inc (amTriggersIn m)
|
||||||
|
Counter.inc (amServicesOut m)
|
||||||
|
sample <- Metrics.sampleAll store
|
||||||
|
HM.lookup "hass.trigger.in" sample `shouldBe` Just (M.Counter 1)
|
||||||
|
HM.lookup "hass.service.out" sample `shouldBe` Just (M.Counter 1)
|
||||||
|
|
||||||
describe "end-to-end (rrdtool-gated)" $ do
|
describe "end-to-end (rrdtool-gated)" $ do
|
||||||
it "creates an rrd, samples, and updates it" $ do
|
it "creates an rrd, samples, and updates it" $ do
|
||||||
mRrdtool <- findExecutable "rrdtool"
|
mRrdtool <- findExecutable "rrdtool"
|
||||||
@@ -148,3 +196,31 @@ spec = do
|
|||||||
length out `shouldSatisfy` (> 0)
|
length out `shouldSatisfy` (> 0)
|
||||||
_ <- try (removeFile rrdPath) :: IO (Either SomeException ())
|
_ <- try (removeFile rrdPath) :: IO (Either SomeException ())
|
||||||
return ()
|
return ()
|
||||||
|
|
||||||
|
it "backs up and recreates the rrd when the schema changes" $ do
|
||||||
|
mRrdtool <- findExecutable "rrdtool"
|
||||||
|
case mRrdtool of
|
||||||
|
Nothing -> pendingWith "rrdtool not on PATH"
|
||||||
|
Just rrdtool -> do
|
||||||
|
tmp <- getTemporaryDirectory
|
||||||
|
let rrdPath = tmp ++ "/hass-controller-schema-change-test.rrd"
|
||||||
|
bakPrefix = "hass-controller-schema-change-test.rrd.bak-"
|
||||||
|
base = [ DsSpec "a" "a" Derive, DsSpec "b" "b" Gauge ]
|
||||||
|
expanded = base ++ [ DsSpec "c" "c" Derive ]
|
||||||
|
clean = do
|
||||||
|
stale <- filter (isPrefixOf bakPrefix) <$> listDirectory tmp
|
||||||
|
forM_ (rrdPath : map (\f -> tmp ++ "/" ++ f) stale) $ \f ->
|
||||||
|
try (removeFile f) :: IO (Either SomeException ())
|
||||||
|
clean
|
||||||
|
( do
|
||||||
|
ensureRrd rrdtool rrdPath base
|
||||||
|
ensureRrd rrdtool rrdPath base
|
||||||
|
noBackups <- filter (isPrefixOf bakPrefix) <$> listDirectory tmp
|
||||||
|
noBackups `shouldBe` []
|
||||||
|
ensureRrd rrdtool rrdPath expanded
|
||||||
|
backups <- filter (isPrefixOf bakPrefix) <$> listDirectory tmp
|
||||||
|
length backups `shouldBe` 1
|
||||||
|
(rc, out, _) <- readProcessWithExitCode rrdtool ["info", rrdPath] ""
|
||||||
|
rc `shouldBe` ExitSuccess
|
||||||
|
out `shouldContain` "ds[c].type"
|
||||||
|
) `finally` clean
|
||||||
+25
-2
@@ -1,9 +1,32 @@
|
|||||||
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
|
|
||||||
module RuntimeSpec (spec) where
|
module RuntimeSpec (spec) where
|
||||||
|
|
||||||
|
import Control.Concurrent (threadDelay)
|
||||||
|
import Control.Concurrent.Async (async, cancel, race, waitCatch)
|
||||||
|
import Control.Exception (bracket)
|
||||||
|
import Control.Monad (forever)
|
||||||
|
import Control.Monad.IO.Class (liftIO)
|
||||||
|
import HomeAssistant.Runtime.Supervisor (supervised)
|
||||||
|
import Katip (LogEnv, closeScribes, initLogEnv, runKatipContextT)
|
||||||
import Test.Hspec
|
import Test.Hspec
|
||||||
|
|
||||||
spec :: Spec
|
spec :: Spec
|
||||||
spec = pure ()
|
spec = describe "supervised" $ do
|
||||||
|
it "propagates ThreadKilled instead of retrying" $
|
||||||
|
withTestLogEnv $ \le -> do
|
||||||
|
worker <- async $ runKatipContextT le () mempty $
|
||||||
|
supervised "test" (liftIO (forever (threadDelay 100000)))
|
||||||
|
threadDelay 200000
|
||||||
|
cancel worker
|
||||||
|
result <- race (threadDelay 5000000) (waitCatch worker)
|
||||||
|
case result of
|
||||||
|
Left () -> expectationFailure
|
||||||
|
"supervised retried on ThreadKilled (process would hang)"
|
||||||
|
Right _ -> pure ()
|
||||||
|
|
||||||
|
withTestLogEnv :: (LogEnv -> IO a) -> IO a
|
||||||
|
withTestLogEnv = bracket (initLogEnv "test" "test") closeScribes
|
||||||
|
|
||||||
-- I'm commenting out the test as that specific controller
|
-- I'm commenting out the test as that specific controller
|
||||||
-- was just a dummy for seeing how the data flows (dead code)
|
-- was just a dummy for seeing how the data flows (dead code)
|
||||||
@@ -11,7 +34,7 @@ spec = pure ()
|
|||||||
--
|
--
|
||||||
-- spec :: Spec
|
-- spec :: Spec
|
||||||
-- spec = describe "runController" $ do
|
-- spec = describe "runController" $ do
|
||||||
-- it "feeds inbound events through the machine and forwards service calls" $ withBus InfoS $ \bus -> do
|
-- it "feeds inbound events through the machine and forwards service calls" $ withBus severity appMetrics $ \bus -> do
|
||||||
-- _ <- async (runController bus (Controller "test" lightController True))
|
-- _ <- async (runController bus (Controller "test" lightController True))
|
||||||
-- putStrLn "Before the delay"
|
-- putStrLn "Before the delay"
|
||||||
-- threadDelay 100000 -- let the controller dup its inbound channel
|
-- threadDelay 100000 -- let the controller dup its inbound channel
|
||||||
|
|||||||
@@ -1,73 +0,0 @@
|
|||||||
{-# LANGUAGE OverloadedStrings #-}
|
|
||||||
|
|
||||||
module SupervisorSpec (spec) where
|
|
||||||
|
|
||||||
import Control.Concurrent (newEmptyMVar, putMVar, readMVar, threadDelay)
|
|
||||||
import Control.Concurrent.Async (async, cancel, poll, waitCatch)
|
|
||||||
import Control.Exception (fromException)
|
|
||||||
import Control.Exception.Annotated (AnnotatedException (..), throw)
|
|
||||||
import Control.Monad (forever)
|
|
||||||
import Data.IORef (atomicModifyIORef', newIORef, readIORef)
|
|
||||||
import Data.Maybe (isJust, isNothing)
|
|
||||||
import HomeAssistant.Runtime.Supervisor
|
|
||||||
import Test.Hspec
|
|
||||||
|
|
||||||
tinyBackoff :: Backoff
|
|
||||||
tinyBackoff = Backoff 0.001 0.002 0.001
|
|
||||||
|
|
||||||
spec :: Spec
|
|
||||||
spec = describe "supervised" $ do
|
|
||||||
it "restarts a crashing action until it stays up" $ do
|
|
||||||
counter <- newIORef (0 :: Int)
|
|
||||||
up <- newEmptyMVar
|
|
||||||
let action = do
|
|
||||||
n <- atomicModifyIORef' counter (\c -> (c + 1, c + 1))
|
|
||||||
if n < 3
|
|
||||||
then ioError (userError "boom")
|
|
||||||
else do putMVar up (); forever (threadDelay 1000000)
|
|
||||||
sup <- async (supervised "test" tinyBackoff action)
|
|
||||||
readMVar up
|
|
||||||
threadDelay 50000
|
|
||||||
status <- poll sup
|
|
||||||
isNothing status `shouldBe` True
|
|
||||||
readIORef counter `shouldReturn` 3
|
|
||||||
cancel sup
|
|
||||||
|
|
||||||
it "rethrows Fatal instead of restarting" $ do
|
|
||||||
counter <- newIORef (0 :: Int)
|
|
||||||
let action = do
|
|
||||||
_ <- atomicModifyIORef' counter (\c -> (c + 1, c + 1))
|
|
||||||
throw (Fatal "auth_invalid")
|
|
||||||
sup <- async (supervised "test" tinyBackoff action)
|
|
||||||
res <- waitCatch sup
|
|
||||||
case res of
|
|
||||||
Left se -> case fromException se :: Maybe (AnnotatedException Fatal) of
|
|
||||||
Just _ -> pure ()
|
|
||||||
Nothing -> expectationFailure "expected Fatal to propagate"
|
|
||||||
Right _ -> expectationFailure "supervised returned"
|
|
||||||
threadDelay 50000
|
|
||||||
readIORef counter `shouldReturn` 1
|
|
||||||
|
|
||||||
it "does not restart on async exceptions" $ do
|
|
||||||
counter <- newIORef (0 :: Int)
|
|
||||||
let action = do
|
|
||||||
_ <- atomicModifyIORef' counter (\c -> (c + 1, c + 1))
|
|
||||||
forever (threadDelay 1000000)
|
|
||||||
sup <- async (supervised "test" tinyBackoff action)
|
|
||||||
threadDelay 100000
|
|
||||||
cancel sup
|
|
||||||
threadDelay 100000
|
|
||||||
readIORef counter `shouldReturn` 1
|
|
||||||
status <- poll sup
|
|
||||||
isJust status `shouldBe` True
|
|
||||||
|
|
||||||
describe "nextAttempt" $ do
|
|
||||||
it "resets after a quiet period" $
|
|
||||||
nextAttempt tinyBackoff 0.001 5 `shouldBe` 1
|
|
||||||
it "increments otherwise" $
|
|
||||||
nextAttempt tinyBackoff 0.0005 5 `shouldBe` 6
|
|
||||||
|
|
||||||
describe "backoffDelay" $ do
|
|
||||||
it "starts at base" $ backoffDelay tinyBackoff 1 `shouldBe` 0.001
|
|
||||||
it "doubles" $ backoffDelay tinyBackoff 2 `shouldBe` 0.002
|
|
||||||
it "clamps at cap" $ backoffDelay tinyBackoff 3 `shouldBe` 0.002
|
|
||||||
+13
-2
@@ -4,6 +4,7 @@ module Support
|
|||||||
( Acc(..)
|
( Acc(..)
|
||||||
, interp
|
, interp
|
||||||
, runHASS
|
, runHASS
|
||||||
|
, runHASSTimed
|
||||||
, services
|
, services
|
||||||
, fakeRequest
|
, fakeRequest
|
||||||
, sec
|
, sec
|
||||||
@@ -16,11 +17,11 @@ import Data.Aeson (Value, object, (.=))
|
|||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
import Data.Time (UTCTime (..), utc)
|
import Data.Time (UTCTime (..), utc)
|
||||||
import Data.UUID (nil)
|
import Data.UUID (nil)
|
||||||
import AFRP (Mealy (..), Request (..), stepAuto)
|
import AFRP (Mealy (..), Request (..), stepAuto, Event (..))
|
||||||
import HomeAssistant.Controller (HASSEff (..), Service)
|
import HomeAssistant.Controller (HASSEff (..), Service)
|
||||||
|
|
||||||
fakeRequest :: Request
|
fakeRequest :: Request
|
||||||
fakeRequest = Request (sec 0) utc nil
|
fakeRequest = Request (sec 0) utc nil "test"
|
||||||
|
|
||||||
sec :: Integer -> UTCTime
|
sec :: Integer -> UTCTime
|
||||||
sec n = UTCTime (toEnum 0) (fromIntegral n)
|
sec n = UTCTime (toEnum 0) (fromIntegral n)
|
||||||
@@ -56,6 +57,16 @@ runHASS m = go (runMealy m interp)
|
|||||||
case runAcc (stepAuto w fakeRequest a) [] of
|
case runAcc (stepAuto w fakeRequest a) [] of
|
||||||
((b, w'), svcs) -> (b, svcs) : go w' as
|
((b, w'), svcs) -> (b, svcs) : go w' as
|
||||||
|
|
||||||
|
-- | Run a HASS arrow over a list of (time, input) pairs, collecting per-step
|
||||||
|
-- emitted services. Each step uses a fresh Request with the given UTCTime.
|
||||||
|
runHASSTimed :: Mealy HASSEff (Event a) b -> [(UTCTime, Event a)] -> [(b, [Service])]
|
||||||
|
runHASSTimed m = go (runMealy m interp)
|
||||||
|
where
|
||||||
|
go _ [] = []
|
||||||
|
go w ((t, a) : as) =
|
||||||
|
case runAcc (stepAuto w (Request t utc nil "test") a) [] of
|
||||||
|
((b, w'), svcs) -> (b, svcs) : go w' as
|
||||||
|
|
||||||
services :: [(b, [Service])] -> [[Service]]
|
services :: [(b, [Service])] -> [[Service]]
|
||||||
services = map snd
|
services = map snd
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user