(
        self,
        value,
        value_template,
        attributes,
        attributes_template,
        replace_attributes,
    )
Update variable.
Update variable.
Update variable .
def async_set_variable(
        self,
        value,
        value_template,
        attributes,
        attributes_template,
        replace_attributes,
    ):
        """Update variable."""
        current_state = self.hass.states.get(self.entity_id)
        updated_attributes = None
        updated_value = None

        if not replace_attributes and self._attributes is not None:
            updated_attributes = dict(self._attributes)

        if attributes is not None:
            if updated_attributes is not None:
                updated_attributes.update(attributes)
            else:
                updated_attributes = attributes

        elif attributes_template is not None:
            attributes_template.hass = self.hass

            try:
                attributes = json.loads(
                    attributes_template.async_render(
                        {"variable": current_state}
                    )
                )

                if isinstance(attributes, dict):
                    if updated_attributes is not None:
                        updated_attributes.update(attributes)
                    else:
                        updated_attributes = attributes

            except TemplateError as ex:
                _LOGGER.error(
                    "Could not render attribute_template %s: %s",
                    self.entity_id,
                    ex,
                )

        if value is not None:
            updated_value = value

        elif value_template is not None:
            try:
                value_template.hass = self.hass
                updated_value = value_template.async_render(
                    {"variable": current_state}
                )
            except TemplateError as ex:
                _LOGGER.error(
                    "Could not render value_template %s: %s",
                    self.entity_id,
                    ex,
                )

        self._attributes = updated_attributes

        if updated_value is not None:
            self._value = updated_value

        yield from self.async_update_ha_state()
def async_set_variable ( self , value , value_template , attributes , attributes_template , replace_attributes , ) : current_state = self . hass . states . get ( self . entity_id ) updated_attributes = None updated_value = None if not replace_attributes and self . _attributes is not None : updated_attributes = dict ( self . _attributes ) if attributes is not None : if updated_attributes is not None : updated_attributes . update ( attributes ) else : updated_attributes = attributes elif attributes_template is not None : attributes_template . hass = self . hass try : attributes = json . loads ( attributes_template . async_render ( { "variable" : current_state } ) ) if isinstance ( attributes , dict ) : if updated_attributes is not None : updated_attributes . update ( attributes ) else : updated_attributes = attributes except TemplateError as ex : _LOGGER . error ( "Could not render attribute_template %s: %s" , self . entity_id , ex , ) if value is not None : updated_value = value elif value_template is not None : try : value_template . hass = self . hass updated_value = value_template . async_render ( { "variable" : current_state } ) except TemplateError as ex : _LOGGER . error ( "Could not render value_template %s: %s" , self . entity_id , ex , ) self . _attributes = updated_attributes if updated_value is not None : self . _value = updated_value yield from self . async_update_ha_state ( )