This is my first complete homebrew solution to integrate my home office phone into home assistant. Basically, I like media playing while I am working, but would like to pause music if I am taking a phone call. It requires a PHP web server, MQTT broker, VVX phone, and optionally media players to control. I never programmed in PHP before, so excuse perhaps some sloppy code. I really can’t wrap my head around Python as a C# developer. Someday I will figure out how to make this a component.
Polycom only offers call state data in its XML format, this parses the XML and produces appropriate information based on what is received. Only tested on Polycom VVX 500.
polycom.php
<?php
use Mosquitto\Client;
$random = rand();
define('BROKER', 'localhost');
define('PORT', 1883);
define('CLIENT_ID', (string)"vvx_proxy_{$random}");
define('USERNAME', "username");
define('PASSWORD', "password");
$client = new Mosquitto\Client(CLIENT_ID);
$client->setCredentials(USERNAME,PASSWORD);
$responsecode = 500;
$xml = simplexml_load_string(file_get_contents("php://input"), "SimpleXMLElement", LIBXML_NOCDATA);
$mid = 0;
$client->onConnect(function() use ($client, $mid, $xml) {
//$mid = $client->publish('phone/status', print_r($xml, true), 0, false);
$mac = $xml->CallStateChangeEvent->MACAddress;
$statetopic = (string)"phone/{$mac}/state";
$attributetopic = (string)"phone/{$mac}/attributes";
$state = (string)$xml->CallStateChangeEvent->attributes()['CallState'];
$attributes = array();
$lineinfo = $xml->CallStateChangeEvent->CallLineInfo;
foreach ($lineinfo as $line) {
$linenum = $line->LineKeyNum;
$linestate = $line->LineState;
$attributes["line_{$linenum}_state"] = (string)$linestate;
$callstate = $linestate;
$calltype = "None";
$calledpartyname = "";
$callingpartyname = "";
if (property_exists($line, 'CallInfo')) {
$callinfo = $line->CallInfo;
if (property_exists($callinfo, 'CallState')) {
$callstate = $callinfo->CallState;
}
if (property_exists($callinfo, 'CallType')) {
$calltype = $callinfo->CallType;
}
if (property_exists($callinfo, 'CalledPartyName')) {
$calledpartyname = $line->CallInfo->CalledPartyName;
}
if (property_exists($callinfo, 'CallingPartyName')) {
$callingpartyname = $line->CallInfo->CallingPartyName;
}
}
$attributes["line_{$linenum}_call_state"] = (string)$callstate;
$attributes["line_{$linenum}_call_type"] = (string)$calltype;
$attributes["line_{$linenum}_called_party_name"] = (string)$calledpartyname;
$attributes["line_{$linenum}_calling_party_name"] = (string)$callingpartyname;
}
$mid = $client->publish($statetopic,$state,0,false);
$mid = $client->publish($attributetopic,json_encode($attributes),0,false);
});
$client->onPublish(function($publishedId) use ($client, $mid, $responsecode) {
$client->disconnect();
$responsecode = 200;
});
$client->connect(BROKER, PORT, 60);
$client->loopForever();
http_response_code($responsecode);
?>
mqtt sensor YAML
{mac} in the format of xxxxxxxxxxxxxx
- platform: mqtt
state_topic: “phone/{mac}/state”
name: Phone
icon: mdi:deskphone
json_attributes_topic: “phone/{mac}/attributes”
unique_id: phone_{mac}
office automation to announce caller
- id: phone_offering
alias: Phone Offering Phone Call
initial_state: ‘on’
trigger:- platform: state
entity_id: sensor.phone
to: ‘Offering’
action: - delay: 00:00:01
- service: tts.google_say
entity_id: media_player.home_speaker
data_template:
message: Incoming telephone call from {{ states.sensor.office_phone.attributes.line_1_calling_party_name }}.
- platform: state
office automation to pause playback
- id: phone_busy_disable_speaker
alias: Phone Busy Disable Speaker
initial_state: ‘on’
trigger:- platform: state
entity_id: sensor.phone
from: ‘Free’
condition:
condition: state
entity_id: media_player.office_speaker
state: ‘playing’
action: - service: automation.turn_on
entity_id: automation.phone_busy_enable_speaker - service: media_player.media_pause
entity_id: media_player.office_speaker
- platform: state
office automation to resume playback
- id: phone_busy_enable_speaker
alias: Phone Busy Enable Speaker
initial_state: ‘off’
trigger:- platform: state
entity_id: sensor.phone
to: ‘Free’
action: - service: media_player.media_play
entity_id: media_player.office_speaker - service: automation.turn_off
entity_id: automation.phone_busy_enable_speaker
- platform: state