Commit 69c61b0
authored
fix: Restore the engine user's saved theme after dashboard screenshots (#1912)
* fix: Restore the engine user's saved theme after dashboard screenshots
Stock Puppet dispatches a settheme event on every cold-browser render,
and its dark query flag is presence-based, so a default screenshot
reaches the frontend as an explicit light selection. Home Assistant
persists that per-user (frontend/set_user_data, key "theme") and syncs
it to every real session of the engine token's user — flipping a
dark-mode user's web and mobile UI to light on each capture (#1909).
Bracket every capture batch with a ThemeGuard: read the engine user's
saved theme before rendering and write it back afterwards when the
render changed it. In add-on mode the guard authenticates with the
Puppet add-on's own access_token/home_assistant_url options (already
fetched during Supervisor discovery, kept in process memory only); in
sidecar/standalone/OAuth mode it falls back to ha-mcp's direct HA
credentials, which protect the user whenever both tokens belong to the
same account. Guard failures never break a capture; an attempted but
failed snapshot/restore surfaces as a tool-response warning.
Fixes #1909
* fix: Harden the theme guard per review findings
Review-toolkit and bot findings, batched:
- Surface guard warnings on the error path too: a failing batch may
already have rendered (and clobbered the theme), so the raised
ToolError payload now carries them, and the set-path degraded-warning
handler merges them into the response warnings (codex P2 + toolkit).
- Gate the client-credential fallback on is_running_in_addon() instead
of a raw SUPERVISOR_TOKEN check so embedded mode (HA core container
env, plain admin client token) keeps theme protection (codex P2).
- Restore a never-configured baseline as {} rather than null: live
frontend sessions ignore a null subscription push and would stay
flipped until reload; an empty settings object re-applies default
behavior immediately and means the same thing on the next boot
(codex P2, verified against frontend themes-mixin).
- Carry a narrow frozen EngineCredential through EngineTarget instead
of the raw secret-bearing Supervisor options dict; privatize the
guard's snapshot lifecycle state; type the WebSocket handle.
- Drop the production-dead resolve_engine_url wrapper.
- Defensively tolerate malformed get_user_data responses (Gemini).
- Note the settheme-before-response ordering assumption; disambiguate
'standalone' wording in docs/beta.md.
- Tests: guard-warning propagation into both tool responses, the
client-credential fallback through the full capture bracket, embedded
mode, error-path warning delivery, and malformed-response tolerance.
* fix: Close theme-guard gaps from the second review round
- Discover the Puppet add-on credential best-effort even when
HAMCP_DASHBOARD_SCREENSHOT_ENGINE_URL is set on HA OS, so an explicit
engine URL no longer disables the theme guard in add-on mode.
- Wait RESTORE_SETTLE_SECONDS before the post-capture read so a
low-wait_ms render cannot race the frontend's async user-data write
(a stale read would skip the restore and let the late write survive).
- Keep theme-guard warnings visible when image packaging fails after a
successful render, on both the standalone and config tool paths.
---------
Co-authored-by: kingpanther13 <kingpanther13@users.noreply.github.com>1 parent f1de58a commit 69c61b0
11 files changed
Lines changed: 1258 additions & 209 deletions
File tree
- docs
- src/ha_mcp
- dashboard_screenshot
- tools
- tests/src
- e2e/tools
- unit
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
175 | 175 | | |
176 | 176 | | |
177 | 177 | | |
178 | | - | |
179 | | - | |
180 | | - | |
181 | | - | |
182 | | - | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
183 | 191 | | |
184 | 192 | | |
185 | 193 | | |
| |||
254 | 262 | | |
255 | 263 | | |
256 | 264 | | |
257 | | - | |
258 | | - | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
259 | 268 | | |
260 | 269 | | |
261 | 270 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
836 | 836 | | |
837 | 837 | | |
838 | 838 | | |
839 | | - | |
| 839 | + | |
840 | 840 | | |
841 | 841 | | |
842 | 842 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
22 | | - | |
| 22 | + | |
| 23 | + | |
23 | 24 | | |
24 | 25 | | |
25 | 26 | | |
26 | 27 | | |
27 | 28 | | |
28 | 29 | | |
29 | 30 | | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
30 | 34 | | |
31 | | - | |
| 35 | + | |
32 | 36 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
6 | | - | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
7 | 10 | | |
8 | 11 | | |
9 | 12 | | |
| |||
20 | 23 | | |
21 | 24 | | |
22 | 25 | | |
23 | | - | |
| 26 | + | |
| 27 | + | |
24 | 28 | | |
25 | 29 | | |
26 | 30 | | |
| |||
757 | 761 | | |
758 | 762 | | |
759 | 763 | | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
760 | 784 | | |
761 | 785 | | |
762 | 786 | | |
| 787 | + | |
763 | 788 | | |
764 | 789 | | |
765 | 790 | | |
| |||
769 | 794 | | |
770 | 795 | | |
771 | 796 | | |
| 797 | + | |
| 798 | + | |
| 799 | + | |
| 800 | + | |
| 801 | + | |
772 | 802 | | |
773 | 803 | | |
774 | 804 | | |
775 | 805 | | |
| 806 | + | |
| 807 | + | |
| 808 | + | |
| 809 | + | |
| 810 | + | |
| 811 | + | |
| 812 | + | |
| 813 | + | |
| 814 | + | |
| 815 | + | |
| 816 | + | |
| 817 | + | |
| 818 | + | |
| 819 | + | |
| 820 | + | |
| 821 | + | |
| 822 | + | |
| 823 | + | |
| 824 | + | |
| 825 | + | |
| 826 | + | |
| 827 | + | |
| 828 | + | |
| 829 | + | |
| 830 | + | |
| 831 | + | |
776 | 832 | | |
777 | 833 | | |
778 | 834 | | |
| |||
789 | 845 | | |
790 | 846 | | |
791 | 847 | | |
| 848 | + | |
| 849 | + | |
792 | 850 | | |
793 | 851 | | |
794 | 852 | | |
795 | 853 | | |
796 | 854 | | |
797 | 855 | | |
798 | 856 | | |
| 857 | + | |
| 858 | + | |
| 859 | + | |
| 860 | + | |
| 861 | + | |
| 862 | + | |
799 | 863 | | |
800 | 864 | | |
801 | 865 | | |
| |||
814 | 878 | | |
815 | 879 | | |
816 | 880 | | |
817 | | - | |
| 881 | + | |
| 882 | + | |
818 | 883 | | |
819 | 884 | | |
820 | 885 | | |
821 | 886 | | |
822 | | - | |
823 | | - | |
824 | | - | |
825 | | - | |
826 | | - | |
827 | | - | |
828 | | - | |
829 | | - | |
830 | | - | |
831 | | - | |
832 | | - | |
833 | | - | |
834 | | - | |
835 | | - | |
836 | | - | |
837 | | - | |
838 | | - | |
839 | | - | |
840 | | - | |
841 | | - | |
842 | | - | |
843 | | - | |
844 | | - | |
845 | | - | |
846 | | - | |
847 | | - | |
848 | | - | |
849 | | - | |
850 | | - | |
851 | | - | |
852 | | - | |
853 | | - | |
854 | | - | |
855 | | - | |
856 | | - | |
857 | | - | |
858 | | - | |
859 | | - | |
860 | | - | |
861 | | - | |
862 | | - | |
863 | | - | |
864 | | - | |
| 887 | + | |
| 888 | + | |
| 889 | + | |
| 890 | + | |
| 891 | + | |
| 892 | + | |
| 893 | + | |
| 894 | + | |
| 895 | + | |
| 896 | + | |
| 897 | + | |
| 898 | + | |
| 899 | + | |
| 900 | + | |
| 901 | + | |
| 902 | + | |
| 903 | + | |
| 904 | + | |
| 905 | + | |
| 906 | + | |
| 907 | + | |
| 908 | + | |
| 909 | + | |
| 910 | + | |
| 911 | + | |
| 912 | + | |
| 913 | + | |
| 914 | + | |
| 915 | + | |
| 916 | + | |
| 917 | + | |
| 918 | + | |
| 919 | + | |
| 920 | + | |
| 921 | + | |
| 922 | + | |
| 923 | + | |
| 924 | + | |
| 925 | + | |
| 926 | + | |
| 927 | + | |
865 | 928 | | |
866 | | - | |
867 | | - | |
868 | | - | |
869 | | - | |
870 | | - | |
871 | | - | |
872 | | - | |
873 | | - | |
874 | | - | |
875 | | - | |
876 | | - | |
877 | | - | |
878 | | - | |
879 | | - | |
880 | | - | |
881 | | - | |
882 | | - | |
883 | | - | |
884 | | - | |
885 | | - | |
886 | | - | |
887 | | - | |
888 | 929 | | |
889 | | - | |
890 | | - | |
891 | | - | |
892 | | - | |
893 | | - | |
894 | | - | |
895 | | - | |
896 | | - | |
897 | | - | |
898 | | - | |
899 | | - | |
900 | | - | |
901 | | - | |
| 930 | + | |
| 931 | + | |
| 932 | + | |
| 933 | + | |
| 934 | + | |
| 935 | + | |
| 936 | + | |
902 | 937 | | |
903 | | - | |
| 938 | + | |
| 939 | + | |
| 940 | + | |
| 941 | + | |
| 942 | + | |
| 943 | + | |
904 | 944 | | |
905 | | - | |
906 | | - | |
907 | | - | |
| 945 | + | |
| 946 | + | |
| 947 | + | |
| 948 | + | |
| 949 | + | |
| 950 | + | |
| 951 | + | |
| 952 | + | |
| 953 | + | |
| 954 | + | |
| 955 | + | |
| 956 | + | |
| 957 | + | |
| 958 | + | |
| 959 | + | |
| 960 | + | |
| 961 | + | |
| 962 | + | |
| 963 | + | |
| 964 | + | |
| 965 | + | |
| 966 | + | |
| 967 | + | |
| 968 | + | |
| 969 | + | |
| 970 | + | |
| 971 | + | |
| 972 | + | |
| 973 | + | |
| 974 | + | |
| 975 | + | |
| 976 | + | |
| 977 | + | |
| 978 | + | |
| 979 | + | |
0 commit comments