Address late comments for frontier silicon (#72745)

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
Thijs W 2022-05-31 09:56:25 +02:00 committed by GitHub
parent 635d7085cf
commit db9c586404
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -22,8 +22,6 @@ from homeassistant.const import (
STATE_OPENING, STATE_OPENING,
STATE_PAUSED, STATE_PAUSED,
STATE_PLAYING, STATE_PLAYING,
STATE_UNAVAILABLE,
STATE_UNKNOWN,
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
@ -74,12 +72,13 @@ async def async_setup_platform(
webfsapi_url = await AFSAPI.get_webfsapi_endpoint( webfsapi_url = await AFSAPI.get_webfsapi_endpoint(
f"http://{host}:{port}/device" f"http://{host}:{port}/device"
) )
afsapi = AFSAPI(webfsapi_url, password)
async_add_entities([AFSAPIDevice(name, afsapi)], True)
except FSConnectionError: except FSConnectionError:
_LOGGER.error( _LOGGER.error(
"Could not add the FSAPI device at %s:%s -> %s", host, port, password "Could not add the FSAPI device at %s:%s -> %s", host, port, password
) )
return
afsapi = AFSAPI(webfsapi_url, password)
async_add_entities([AFSAPIDevice(name, afsapi)], True)
class AFSAPIDevice(MediaPlayerEntity): class AFSAPIDevice(MediaPlayerEntity):
@ -188,7 +187,7 @@ class AFSAPIDevice(MediaPlayerEntity):
PlayState.STOPPED: STATE_IDLE, PlayState.STOPPED: STATE_IDLE,
PlayState.LOADING: STATE_OPENING, PlayState.LOADING: STATE_OPENING,
None: STATE_IDLE, None: STATE_IDLE,
}.get(status, STATE_UNKNOWN) }.get(status)
else: else:
self._state = STATE_OFF self._state = STATE_OFF
except FSConnectionError: except FSConnectionError:
@ -197,32 +196,32 @@ class AFSAPIDevice(MediaPlayerEntity):
"Could not connect to %s. Did it go offline?", "Could not connect to %s. Did it go offline?",
self._name or afsapi.webfsapi_endpoint, self._name or afsapi.webfsapi_endpoint,
) )
self._state = STATE_UNAVAILABLE
self._attr_available = False self._attr_available = False
else: return
if not self._attr_available:
_LOGGER.info(
"Reconnected to %s",
self._name or afsapi.webfsapi_endpoint,
)
self._attr_available = True if not self._attr_available:
if not self._name: _LOGGER.info(
self._name = await afsapi.get_friendly_name() "Reconnected to %s",
self._name or afsapi.webfsapi_endpoint,
)
if not self._source_list: self._attr_available = True
self.__modes_by_label = { if not self._name:
mode.label: mode.key for mode in await afsapi.get_modes() self._name = await afsapi.get_friendly_name()
}
self._source_list = list(self.__modes_by_label.keys())
# The API seems to include 'zero' in the number of steps (e.g. if the range is if not self._source_list:
# 0-40 then get_volume_steps returns 41) subtract one to get the max volume. self.__modes_by_label = {
# If call to get_volume fails set to 0 and try again next time. mode.label: mode.key for mode in await afsapi.get_modes()
if not self._max_volume: }
self._max_volume = int(await afsapi.get_volume_steps() or 1) - 1 self._source_list = list(self.__modes_by_label)
if self._state not in [STATE_OFF, STATE_UNAVAILABLE]: # The API seems to include 'zero' in the number of steps (e.g. if the range is
# 0-40 then get_volume_steps returns 41) subtract one to get the max volume.
# If call to get_volume fails set to 0 and try again next time.
if not self._max_volume:
self._max_volume = int(await afsapi.get_volume_steps() or 1) - 1
if self._state != STATE_OFF:
info_name = await afsapi.get_play_name() info_name = await afsapi.get_play_name()
info_text = await afsapi.get_play_text() info_text = await afsapi.get_play_text()
@ -269,7 +268,7 @@ class AFSAPIDevice(MediaPlayerEntity):
async def async_media_play_pause(self): async def async_media_play_pause(self):
"""Send play/pause command.""" """Send play/pause command."""
if "playing" in self._state: if self._state == STATE_PLAYING:
await self.fs_device.pause() await self.fs_device.pause()
else: else:
await self.fs_device.play() await self.fs_device.play()