Settings update

This commit is contained in:
Hans Karlinius
2017-05-21 10:43:37 +02:00
parent d5adcb6c00
commit ea4ef81757
5 changed files with 78 additions and 36 deletions
+34 -15
View File
@@ -9,30 +9,47 @@ use Slim::Utils::Log;
use Slim::Utils::Prefs; use Slim::Utils::Prefs;
my $log = logger('plugin.assistant'); my $log = logger('plugin.assistant');
my $url;
my $cache; my $cache;
my $prefs = preferences('plugin.assistant'); my $prefs = preferences('plugin.assistant');
sub init { sub init {
($cache) = @_; ($cache) = @_;
$url = $prefs->get('connect');
#$pass = $prefs->get('pass');
} }
sub testHass { sub testHassConnection {
my ( $client, $cb, $params, $args ) = @_;
# TODO: if (defined $prefs->get('connect')) {
# empty api request my $http = Slim::Networking::SimpleAsyncHTTP->new(
# if response message = "API running." then return true sub {
# show in settings $log->info("Connected to Home Assistant at (".$prefs->get('connect').")");
},
sub {
$log->warn("Warning (".$prefs->get('connect')."): $_[1]");
},
{
timeout => 5,
},
);
$http->get(
$prefs->get('connect'),
'Content-Type' => 'application/json',
'charset' => 'UTF-8',
access(),
);
}
} }
sub access() {
if ($prefs->get('pass')) {
$log->debug('PASS');
return 'x-ha-access' => $prefs->get('pass');
}
return !defined;
}
sub getEntities { sub getEntities {
my ( $client, $cb, $params, $args ) = @_; my ( $client, $cb, $params, $args ) = @_;
@@ -83,7 +100,7 @@ sub getEntities {
sub getEntity { sub getEntity {
my ($client, $cb, $params, $args) = @_; my ($client, $cb, $params, $args) = @_;
my $localurl = $url.'states'; my $localurl = $prefs->get('connect').'states';
if (defined $args->{'entity_id'}) { if (defined $args->{'entity_id'}) {
$localurl = $localurl.'/'.$args->{'entity_id'}; $localurl = $localurl.'/'.$args->{'entity_id'};
} }
@@ -114,6 +131,7 @@ sub getEntity {
$localurl, $localurl,
'Content-Type' => 'application/json', 'Content-Type' => 'application/json',
'charset' => 'UTF-8', 'charset' => 'UTF-8',
access(),
); );
} }
@@ -121,7 +139,7 @@ sub getEntity {
sub toggleLightEntity { sub toggleLightEntity {
my ($client, $cb, $params, $args) = @_; my ($client, $cb, $params, $args) = @_;
my $localurl = $url.'services/light/toggle'; my $localurl = $prefs->get('connect').'services/light/toggle';
my $req->{'entity_id'} = $args->{'entity_id'}; my $req->{'entity_id'} = $args->{'entity_id'};
my $http = Slim::Networking::SimpleAsyncHTTP->new( my $http = Slim::Networking::SimpleAsyncHTTP->new(
@@ -147,7 +165,8 @@ sub toggleLightEntity {
$localurl, $localurl,
'Content-Type' => 'application/json', 'Content-Type' => 'application/json',
'charset' => 'UTF-8', 'charset' => 'UTF-8',
encode_json($req) encode_json($req),
access(),
); );
} }
+7
View File
@@ -8,4 +8,11 @@
<input type="text" name="pref_pass" value="[% prefs.pass == '_pass_' ? '' : prefs.pass %]"> <input type="text" name="pref_pass" value="[% prefs.pass == '_pass_' ? '' : prefs.pass %]">
[% END %] [% END %]
[% WRAPPER setting title="PLUGIN_ASSISTANT_SHOW_HOME" desc="PLUGIN_ASSISTANT_SHOW_HOME_DESC" %]
<select class="stdedit" name="pref_show_home">
<option [% IF NOT prefs.show_home %]selected [% END %]value="0">[% 'NO' | getstring %]</option>
<option [% IF prefs.show_home %]selected [% END %]value="1">[% 'YES' | getstring %]</option>
</select>
[% END %]
[% PROCESS settings/footer.html %] [% PROCESS settings/footer.html %]
+11 -19
View File
@@ -4,7 +4,6 @@ use strict;
use base qw(Slim::Plugin::OPMLBased); use base qw(Slim::Plugin::OPMLBased);
use JSON::XS::VersionOneAndTwo; use JSON::XS::VersionOneAndTwo;
use threads::shared; use threads::shared;
use feature qw(switch);
use Slim::Utils::Log; use Slim::Utils::Log;
use Slim::Utils::Prefs; use Slim::Utils::Prefs;
@@ -28,16 +27,6 @@ my $cache = Slim::Utils::Cache->new('assistant', 3);
sub initPlugin { sub initPlugin {
my $class = shift; my $class = shift;
if (my $username = $prefs->get('connect')) {
$prefs->set('connect', '') if $username eq '_assistant_';
}
$prefs->init(
{
connect => '_assistant_'
}
);
Plugins::Assistant::HASS->init($cache); Plugins::Assistant::HASS->init($cache);
$class->SUPER::initPlugin( $class->SUPER::initPlugin(
@@ -56,6 +45,7 @@ sub initPlugin {
sub getDisplayName { 'PLUGIN_ASSISTANT' } sub getDisplayName { 'PLUGIN_ASSISTANT' }
# don't add this plugin to the Extras menu # don't add this plugin to the Extras menu
sub playerMenu {} sub playerMenu {}
@@ -66,7 +56,9 @@ sub handleFeed {
my $params = $args->{params}; my $params = $args->{params};
# Only groups in first level # Only groups in first level
$args->{'onlygroups'} = 1; if (defined $prefs->get('show_home') && $prefs->get('show_home') == 1) {
$args->{'showhome'} = $prefs->get('show_home');
}
getItems($client,$cb,$params,$args); getItems($client,$cb,$params,$args);
} }
@@ -122,10 +114,9 @@ sub getItems {
] ]
}; };
} elsif ($namespace eq 'light' && !defined $args->{'onlygroups'}) { } elsif ($namespace eq 'light' && defined $args->{'showhome'}) {
push @$items, push @$items,{
{
name => $entity->{'attributes'}->{'friendly_name'}, name => $entity->{'attributes'}->{'friendly_name'},
image => 'plugins/Assistant/html/images/light_'.$entity->{'state'}.'.png', image => 'plugins/Assistant/html/images/light_'.$entity->{'state'}.'.png',
order => $order, order => $order,
@@ -137,10 +128,11 @@ sub getItems {
state => $entity->{'state'}, state => $entity->{'state'},
} }
], ],
#nextWindow => 'refresh',
};
} elsif ($namespace eq 'sensor' && !defined $args->{'onlygroups'}) { #nextWindow => 'refresh',
};
} elsif ($namespace eq 'sensor' && defined $args->{'showhome'}) {
push @$items, push @$items,
{ {
@@ -149,7 +141,7 @@ sub getItems {
type => 'text', type => 'text',
}; };
} elsif (!defined $args->{'onlygroups'}) { } elsif (defined $args->{'showhome'}) {
push @$items, push @$items,
{ {
+19 -1
View File
@@ -14,7 +14,7 @@ sub name {
sub prefs { sub prefs {
return ($prefs, 'connect'); return ($prefs, qw(connect pass show_home));
} }
@@ -22,4 +22,22 @@ sub page {
return 'plugins/Assistant/settings.html'; return 'plugins/Assistant/settings.html';
} }
sub handler {
my ($class, $client, $params, $callback, @args) = @_;
if ( $params->{saveSettings} ) {
$prefs->set('connect', $params->{pref_connect});
$prefs->set('pass', $params->{pref_pass});
$prefs->set('pref_show_home', $params->{pref_show_home});
$prefs->savenow();
}
if ( $prefs->get('connect') ) {
Plugins::Assistant::HASS->testHassConnection();
}
return $class->SUPER::handler($client, $params);
}
1; 1;
+7 -1
View File
@@ -2,7 +2,7 @@ PLUGIN_ASSISTANT
EN Assistant EN Assistant
PLUGIN_ASSISTANT_DESCRIPTION PLUGIN_ASSISTANT_DESCRIPTION
EN This is good EN Remote controlling entities in Home Assistant.
PLUGIN_ASSISTANT_CONNECT PLUGIN_ASSISTANT_CONNECT
EN Home Assistant connect url EN Home Assistant connect url
@@ -10,6 +10,12 @@ PLUGIN_ASSISTANT_CONNECT
PLUGIN_ASSISTANT_CONNECT_DESC PLUGIN_ASSISTANT_CONNECT_DESC
EN Should be the same as used for web access with addition of /api at the end like http://localhost:8123/api EN Should be the same as used for web access with addition of /api at the end like http://localhost:8123/api
PLUGIN_ASSISTANT_SHOW_HOME
EN Show "home" in menu
PLUGIN_ASSISTANT_SHOW_HOME_DESC
EN By not showing "home" only groups will be shown for a cleaner looking menu.
PLUGIN_ASSISTANT_PASS PLUGIN_ASSISTANT_PASS
EN Home Assistant API password EN Home Assistant API password