Skip to content

Commit

Permalink
Fixes alarm volume (#365)
Browse files Browse the repository at this point in the history
Thanks @DurgNomis-drol πŸ’₯ πŸš€
  • Loading branch information
DurgNomis-drol authored Oct 19, 2021
1 parent b8433d6 commit 0ff6d63
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 27 deletions.
39 changes: 25 additions & 14 deletions custom_components/google_home/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ async def update_do_not_disturb(
return device

async def update_alarm_volume(
self, device: GoogleHomeDevice, volume: float | None = None
self, device: GoogleHomeDevice, volume: int | None = None
) -> GoogleHomeDevice:
"""Gets or sets the alarm volume setting on a Google Home device."""

Expand All @@ -316,16 +316,18 @@ async def update_alarm_volume(

if volume is not None:
# Setting is inverted on device
data = {JSON_ALARM_VOLUME: volume}
volume_float = float(volume / 100)
data = {JSON_ALARM_VOLUME: volume_float}
_LOGGER.debug(
"Setting Alarm Volume setting to %f on Google Home device %s",
"Setting alarm volume to %d(float=%f) on Google Home device %s",
volume,
volume_float,
device.name,
)
else:
polling = True
_LOGGER.debug(
"Getting Alarm Volume setting from Google Home device %s",
"Getting alarm volume from Google Home device %s",
device.name,
)

Expand All @@ -338,16 +340,25 @@ async def update_alarm_volume(
)
if response:
if JSON_ALARM_VOLUME in response:
volume_raw = str(response[JSON_ALARM_VOLUME])
loaded_volume = float(volume_raw)
_LOGGER.debug(
"Received Alarm Volume setting from Google Home device %s"
" - Volume: %f",
device.name,
loaded_volume,
)

device.set_alarm_volume(loaded_volume)
if polling:
volume_raw = str(response[JSON_ALARM_VOLUME])
volume_int = int(float(volume_raw) * 100)
_LOGGER.debug(
"Received alarm volume from Google Home device %s"
" - Volume: %d(raw=%s)",
device.name,
volume_int,
volume_raw,
)
else:
volume_int = volume # type: ignore
_LOGGER.debug(
"Successfully set alarm volume to %d "
"on Google Home device %s",
volume,
device.name,
)
device.set_alarm_volume(volume_int)
else:
_LOGGER.debug(
(
Expand Down
4 changes: 2 additions & 2 deletions custom_components/google_home/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@

# Defaults
DEFAULT_NAME: Final = "Google Home"
GOOGLE_HOME_ALARM_DEFAULT_VALUE: Final = 0.0
GOOGLE_HOME_ALARM_DEFAULT_VALUE: Final = 0

LABEL_ALARMS: Final = "alarms"
LABEL_ALARM_VOLUME: Final = "alarm_volume"
LABEL_ALARM_VOLUME: Final = "alarm volume"
LABEL_AVAILABLE: Final = "available"
LABEL_TIMERS: Final = "timers"
LABEL_DEVICE: Final = "device"
Expand Down
4 changes: 2 additions & 2 deletions custom_components/google_home/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ def get_do_not_disturb(self) -> bool:
"""Return Do Not Disturb status."""
return self._do_not_disturb

def set_alarm_volume(self, volume: float) -> None:
def set_alarm_volume(self, volume: int) -> None:
"""Set Alarm Volume status."""
self._alarm_volume = volume

def get_alarm_volume(self) -> float:
def get_alarm_volume(self) -> int:
"""Return Alarm Volume status."""
return self._alarm_volume

Expand Down
18 changes: 9 additions & 9 deletions custom_components/google_home/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,29 +78,29 @@ def icon(self) -> str:
volume = device.get_alarm_volume()
if volume == 0:
return ICON_ALARM_VOLUME_OFF
if volume <= 0.3:
if volume <= 30:
return ICON_ALARM_VOLUME_LOW
if volume <= 0.6:
if volume <= 60:
return ICON_ALARM_VOLUME_MID
return ICON_ALARM_VOLUME_HIGH

@property
def min_value(self) -> float:
def min_value(self) -> int:
"""Return the minimum value for the volume"""
return 0

@property
def max_value(self) -> float:
def max_value(self) -> int:
"""Return the minimum value for the volume"""
return 1
return 100

@property
def step(self) -> float:
def step(self) -> int:
"""Return the step value for the volume"""
return 0.01
return 1

@property
def value(self) -> float:
def value(self) -> int:
"""Return the current volume value"""
device = self.get_device()

Expand All @@ -110,7 +110,7 @@ def value(self) -> float:
volume = device.get_alarm_volume()
return volume

async def async_set_value(self, value: float) -> None:
async def async_set_value(self, value: int) -> None: # type: ignore
"""Sets the alarm volume"""
device = self.get_device()
if device is None:
Expand Down

0 comments on commit 0ff6d63

Please sign in to comment.