24require_once DOL_DOCUMENT_ROOT .
'/core/lib/geturl.lib.php';
38 'espresso',
'ristretto',
'cappuccino',
'latte',
'americano',
'macchiato',
'flat_white',
'mocha',
39 'nes',
'frappe',
'freddo_espresso',
'freddo_cappuccino',
'greek',
40 'cafe_au_lait',
'noisette',
'allonge',
41 'milchkaffee',
'eiskaffee',
'pharisaer',
42 'cortado',
'cafe_con_leche',
'carajillo',
'bombon'
44 const COFFEE_INTENSITIES = array(
'mild',
'normal',
'strong');
70 "name" =>
"make_coffee",
71 "description" =>
"Prepares a cup of coffee using the local Home Assistant smart device framework.",
78 "description" =>
"The type of coffee to brew. Default is espresso. Greek: 'nes' (instant), 'frappe', 'freddo_espresso', 'freddo_cappuccino', 'greek' (ellinikos). French: 'cafe_au_lait', 'noisette' (espresso, dash of milk), 'allonge' (lungo). German: 'milchkaffee', 'eiskaffee' (with ice cream), 'pharisaer' (with rum and cream). Spanish: 'cortado', 'cafe_con_leche', 'carajillo' (with brandy), 'bombon' (condensed milk).",
79 "default" =>
"espresso"
83 "enum" => self::COFFEE_INTENSITIES,
84 "description" =>
"The strength of the coffee flavor profile.",
88 "required" => [
"type"]
112 return [
'smarthome',
'office'];
122 public function execute(
string $name, array $args)
126 return [
"error" =>
"Tool function '$name' not found."];
133 return [
"error" =>
"Tool function '$name' not found."];
145 $type = $args[
'type'] ??
'espresso';
146 $intensity = $args[
'intensity'] ??
'normal';
149 if (!in_array($type, self::COFFEE_TYPES,
true)) {
150 return [
"error" =>
"Unknown coffee type '".$type.
"'. Supported: ".implode(
', ', self::COFFEE_TYPES).
"."];
152 if (!in_array($intensity, self::COFFEE_INTENSITIES,
true)) {
153 return [
"error" =>
"Unknown intensity '".$intensity.
"'. Supported: ".implode(
', ', self::COFFEE_INTENSITIES).
"."];
158 $script_entity =
getDolGlobalString(
'AI_COFFEE_HA_SCRIPT',
'script.brew_smart_coffee');
160 if (empty($ha_url) || empty($token)) {
161 return [
"error" =>
"Home Assistant configuration is incomplete: set the constants AI_COFFEE_HA_URL and AI_COFFEE_HA_TOKEN (Home > Setup > Other), and optionally AI_COFFEE_HA_SCRIPT (default script.brew_smart_coffee)."];
167 $host = (string) parse_url($ha_url, PHP_URL_HOST);
168 $scheme = (string) parse_url($ha_url, PHP_URL_SCHEME);
169 if ($scheme ==
'http' && $host !=
'localhost' && !preg_match(
'/\.local$/', $host) && filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !==
false) {
170 dol_syslog(
"CoffeeMaker: AI_COFFEE_HA_URL uses plain http to a public host - the Home Assistant token is sent in cleartext. Use https (Nabu Casa or a TLS reverse proxy).", LOG_WARNING);
174 $script_name = str_replace(
'script.',
'', $script_entity);
175 $endpoint = rtrim($ha_url,
'/') .
'/api/services/script/' . $script_name;
178 'entity_id' => $script_entity,
179 'coffee_type' => $type,
180 'coffee_intensity' => $intensity
184 "Authorization: Bearer " . $token,
185 "Content-Type: application/json"
192 $result =
getURLContent($endpoint,
'POST', json_encode($payload_data), 1, $headers, array(
'http',
'https'), 2, -1, 5, 10);
194 if ($result[
'curl_error_no'] != 0 || !in_array($result[
'http_code'], [200, 201])) {
195 dol_syslog(
"CoffeeMaker Error: " . (empty($result[
'curl_error_msg']) ? $result[
'http_code'] : $result[
'curl_error_msg']), LOG_ERR);
196 return [
"error" =>
"Failed to communicate with coffee machine."];
200 "status" =>
"success",
201 "info" =>
"Your {$intensity} {$type} is brewing!"
Class to manage Dolibarr database access.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
getURLContent($url, $postorget='GET', $param='', $followlocation=1, $addheaders=array(), $allowedschemes=array('http', 'https'), $localurl=0, $ssl_verifypeer=-1, $timeoutconnect=0, $timeoutresponse=0, $otherCurlOptions=array(), $morelogsuffix='')
Function to get a content from an URL (use proxy if proxy defined).