Change light toggle for on/off and pics
This commit is contained in:
@@ -5,6 +5,7 @@ use JSON::XS::VersionOneAndTwo;
|
|||||||
use threads::shared;
|
use threads::shared;
|
||||||
|
|
||||||
use Slim::Networking::SimpleAsyncHTTP;
|
use Slim::Networking::SimpleAsyncHTTP;
|
||||||
|
use Slim::Networking::SqueezeNetwork;
|
||||||
use Slim::Utils::Log;
|
use Slim::Utils::Log;
|
||||||
use Slim::Utils::Prefs;
|
use Slim::Utils::Prefs;
|
||||||
|
|
||||||
@@ -129,24 +130,28 @@ sub getEntity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
sub toggleLightEntity {
|
sub services {
|
||||||
my ($client, $cb, $params, $args) = @_;
|
my ($client, $cb, $params, $args) = @_;
|
||||||
|
|
||||||
my $localurl = $prefs->get('connect').'services/light/toggle';
|
|
||||||
|
my $url = $prefs->get('connect').'services/'.$args->{'domain'}.'/'.$args->{'service'};
|
||||||
my $req->{'entity_id'} = $args->{'entity_id'};
|
my $req->{'entity_id'} = $args->{'entity_id'};
|
||||||
|
|
||||||
|
$log->debug($url.' { '.$req->{'entity_id'}.' }');
|
||||||
|
|
||||||
my $http = Slim::Networking::SimpleAsyncHTTP->new(
|
my $http = Slim::Networking::SimpleAsyncHTTP->new(
|
||||||
sub {
|
sub {
|
||||||
my $response = shift;
|
my $response = shift;
|
||||||
my $params = $response->params('params');
|
my $params = $response->params('params');
|
||||||
my $result;
|
my $result;
|
||||||
if ( $response->headers->content_type =~ /json/ ) {
|
if ( $response->headers->content_type =~ /json/ ) {
|
||||||
|
$log->debug($response->content);
|
||||||
$result = decode_json($response->content);
|
$result = decode_json($response->content);
|
||||||
}
|
}
|
||||||
$cb->($result);
|
$cb->($client, $result, $params, $args);
|
||||||
},
|
},
|
||||||
sub {
|
sub {
|
||||||
$log->error("Error (".$localurl."): $_[1]");
|
$log->error("Error (".$url."): $_[1]");
|
||||||
$cb->();
|
$cb->();
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -155,12 +160,14 @@ sub toggleLightEntity {
|
|||||||
);
|
);
|
||||||
|
|
||||||
$http->post(
|
$http->post(
|
||||||
$localurl,
|
$url,
|
||||||
'x-ha-access' => $prefs->get('pass'),
|
'x-ha-access' => $prefs->get('pass'),
|
||||||
'Content-Type' => 'application/json',
|
'Content-Type' => 'application/json',
|
||||||
'charset' => 'UTF-8',
|
'charset' => 'UTF-8',
|
||||||
encode_json($req),
|
encode_json($req),
|
||||||
);
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
1;
|
1;
|
||||||
|
|||||||
Binary file not shown.
|
After Width: | Height: | Size: 751 B |
Binary file not shown.
|
After Width: | Height: | Size: 655 B |
Binary file not shown.
|
After Width: | Height: | Size: 346 B |
@@ -21,6 +21,7 @@ my $log = Slim::Utils::Log->addLogCategory(
|
|||||||
|
|
||||||
my $prefs = preferences('plugin.assistant');
|
my $prefs = preferences('plugin.assistant');
|
||||||
my $cache = Slim::Utils::Cache->new('assistant', 3);
|
my $cache = Slim::Utils::Cache->new('assistant', 3);
|
||||||
|
my %entities;
|
||||||
|
|
||||||
|
|
||||||
sub initPlugin {
|
sub initPlugin {
|
||||||
@@ -56,7 +57,7 @@ sub handleFeed {
|
|||||||
$client,
|
$client,
|
||||||
sub {
|
sub {
|
||||||
my $tentities = shift;
|
my $tentities = shift;
|
||||||
my %entities;
|
|
||||||
my $items = [];
|
my $items = [];
|
||||||
my $order = 1000;
|
my $order = 1000;
|
||||||
|
|
||||||
@@ -70,7 +71,7 @@ sub handleFeed {
|
|||||||
if (($namespace eq 'group' && (!$entities{$id}->{'attributes'}->{'hidden'} || $entities{$id}->{'attributes'}->{'view'}))
|
if (($namespace eq 'group' && (!$entities{$id}->{'attributes'}->{'hidden'} || $entities{$id}->{'attributes'}->{'view'}))
|
||||||
|| $prefs->get('show_home') == 1) {
|
|| $prefs->get('show_home') == 1) {
|
||||||
|
|
||||||
my $item = getItem($id, %entities);
|
my $item = getItem($id);
|
||||||
$item->{'order'} = $order++ if (!defined $item->{'order'});
|
$item->{'order'} = $order++ if (!defined $item->{'order'});
|
||||||
$log->debug('Namespace: ', $namespace, ' Name: ', $name, ' - ', $item->{'name'}, ' - ', $item->{'order'});
|
$log->debug('Namespace: ', $namespace, ' Name: ', $name, ' - ', $item->{'name'}, ' - ', $item->{'order'});
|
||||||
push @$items, $item;
|
push @$items, $item;
|
||||||
@@ -89,7 +90,8 @@ sub handleFeed {
|
|||||||
|
|
||||||
|
|
||||||
sub getItem {
|
sub getItem {
|
||||||
my ($id, %entities) = @_;
|
|
||||||
|
my ($id) = @_;
|
||||||
my ($namespace, $name) = split('\.', $id, 2);
|
my ($namespace, $name) = split('\.', $id, 2);
|
||||||
|
|
||||||
$log->debug('Namespace: ', $namespace, ' Name: ', $name);
|
$log->debug('Namespace: ', $namespace, ' Name: ', $name);
|
||||||
@@ -104,6 +106,7 @@ sub getItem {
|
|||||||
# Note: Currently only light is supported
|
# Note: Currently only light is supported
|
||||||
if (!grep(!/light\./, @{$entities{$id}->{'attributes'}->{'entity_id'}})) {
|
if (!grep(!/light\./, @{$entities{$id}->{'attributes'}->{'entity_id'}})) {
|
||||||
|
|
||||||
|
$namespace = 'light';
|
||||||
my $tid = 'light.'.$name;
|
my $tid = 'light.'.$name;
|
||||||
if (!defined $entities{$tid}) {
|
if (!defined $entities{$tid}) {
|
||||||
$entities{$tid} = $entities{$id};
|
$entities{$tid} = $entities{$id};
|
||||||
@@ -116,13 +119,15 @@ sub getItem {
|
|||||||
foreach my $gid(@{$entities{$id}->{'attributes'}->{'entity_id'}}) {
|
foreach my $gid(@{$entities{$id}->{'attributes'}->{'entity_id'}}) {
|
||||||
my $gitem = getItem($gid, %entities);
|
my $gitem = getItem($gid, %entities);
|
||||||
$gitem->{'order'} = $gorder++ if (!defined $gitem->{'order'});
|
$gitem->{'order'} = $gorder++ if (!defined $gitem->{'order'});
|
||||||
|
|
||||||
$log->debug('Namespace: ', $namespace, ' Name: ', $name, ' - ', $gitem->{'name'}, ' - ', $gitem->{'order'});
|
$log->debug('Namespace: ', $namespace, ' Name: ', $name, ' - ', $gitem->{'name'}, ' - ', $gitem->{'order'});
|
||||||
push @$gitems, $gitem;
|
push @$gitems, $gitem;
|
||||||
}
|
}
|
||||||
$gitems = [ sort { uc($a->{order}) cmp uc($b->{order}) } @$gitems ];
|
$gitems = [ sort { uc($a->{order}) cmp uc($b->{order}) } @$gitems ];
|
||||||
|
|
||||||
return {
|
return {
|
||||||
name => $entities{$id}->{'attributes'}->{'friendly_name'}.' '.$entities{$id}->{'state'},
|
name => $entities{$id}->{'attributes'}->{'friendly_name'},
|
||||||
|
image => 'plugins/Assistant/html/images/'.$namespace.'_'.$entities{$id}->{'state'}.'.png',
|
||||||
order => $entities{$id}->{'attributes'}->{'order'},
|
order => $entities{$id}->{'attributes'}->{'order'},
|
||||||
type => 'link',
|
type => 'link',
|
||||||
items => $gitems,
|
items => $gitems,
|
||||||
@@ -134,15 +139,16 @@ sub getItem {
|
|||||||
name => $entities{$id}->{'attributes'}->{'friendly_name'},
|
name => $entities{$id}->{'attributes'}->{'friendly_name'},
|
||||||
image => 'plugins/Assistant/html/images/light_'.$entities{$id}->{'state'}.'.png',
|
image => 'plugins/Assistant/html/images/light_'.$entities{$id}->{'state'}.'.png',
|
||||||
order => $entities{$id}->{'attributes'}->{'order'},
|
order => $entities{$id}->{'attributes'}->{'order'},
|
||||||
type => 'link',
|
|
||||||
url => \&toggleLightEntity,
|
|
||||||
nextWindow => 'refresh',
|
nextWindow => 'refresh',
|
||||||
|
type => 'link',
|
||||||
|
url => \&servicesCall,
|
||||||
passthrough => [
|
passthrough => [
|
||||||
{
|
{
|
||||||
entity_id => $entities{$id}->{'entity_id'},
|
entity_id => $entities{$id}->{'entity_id'},
|
||||||
state => $entities{$id}->{'state'},
|
domain => $namespace,
|
||||||
|
service => $entities{$id}->{'state'} eq 'on' ? 'turn_off' : 'turn_on',
|
||||||
}
|
}
|
||||||
]
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
} elsif ($namespace eq 'sensor') {
|
} elsif ($namespace eq 'sensor') {
|
||||||
@@ -167,17 +173,25 @@ sub getItem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
sub toggleLightEntity {
|
sub servicesCall {
|
||||||
my ($client, $cb, $params, $args) = @_;
|
my ($client, $cb, $params, $args) = @_;
|
||||||
|
|
||||||
Plugins::Assistant::HASS::toggleLightEntity(
|
Plugins::Assistant::HASS::services(
|
||||||
$client,
|
$client,
|
||||||
sub {
|
sub {
|
||||||
my $items = [];
|
my ($client, $result, $params, $args) = @_;
|
||||||
|
|
||||||
|
my $newstate = '';
|
||||||
|
foreach my $entity(@$result) {
|
||||||
|
if ($entity->{'entity_id'} eq $args->{'entity_id'}) {
|
||||||
|
$newstate = $entity->{'state'};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
my $items = [];
|
||||||
push @$items,
|
push @$items,
|
||||||
{
|
{
|
||||||
name => 'Toggled Light',
|
name => $entities{$args->{'entity_id'}}->{'attributes'}->{'friendly_name'}.' '.$newstate,
|
||||||
type => 'text',
|
type => 'text',
|
||||||
showBriefly => 1,
|
showBriefly => 1,
|
||||||
};
|
};
|
||||||
@@ -192,4 +206,5 @@ sub toggleLightEntity {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
1;
|
1;
|
||||||
Reference in New Issue
Block a user