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