Add generic group entity and image handling

This commit is contained in:
Hans Karlinius
2017-06-08 22:25:30 +02:00
parent 59862cfed8
commit d8d771acbe
2 changed files with 54 additions and 25 deletions

View File

@@ -6,11 +6,15 @@ use JSON::XS::VersionOneAndTwo;
use threads::shared; use threads::shared;
use Slim::Utils::Log; use Slim::Utils::Log;
use Slim::Utils::OSDetect;
use Slim::Utils::Prefs; use Slim::Utils::Prefs;
use Slim::Utils::Strings qw(string cstring); use Slim::Utils::Strings qw(string cstring);
use Plugins::Assistant::HASS; use Plugins::Assistant::HASS;
use constant IMAGE_PATH => 'plugins/Assistant/html/images/';
use constant IMAGE_UNKNOWN => 'group_unknown';
my $log = Slim::Utils::Log->addLogCategory( my $log = Slim::Utils::Log->addLogCategory(
{ {
'category' => 'plugin.assistant', 'category' => 'plugin.assistant',
@@ -22,6 +26,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; my %entities;
my @images = ('cover_closed', 'cover_open', 'group_on', 'group_off', 'group_unknown', 'light_off', 'light_on', 'switch_off', 'switch_on');
sub initPlugin { sub initPlugin {
@@ -41,6 +46,7 @@ sub initPlugin {
require Plugins::Assistant::Settings; require Plugins::Assistant::Settings;
Plugins::Assistant::Settings->new(); Plugins::Assistant::Settings->new();
} }
} }
sub getDisplayName { 'PLUGIN_ASSISTANT' } sub getDisplayName { 'PLUGIN_ASSISTANT' }
@@ -101,22 +107,26 @@ sub getItem {
my $gorder = 2000; my $gorder = 2000;
my $gitems = []; my $gitems = [];
# Add current to request list if all sub entities the same # Add unique entity for group of same type excluded group
# Add current entity id to args # As I do beleive is similar to what HASS does :)
# Note: Currently only light is supported my %seen;
if (!grep(!/light\./, @{$entities{$id}->{'attributes'}->{'entity_id'}})) { my @uniqueGroup = grep {not $seen{$_}++ } map { /^(?!group)(\S*)\./ } @{$entities{$id}->{'attributes'}->{'entity_id'}};
if (scalar(@uniqueGroup) == 1) {
$namespace = 'light'; $namespace = @uniqueGroup[0];
my $tid = $namespace.'.'.$name; my $tid = $namespace.'.'.$name;
$entities{$tid} = $entities{$id}; $entities{$tid} = $entities{$id};
push @{$entities{$id}->{'attributes'}->{'entity_id'}}, $tid; if (!grep(/$tid/, @{$entities{$id}->{'attributes'}->{'entity_id'}})) {
push @{$entities{$id}->{'attributes'}->{'entity_id'}}, $tid;
}
} }
foreach my $gid(@{$entities{$id}->{'attributes'}->{'entity_id'}}) { foreach my $gid(@{$entities{$id}->{'attributes'}->{'entity_id'}}) {
my $gitem = getItem($gid, %entities);
$gitem->{'order'} = $gorder++ if (!defined $gitem->{'order'});
my $gitem = getItem($gid, %entities);
$gitem->{'order'} = $gorder++ if (!defined $gitem->{'order'});
$log->debug($id.' - '.$gitem->{'name'}.' - '.$gitem->{'order'}); $log->debug($id.' - '.$gitem->{'name'}.' - '.$gitem->{'order'});
push @$gitems, $gitem; push @$gitems, $gitem;
} }
@@ -124,7 +134,7 @@ sub getItem {
return { return {
name => $entities{$id}->{'attributes'}->{'friendly_name'}, name => $entities{$id}->{'attributes'}->{'friendly_name'},
image => 'plugins/Assistant/html/images/'.$namespace.'_'.$entities{$id}->{'state'}.'.png', image => getImage($namespace.'_'.$entities{$id}->{'state'}),
order => $entities{$id}->{'attributes'}->{'order'}, order => $entities{$id}->{'attributes'}->{'order'},
type => 'link', type => 'link',
items => $gitems, items => $gitems,
@@ -134,7 +144,7 @@ sub getItem {
return { return {
name => $entities{$id}->{'attributes'}->{'friendly_name'}, name => $entities{$id}->{'attributes'}->{'friendly_name'},
image => 'plugins/Assistant/html/images/'.$namespace.'_'.$entities{$id}->{'state'}.'.png', image => getImage($namespace.'_'.$entities{$id}->{'state'}),
order => $entities{$id}->{'attributes'}->{'order'}, order => $entities{$id}->{'attributes'}->{'order'},
nextWindow => 'refresh', nextWindow => 'refresh',
type => 'link', type => 'link',
@@ -151,6 +161,7 @@ sub getItem {
} elsif ($namespace eq 'cover') { } elsif ($namespace eq 'cover') {
my $service = 'stop_cover'; my $service = 'stop_cover';
if ($entities{$id}->{'state'} eq 'closed') { if ($entities{$id}->{'state'} eq 'closed') {
$service = 'open_cover'; $service = 'open_cover';
} elsif ($entities{$id}->{'state'} eq 'open') { } elsif ($entities{$id}->{'state'} eq 'open') {
@@ -159,7 +170,7 @@ sub getItem {
return { return {
name => $entities{$id}->{'attributes'}->{'friendly_name'}, name => $entities{$id}->{'attributes'}->{'friendly_name'},
image => 'plugins/Assistant/html/images/'.$namespace.'_'.$entities{$id}->{'state'}.'.png', image => getImage($namespace.'_'.$entities{$id}->{'state'}),
order => $entities{$id}->{'attributes'}->{'order'}, order => $entities{$id}->{'attributes'}->{'order'},
nextWindow => 'refresh', nextWindow => 'refresh',
type => 'link', type => 'link',
@@ -176,6 +187,7 @@ sub getItem {
} elsif ($namespace eq 'sensor') { } elsif ($namespace eq 'sensor') {
my $name = $entities{$id}->{'attributes'}->{'friendly_name'}.' '.$entities{$id}->{'state'}.$entities{$id}->{'attributes'}->{'unit_of_measurement'}; my $name = $entities{$id}->{'attributes'}->{'friendly_name'}.' '.$entities{$id}->{'state'}.$entities{$id}->{'attributes'}->{'unit_of_measurement'};
$name =~ s/\R//g; $name =~ s/\R//g;
return { return {
@@ -195,6 +207,17 @@ sub getItem {
} }
sub getImage {
my ($img) = @_;
if (grep(/^$img$/, @images)) {
return IMAGE_PATH.$img.'.png';
} else {
return IMAGE_PATH.IMAGE_UNKNOWN.'.png';
}
}
sub servicesCall { sub servicesCall {
my ($client, $cb, $params, $args) = @_; my ($client, $cb, $params, $args) = @_;
@@ -202,15 +225,16 @@ sub servicesCall {
$client, $client,
sub { sub {
my ($client, $result, $params, $args) = @_; my ($client, $result, $params, $args) = @_;
my $newstate = ''; my $newstate = '';
foreach my $entity(@$result) {
foreach my $entity (@$result) {
if ($entity->{'entity_id'} eq $args->{'entity_id'}) { if ($entity->{'entity_id'} eq $args->{'entity_id'}) {
$newstate = $entity->{'state'}; $newstate = $entity->{'state'};
} }
} }
my $items = []; my $items = [];
push @$items, push @$items,
{ {
name => $entities{$args->{'entity_id'}}->{'attributes'}->{'friendly_name'}.' '.$newstate, name => $entities{$args->{'entity_id'}}->{'attributes'}->{'friendly_name'}.' '.$newstate,

View File

@@ -1,20 +1,25 @@
## Squeezebox Remote control Home Assistant # Squeezebox Remote control Home Assistant
This is a Plugin for Squeezebox server where you can control entities in Home Assistane. This is a Plugin for Squeezebox server where you can control entities in Home Assistane.
Supports: ## Supports
- Lights on/off
- Cover open/close
- Switch on/off
This is tested on: - Lights on/off
- WEB - Cover open/close
- Squeezebox DUET Controller - Switch on/off
- Squeezebox Radio
Known limitations: ## Tested on
- Menues are not updated without going back and forward
Server plugin repository URL https://hans99.github.io/slim/repo.xml - WEB
- Squeezebox DUET Controller
- Squeezebox Radio
## Known limitations
- Menues are not updated without going back and forward
## Installation
Server plugin repository URL `https://hans99.github.io/slim/repo.xml`
License information is found in the LICENSE file. License information is found in the LICENSE file.