Skip to content

Commit

Permalink
Move the speaker check to a collection wrapper
Browse files Browse the repository at this point in the history
This will enable users to add new models if theirs isn't supported yet
It will probably result in less PR/Issues for new models to be added,
but is unfortunate, but it should also result in less forks and frustration
  • Loading branch information
duncan3dc committed Nov 27, 2024
1 parent f476b34 commit 24ee8db
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 65 deletions.
44 changes: 0 additions & 44 deletions src/Devices/Device.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,48 +184,4 @@ public function getModel(): string

return $this->model;
}


/**
* Check if this sonos device is a speaker.
*
* @return bool
*/
public function isSpeaker(): bool
{
$model = $this->getModel();

$models = [
"S1" => "PLAY:1",
"S12" => "PLAY:1",
"S3" => "PLAY:3",
"S5" => "PLAY:5",
"S6" => "PLAY:5",
"S24" => "PLAY:5",
"S9" => "PLAYBAR",
"S11" => "PLAYBASE",
"S13" => "ONE",
"S18" => "ONE",
"S14" => "BEAM",
"S31" => "BEAM",
"S15" => "CONNECT",
"S17" => "Move",
"S19" => "ARC",
"S20" => "SYMFONISK Table Lamp",
"S21" => "SYMFONISK Bookshelf",
"S29" => "SYMFONISK Picture Frame",
"S22" => "ONE SL",
"S38" => "ONE SL",
"S23" => "PORT",
"S27" => "ROAM",
"S35" => "ROAM SL",
"ZP80" => "ZONEPLAYER",
"ZP90" => "CONNECT",
"S16" => "CONNECT:AMP",
"ZP100" => "CONNECT:AMP",
"ZP120" => "CONNECT:AMP",
];

return array_key_exists($model, $models);
}
}
148 changes: 148 additions & 0 deletions src/Devices/Speakers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?php

namespace duncan3dc\Sonos\Devices;

use duncan3dc\Sonos\Interfaces\Devices\CollectionInterface;
use duncan3dc\Sonos\Interfaces\Devices\DeviceInterface;
use Psr\Log\LoggerInterface;

use function array_key_exists;

/**
* Use discovery but only for known speaker models
*/
final class Speakers implements CollectionInterface
{
private const MODELS = [
"S1" => "PLAY:1",
"S12" => "PLAY:1",
"S3" => "PLAY:3",
"S5" => "PLAY:5",
"S6" => "PLAY:5",
"S24" => "PLAY:5",
"S9" => "PLAYBAR",
"S11" => "PLAYBASE",
"S13" => "ONE",
"S18" => "ONE",
"S14" => "BEAM",
"S31" => "BEAM",
"S15" => "CONNECT",
"S17" => "Move",
"S19" => "ARC",
"S20" => "SYMFONISK Table Lamp",
"S21" => "SYMFONISK Bookshelf",
"S29" => "SYMFONISK Picture Frame",
"S22" => "ONE SL",
"S38" => "ONE SL",
"S23" => "PORT",
"S27" => "ROAM",
"S35" => "ROAM SL",
"ZP80" => "ZONEPLAYER",
"ZP90" => "CONNECT",
"S16" => "CONNECT:AMP",
"ZP100" => "CONNECT:AMP",
"ZP120" => "CONNECT:AMP",
];

/**
* @var CollectionInterface $collection The device collection to actually use.
*/
private $collection;


/**
* @param CollectionInterface $collection The device collection to actually use
*/
public function __construct(CollectionInterface $collection)
{
$this->collection = $collection;
}


/**
* Set the logger object to use.
*
* @return $this
* @var LoggerInterface $logger The logging object
*
*/
public function setLogger(LoggerInterface $logger)
{
$this->collection->setLogger($logger);

return $this;
}


/**
* Get the logger object to use.
*
* @return LoggerInterface $logger The logging object
*/
public function getLogger(): LoggerInterface
{
return $this->collection->getLogger();
}


private function isSpeaker(DeviceInterface $device): bool
{
return array_key_exists($device->getModel(), self::MODELS);
}


/**
* Add a device to this collection.
*
* @param DeviceInterface $device The device to add
*
* @return $this
*/
public function addDevice(DeviceInterface $device): CollectionInterface
{
if (!$this->isSpeaker($device)) {
$error = "This device is not recognised as a speaker model: " . $device->getModel();
throw new \InvalidArgumentException($error);
}

$this->collection->addDevice($device);
return $this;
}


/**
* Add a device to this collection using its IP address
*
* @param string $address The IP address of the device to add
*
* @return $this
*/
public function addIp(string $address): CollectionInterface
{
$this->collection->addIp($address);
return $this;
}


/**
* Get all of the devices on the current network
*
* @return DeviceInterface[]
*/
public function getDevices(): array
{
return $this->collection->getDevices();
}


/**
* Remove all devices from this collection.
*
* @return $this
*/
public function clear(): CollectionInterface
{
$this->collection->clear();
return $this;
}
}
8 changes: 0 additions & 8 deletions src/Interfaces/Devices/DeviceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,4 @@ public function soap(string $service, string $action, array $params = []);
* @return string
*/
public function getModel(): string;


/**
* Check if this sonos device is a speaker.
*
* @return bool
*/
public function isSpeaker(): bool;
}
10 changes: 2 additions & 8 deletions src/Network.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
namespace duncan3dc\Sonos;

use duncan3dc\DomParser\XmlParser;
use duncan3dc\Sonos\Devices\Collection;
use duncan3dc\Sonos\Devices\Discovery;
use duncan3dc\Sonos\Devices\Factory;
use duncan3dc\Sonos\Devices\Speakers;
use duncan3dc\Sonos\Exceptions\NotFoundException;
use duncan3dc\Sonos\Exceptions\UnknownGroupException;
use duncan3dc\Sonos\Interfaces\AlarmInterface;
Expand All @@ -16,7 +15,6 @@
use duncan3dc\Sonos\Interfaces\Services\RadioInterface;
use duncan3dc\Sonos\Interfaces\SpeakerInterface;
use duncan3dc\Sonos\Services\Radio;
use GuzzleHttp\Client;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;

Expand Down Expand Up @@ -54,7 +52,7 @@ final class Network implements NetworkInterface, LoggerAwareInterface
public function __construct(CollectionInterface $collection = null)
{
if ($collection === null) {
$collection = new Discovery();
$collection = new Speakers(new Discovery());
}
$this->collection = $collection;
}
Expand Down Expand Up @@ -106,10 +104,6 @@ public function getSpeakers(): array

$this->speakers = [];
foreach ($devices as $device) {
if (!$device->isSpeaker()) {
continue;
}

$speaker = new Speaker($device);

try {
Expand Down
5 changes: 0 additions & 5 deletions src/Speaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,6 @@ public function __construct($param)
if (preg_match("/^uuid:(.*)$/", $udn, $matches)) {
$this->uuid = $matches[1];
}

if (!$this->device->isSpeaker()) {
$error = "You cannot create a Speaker instance for this model: " . $this->device->getModel();
throw new \InvalidArgumentException($error);
}
}


Expand Down

0 comments on commit 24ee8db

Please sign in to comment.