Skip to content

Commit

Permalink
Merge pull request #4 from fwestenberg/Add-enum-for-services
Browse files Browse the repository at this point in the history
Add enum for services
  • Loading branch information
fwestenberg authored Jan 24, 2023
2 parents ff0d381 + 9e0fe24 commit 4d68313
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 57 deletions.
34 changes: 33 additions & 1 deletion devialet/const.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Constants for the Devialet integration."""
import logging
from typing import Final
from enum import Enum

LOGGER = logging.getLogger(__package__)

Expand All @@ -26,3 +26,35 @@
"FrontLeft": "left",
"FrontRight": "right",
}


class UrlSuffix(Enum):
GET_GENERAL_INFO = "/ipcontrol/v1/devices/current"
GET_SOURCES = "/ipcontrol/v1/groups/current/sources"
GET_CURRENT_SOURCE = "/ipcontrol/v1/groups/current/sources/current"
GET_VOLUME = "/ipcontrol/v1/systems/current/sources/current/soundControl/volume"
GET_NIGHT_MODE = "/ipcontrol/v1/systems/current/settings/audio/nightMode"
GET_EQUALIZER = "/ipcontrol/v1/systems/current/settings/audio/equalizer"
GET_CURRENT_POSITION = (
"/ipcontrol/v1/groups/current/sources/current/playback/position"
)
SEEK = "/ipcontrol/v1/systems/current/sources/current/playback/position"
PLAY = "/ipcontrol/v1/systems/current/sources/current/playback/play"
PAUSE = "/ipcontrol/v1/systems/current/sources/current/playback/pause"
STOP = "/ipcontrol/v1/systems/current/sources/current/playback/pause"
PREVIOUS_TRACK = "/ipcontrol/v1/systems/current/sources/current/playback/previous"
NEXT_TRACK = "/ipcontrol/v1/systems/current/sources/current/playback/next"
TURN_OFF = "/ipcontrol/v1/systems/current/powerOff"
VOLUME_UP = "/ipcontrol/v1/systems/current/sources/current/soundControl/volumeUp"
VOLUME_DOWN = (
"/ipcontrol/v1/systems/current/sources/current/soundControl/volumeDown"
)
VOLUME_SET = "/ipcontrol/v1/systems/current/sources/current/soundControl/volume"
MUTE = "/ipcontrol/v1/groups/current/sources/current/playback/mute"
UNMUTE = "/ipcontrol/v1/groups/current/sources/current/playback/unmute"
EQUALIZER = "/ipcontrol/v1/systems/current/settings/audio/equalizer"
NIGHT_MODE = "/ipcontrol/v1/systems/current/settings/audio/nightMode"
SELECT_SOURCE = "/ipcontrol/v1/groups/current/sources/%SOURCE_ID%/playback/play"

def __str__(self):
return str(self.value)
80 changes: 25 additions & 55 deletions devialet/devialet_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import aiohttp

from .const import LOGGER, NORMAL_INPUTS, SPEAKER_POSITIONS
from .const import LOGGER, NORMAL_INPUTS, SPEAKER_POSITIONS, UrlSuffix


class DevialetApi:
Expand Down Expand Up @@ -37,34 +37,24 @@ async def async_update(self):
"""Get the latest details from the device."""

if self._general_info is None:
self._general_info = await self.get_request("/ipcontrol/v1/devices/current")
self._general_info = await self.get_request(UrlSuffix.GET_GENERAL_INFO)

if self._general_info is None:
return False

if self._sources is None:
self._sources = await self.get_request(
"/ipcontrol/v1/groups/current/sources"
)
self._sources = await self.get_request(UrlSuffix.GET_SOURCES)

self._source_state = await self.get_request(
"/ipcontrol/v1/groups/current/sources/current"
)
self._source_state = await self.get_request(UrlSuffix.GET_CURRENT_SOURCE)

if self._source_state is None:
return self._is_available

self._volume = await self.get_request(
"/ipcontrol/v1/systems/current/sources/current/soundControl/volume"
)
self._volume = await self.get_request(UrlSuffix.GET_VOLUME)

self._night_mode = await self.get_request(
"/ipcontrol/v1/systems/current/settings/audio/nightMode",
)
self._night_mode = await self.get_request(UrlSuffix.GET_NIGHT_MODE)

self._equalizer = await self.get_request(
"/ipcontrol/v1/systems/current/settings/audio/equalizer",
)
self._equalizer = await self.get_request(UrlSuffix.GET_EQUALIZER)

try:
self._media_duration = self._source_state["metadata"]["duration"]
Expand All @@ -77,9 +67,7 @@ async def async_update(self):
self._media_duration = None

if self._media_duration is not None:
position = await self.get_request(
"/ipcontrol/v1/groups/current/sources/current/playback/position"
)
position = await self.get_request(UrlSuffix.GET_CURRENT_POSITION)
try:
self._current_position = position["position"]
self._position_updated_at = datetime.datetime.now()
Expand Down Expand Up @@ -315,69 +303,51 @@ async def async_get_diagnostics(self):

async def async_volume_up(self) -> None:
"""Volume up media player."""
await self.post_request(
"/ipcontrol/v1/systems/current/sources/current/soundControl/volumeUp", {}
)
await self.post_request(UrlSuffix.VOLUME_UP, {})

async def async_volume_down(self) -> None:
"""Volume down media player."""
await self.post_request(
"/ipcontrol/v1/systems/current/sources/current/soundControl/volumeDown", {}
)
await self.post_request(UrlSuffix.VOLUME_DOWN, {})

async def async_set_volume_level(self, volume: float) -> None:
"""Set volume level, range 0..1."""
await self.post_request(
"/ipcontrol/v1/systems/current/sources/current/soundControl/volume",
UrlSuffix.VOLUME_SET,
{"volume": volume * 100},
)

async def async_mute_volume(self, mute: bool) -> None:
"""Mute (true) or unmute (false) media player."""
if mute:
await self.post_request(
"/ipcontrol/v1/groups/current/sources/current/playback/mute", {}
)
await self.post_request(UrlSuffix.MUTE, {})
else:
await self.post_request(
"/ipcontrol/v1/groups/current/sources/current/playback/unmute", {}
)
await self.post_request(UrlSuffix.UNMUTE, {})

async def async_media_play(self) -> None:
"""Play media player."""
await self.post_request(
"/ipcontrol/v1/systems/current/sources/current/playback/play", {}
)
await self.post_request(UrlSuffix.PLAY, {})

async def async_media_pause(self) -> None:
"""Pause media player."""
await self.post_request(
"/ipcontrol/v1/systems/current/sources/current/playback/pause", {}
)
await self.post_request(UrlSuffix.PAUSE, {})

async def async_media_stop(self) -> None:
"""Pause media player."""
await self.post_request(
"/ipcontrol/v1/systems/current/sources/current/playback/pause", {}
)
await self.post_request(UrlSuffix.PAUSE, {})

async def async_media_next_track(self) -> None:
"""Send the next track command."""
await self.post_request(
"/ipcontrol/v1/systems/current/sources/current/playback/next", {}
)
await self.post_request(UrlSuffix.NEXT_TRACK, {})

async def async_media_previous_track(self) -> None:
"""Send the previous track command."""
await self.post_request(
"/ipcontrol/v1/systems/current/sources/current/playback/previous", {}
)
await self.post_request(UrlSuffix.PREVIOUS_TRACK, {})

async def async_media_seek(self, position: float) -> None:
"""Send seek command."""

await self.post_request(
"/ipcontrol/v1/systems/current/sources/current/playback/position",
UrlSuffix.SEEK,
{"position": int(position)},
)

Expand All @@ -389,21 +359,21 @@ async def async_set_night_mode(self, night_mode: bool) -> None:
mode = "off"

await self.post_request(
"/ipcontrol/v1/systems/current/settings/audio/nightMode",
UrlSuffix.NIGHT_MODE,
{"nightMode": mode},
)

async def async_set_equalizer(self, preset: str) -> None:
"""Set the equalizer preset."""

await self.post_request(
"/ipcontrol/v1/systems/current/settings/audio/equalizer",
UrlSuffix.EQUALIZER,
{"preset": preset},
)

async def async_turn_off(self) -> None:
"""Turn off media player."""
await self.post_request("/ipcontrol/v1/systems/current/powerOff", {})
await self.post_request(UrlSuffix.TURN_OFF, {})

async def async_select_source(self, source: str) -> None:
"""Select input source."""
Expand Down Expand Up @@ -447,13 +417,13 @@ async def async_select_source(self, source: str) -> None:
return

await self.post_request(
"/ipcontrol/v1/groups/current/sources/" + source_id + "/playback/play", {}
str(UrlSuffix.SELECT_SOURCE).replace("%SOURCE_ID%", source_id), {}
)

async def get_request(self, suffix=str):
"""Generic GET method."""

url = "http://" + self._host + suffix
url = "http://" + self._host + str(suffix)

try:
async with self._session.get(
Expand Down Expand Up @@ -495,7 +465,7 @@ async def get_request(self, suffix=str):
async def post_request(self, suffix=str, body=str):
"""Generic POST method."""

url = "http://" + self._host + suffix
url = "http://" + self._host + str(suffix)

try:
async with self._session.post(
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
setup(
name='devialet',
packages=['devialet'],
version='1.3.1',
version='1.3.2',
license='MIT',
description='Devialet API',
author='fwestenberg',
Expand Down

0 comments on commit 4d68313

Please sign in to comment.