Skip to content

Commit

Permalink
feat(homeassistant): add tuya presence sensor zha quirk
Browse files Browse the repository at this point in the history
  • Loading branch information
diogotcorreia committed May 13, 2024
1 parent 329d9a5 commit e6ac6d8
Show file tree
Hide file tree
Showing 7 changed files with 499 additions and 6 deletions.
4 changes: 4 additions & 0 deletions hosts/bro/homeassistant.nix
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ in {
home-assistant = package;
})
];

customZhaQuirks = with pkgs.my.home-assistant-custom-zha-quirks; [
tuya-persence-sensor-ts0225
];
};

# https://nixos.wiki/wiki/Home_Assistant#Combine_declarative_and_UI_defined_automations
Expand Down
8 changes: 5 additions & 3 deletions lib/default.nix
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
{lib, ...} @ args: let
listModulesRecursive = dir:
listFilesWithSuffixRecursive = suffix: dir:
lib.filter
(p: lib.hasSuffix ".nix" p && !(lib.hasPrefix "_" (builtins.baseNameOf p)))
(p: lib.hasSuffix suffix p && !(lib.hasPrefix "_" (builtins.baseNameOf p)))
(lib.filesystem.listFilesRecursive dir);

listModulesRecursive = listFilesWithSuffixRecursive ".nix";

listModulesRecursive' = dir:
lib.filter
(p: p != dir + "/default.nix")
(listModulesRecursive dir);
in {
my =
{
inherit listModulesRecursive listModulesRecursive';
inherit listFilesWithSuffixRecursive listModulesRecursive listModulesRecursive';
}
// lib.foldr (path: acc: acc // (import path args)) {} (listModulesRecursive' ./.);
}
2 changes: 1 addition & 1 deletion lib/generators.nix
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
*/
mkOverlays = overlaysDir:
lib.mapAttrsRecursive
(_: module: import module {inherit rakeLeaves inputs;})
(_: module: import module {inherit lib rakeLeaves inputs;})
(rakeLeaves overlaysDir);

/*
Expand Down
56 changes: 56 additions & 0 deletions modules/services/home-assistant.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
config,
lib,
...
}: let
inherit (lib) escapeShellArg escapeShellArgs mdDoc mkAfter mkIf mkOption types;

cfg = config.services.home-assistant;

quirksDir = "${cfg.configDir}/zha-quirks";
in {
# Extend home-assistant module with extra options
options.services.home-assistant = {
customZhaQuirks = mkOption {
type = types.listOf types.path;
default = [];
description = mdDoc ''
List of custom ZHA (Zigbee) quirks to load.
Available quirks can be found below `pkgs.my.home-assistant-custom-zha-quirks`.
'';
};
};

config = mkIf cfg.enable {
systemd.services.home-assistant.preStart = let
copyZhaQuirks = ''
mkdir -p ${escapeShellArg quirksDir}
# remove quirks symlinked in from below the /nix/store
readarray -d "" quirks < <(find ${escapeShellArg quirksDir} -maxdepth 1 -type l -print0)
for quirk in "''${quirks[@]}"; do
if [[ "$(readlink "$quirk")" =~ ^${escapeShellArg builtins.storeDir} ]]; then
rm "$quirk"
fi
done
# recreate symlinks for desired quirks
declare -a quirks=(${escapeShellArgs cfg.customZhaQuirks})
for quirk in "''${quirks[@]}"; do
ln -fs "$quirk" ${escapeShellArg quirksDir}
done
'';
in
mkAfter copyZhaQuirks;

services.home-assistant = {
config = {
zha = mkIf (cfg.customZhaQuirks != []) {
enable_quirks = true;
custom_quirks_path = quirksDir;
};
};
};
};
}
4 changes: 2 additions & 2 deletions overlays/packages.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
let
packagesDir = ../packages;
in
{...}: final: prev: {
{lib, ...}: final: prev: {
my = prev.lib.mapAttrs' (name: value:
prev.lib.nameValuePair (prev.lib.removeSuffix ".nix" name)
(prev.callPackage "${packagesDir}/${name}" {}))
(prev.callPackage "${packagesDir}/${name}" {inherit lib;}))
(builtins.readDir packagesDir);
}
22 changes: 22 additions & 0 deletions packages/home-assistant-custom-zha-quirks/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Custom quirks for ZHA
# https://github.com/zigpy/zha-device-handlers
{
lib,
runCommandLocal,
...
}: let
mkQuirk = quirkPath:
runCommandLocal (builtins.baseNameOf quirkPath) {} ''
cp ${quirkPath} $out
'';

customQuirks = builtins.listToAttrs (
map
(quirkPath:
lib.nameValuePair
(lib.removeSuffix ".py" (builtins.baseNameOf quirkPath))
(mkQuirk quirkPath))
(lib.my.listFilesWithSuffixRecursive ".py" ./.)
);
in
customQuirks
Loading

0 comments on commit e6ac6d8

Please sign in to comment.