Skip to content

Commit f6e8cf9

Browse files
committed
Fix crash in NAD integration
Some amplifiers/receivers do not report any volume (due to them not knowing), for example NAD C 356BEE is documented to return an empty string for volume. At least don't crash when we get None back. The type hint is optional int or float, since upstream is in the process of adding float (some amps report floats back) and some report nothing.
1 parent 224c874 commit f6e8cf9

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

homeassistant/components/nad/media_player.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Support for interfacing with NAD receivers through RS-232."""
22
import logging
3+
from typing import Optional, Union
34

45
from nad_receiver import NADReceiver, NADReceiverTCP, NADReceiverTelnet
56
import voluptuous as vol
@@ -200,12 +201,16 @@ def update(self):
200201
self._volume = self.calc_volume(self._nad_receiver.main_volume("?"))
201202
self._source = self._source_dict.get(self._nad_receiver.main_source("?"))
202203

203-
def calc_volume(self, decibel):
204+
def calc_volume(self, decibel: Optional[Union[int, float]]) -> Optional[float]:
204205
"""
205206
Calculate the volume given the decibel.
206207
207208
Return the volume (0..1).
208209
"""
210+
# Some receivers cannot report the volume, e.g. C 356BEE,
211+
# instead they only support stepping the volume up or down
212+
if decibel is None:
213+
return None
209214
return abs(self._min_volume - decibel) / abs(
210215
self._min_volume - self._max_volume
211216
)

0 commit comments

Comments
 (0)