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 Slim::Utils::Log;
use Slim::Utils::OSDetect;
use Slim::Utils::Prefs;
use Slim::Utils::Strings qw(string cstring);
use Plugins::Assistant::HASS;
use constant IMAGE_PATH => 'plugins/Assistant/html/images/';
use constant IMAGE_UNKNOWN => 'group_unknown';
my $log = Slim::Utils::Log->addLogCategory(
{
'category' => 'plugin.assistant',
@@ -22,6 +26,7 @@ my $log = Slim::Utils::Log->addLogCategory(
my $prefs = preferences('plugin.assistant');
my $cache = Slim::Utils::Cache->new('assistant', 3);
my %entities;
my @images = ('cover_closed', 'cover_open', 'group_on', 'group_off', 'group_unknown', 'light_off', 'light_on', 'switch_off', 'switch_on');
sub initPlugin {
@@ -41,6 +46,7 @@ sub initPlugin {
require Plugins::Assistant::Settings;
Plugins::Assistant::Settings->new();
}
}
sub getDisplayName { 'PLUGIN_ASSISTANT' }
@@ -101,22 +107,26 @@ sub getItem {
my $gorder = 2000;
my $gitems = [];
# Add current to request list if all sub entities the same
# Add current entity id to args
# Note: Currently only light is supported
if (!grep(!/light\./, @{$entities{$id}->{'attributes'}->{'entity_id'}})) {
# Add unique entity for group of same type excluded group
# As I do beleive is similar to what HASS does :)
my %seen;
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;
$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'}}) {
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'});
push @$gitems, $gitem;
}
@@ -124,7 +134,7 @@ sub getItem {
return {
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'},
type => 'link',
items => $gitems,
@@ -134,7 +144,7 @@ sub getItem {
return {
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'},
nextWindow => 'refresh',
type => 'link',
@@ -151,6 +161,7 @@ sub getItem {
} elsif ($namespace eq 'cover') {
my $service = 'stop_cover';
if ($entities{$id}->{'state'} eq 'closed') {
$service = 'open_cover';
} elsif ($entities{$id}->{'state'} eq 'open') {
@@ -159,7 +170,7 @@ sub getItem {
return {
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'},
nextWindow => 'refresh',
type => 'link',
@@ -176,6 +187,7 @@ sub getItem {
} elsif ($namespace eq 'sensor') {
my $name = $entities{$id}->{'attributes'}->{'friendly_name'}.' '.$entities{$id}->{'state'}.$entities{$id}->{'attributes'}->{'unit_of_measurement'};
$name =~ s/\R//g;
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 {
my ($client, $cb, $params, $args) = @_;
@@ -202,15 +225,16 @@ sub servicesCall {
$client,
sub {
my ($client, $result, $params, $args) = @_;
my $newstate = '';
foreach my $entity(@$result) {
foreach my $entity (@$result) {
if ($entity->{'entity_id'} eq $args->{'entity_id'}) {
$newstate = $entity->{'state'};
}
}
my $items = [];
push @$items,
{
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.
Supports:
- Lights on/off
- Cover open/close
- Switch on/off
## Supports
This is tested on:
- WEB
- Squeezebox DUET Controller
- Squeezebox Radio
- Lights on/off
- Cover open/close
- Switch on/off
Known limitations:
- Menues are not updated without going back and forward
## Tested on
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.