@@ -98,12 +98,19 @@ def notify_var_get(cls, var_names, new_vars):
98
98
return notify_vars
99
99
100
100
@classmethod
101
- def set (cls , var_name , value , new_attributes = None , ** kwargs ):
101
+ def set (cls , var_name , value = None , new_attributes = None , ** kwargs ):
102
102
"""Set a state variable and optional attributes in hass."""
103
103
if var_name .count ("." ) != 1 :
104
104
raise NameError (f"invalid name { var_name } (should be 'domain.entity')" )
105
- if new_attributes is None :
105
+
106
+ state_value = None
107
+ if value is None or new_attributes is None :
106
108
state_value = cls .hass .states .get (var_name )
109
+
110
+ if value is None and state_value :
111
+ value = state_value .state
112
+
113
+ if new_attributes is None :
107
114
if state_value :
108
115
new_attributes = state_value .attributes
109
116
else :
@@ -115,6 +122,18 @@ def set(cls, var_name, value, new_attributes=None, **kwargs):
115
122
cls .notify_var_last [var_name ] = str (value )
116
123
cls .hass .states .async_set (var_name , value , new_attributes )
117
124
125
+ @classmethod
126
+ def set_attr (cls , var_attr_name , value ):
127
+ """Set a state variable's attribute in hass."""
128
+ parts = var_attr_name .split ("." )
129
+ if len (parts ) != 3 :
130
+ raise NameError (f"invalid name { var_attr_name } (should be 'domain.entity.attr')" )
131
+
132
+ state_var_name = f"{ parts [0 ]} .{ parts [1 ]} "
133
+ attr_name = parts [2 ]
134
+
135
+ cls .set (state_var_name , ** {attr_name : value })
136
+
118
137
@classmethod
119
138
def exist (cls , var_name ):
120
139
"""Check if a state variable value or attribute exists in hass."""
0 commit comments