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

49
HASS.pm
View File

@@ -9,30 +9,47 @@ use Slim::Utils::Log;
use Slim::Utils::Prefs;
my $log = logger('plugin.assistant');
my $url;
my $cache;
my $prefs = preferences('plugin.assistant');
sub init {
($cache) = @_;
$url = $prefs->get('connect');
#$pass = $prefs->get('pass');
}
sub testHass {
sub testHassConnection {
my ( $client, $cb, $params, $args ) = @_;
# TODO:
# empty api request
# if response message = "API running." then return true
# show in settings
if (defined $prefs->get('connect')) {
my $http = Slim::Networking::SimpleAsyncHTTP->new(
sub {
$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 {
my ( $client, $cb, $params, $args ) = @_;
@@ -83,7 +100,7 @@ sub getEntities {
sub getEntity {
my ($client, $cb, $params, $args) = @_;
my $localurl = $url.'states';
my $localurl = $prefs->get('connect').'states';
if (defined $args->{'entity_id'}) {
$localurl = $localurl.'/'.$args->{'entity_id'};
}
@@ -114,6 +131,7 @@ sub getEntity {
$localurl,
'Content-Type' => 'application/json',
'charset' => 'UTF-8',
access(),
);
}
@@ -121,7 +139,7 @@ sub getEntity {
sub toggleLightEntity {
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 $http = Slim::Networking::SimpleAsyncHTTP->new(
@@ -147,7 +165,8 @@ sub toggleLightEntity {
$localurl,
'Content-Type' => 'application/json',
'charset' => 'UTF-8',
encode_json($req)
encode_json($req),
access(),
);
}

View File

@@ -8,4 +8,11 @@
<input type="text" name="pref_pass" value="[% prefs.pass == '_pass_' ? '' : prefs.pass %]">
[% 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 %]

View File

@@ -4,7 +4,6 @@ use strict;
use base qw(Slim::Plugin::OPMLBased);
use JSON::XS::VersionOneAndTwo;
use threads::shared;
use feature qw(switch);
use Slim::Utils::Log;
use Slim::Utils::Prefs;
@@ -28,16 +27,6 @@ my $cache = Slim::Utils::Cache->new('assistant', 3);
sub initPlugin {
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);
$class->SUPER::initPlugin(
@@ -56,6 +45,7 @@ sub initPlugin {
sub getDisplayName { 'PLUGIN_ASSISTANT' }
# don't add this plugin to the Extras menu
sub playerMenu {}
@@ -66,7 +56,9 @@ sub handleFeed {
my $params = $args->{params};
# 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);
}
@@ -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'},
image => 'plugins/Assistant/html/images/light_'.$entity->{'state'}.'.png',
order => $order,
@@ -137,10 +128,11 @@ sub getItems {
state => $entity->{'state'},
}
],
#nextWindow => 'refresh',
};
} elsif ($namespace eq 'sensor' && !defined $args->{'onlygroups'}) {
#nextWindow => 'refresh',
};
} elsif ($namespace eq 'sensor' && defined $args->{'showhome'}) {
push @$items,
{
@@ -149,7 +141,7 @@ sub getItems {
type => 'text',
};
} elsif (!defined $args->{'onlygroups'}) {
} elsif (defined $args->{'showhome'}) {
push @$items,
{

View File

@@ -14,7 +14,7 @@ sub name {
sub prefs {
return ($prefs, 'connect');
return ($prefs, qw(connect pass show_home));
}
@@ -22,4 +22,22 @@ sub page {
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;

View File

@@ -2,7 +2,7 @@ PLUGIN_ASSISTANT
EN Assistant
PLUGIN_ASSISTANT_DESCRIPTION
EN This is good
EN Remote controlling entities in Home Assistant.
PLUGIN_ASSISTANT_CONNECT
EN Home Assistant connect url
@@ -10,6 +10,12 @@ PLUGIN_ASSISTANT_CONNECT
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
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
EN Home Assistant API password