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_PAUSED,
STATE_PLAYING,
STATE_UNAVAILABLE,
STATE_UNKNOWN,
)
from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv
@ -74,12 +72,13 @@ async def async_setup_platform(
webfsapi_url = await AFSAPI.get_webfsapi_endpoint(
f"http://{host}:{port}/device"
)
afsapi = AFSAPI(webfsapi_url, password)
async_add_entities([AFSAPIDevice(name, afsapi)], True)
except FSConnectionError:
_LOGGER.error(
"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):
@ -188,7 +187,7 @@ class AFSAPIDevice(MediaPlayerEntity):
PlayState.STOPPED: STATE_IDLE,
PlayState.LOADING: STATE_OPENING,
None: STATE_IDLE,
}.get(status, STATE_UNKNOWN)
}.get(status)
else:
self._state = STATE_OFF
except FSConnectionError:
@ -197,32 +196,32 @@ class AFSAPIDevice(MediaPlayerEntity):
"Could not connect to %s. Did it go offline?",
self._name or afsapi.webfsapi_endpoint,
)
self._state = STATE_UNAVAILABLE
self._attr_available = False
else:
if not self._attr_available:
_LOGGER.info(
"Reconnected to %s",
self._name or afsapi.webfsapi_endpoint,
)
return
self._attr_available = True
if not self._name:
self._name = await afsapi.get_friendly_name()
if not self._attr_available:
_LOGGER.info(
"Reconnected to %s",
self._name or afsapi.webfsapi_endpoint,
)
if not self._source_list:
self.__modes_by_label = {
mode.label: mode.key for mode in await afsapi.get_modes()
}
self._source_list = list(self.__modes_by_label.keys())
self._attr_available = True
if not self._name:
self._name = await afsapi.get_friendly_name()
# 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 not self._source_list:
self.__modes_by_label = {
mode.label: mode.key for mode in await afsapi.get_modes()
}
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_text = await afsapi.get_play_text()
@ -269,7 +268,7 @@ class AFSAPIDevice(MediaPlayerEntity):
async def async_media_play_pause(self):
"""Send play/pause command."""
if "playing" in self._state:
if self._state == STATE_PLAYING:
await self.fs_device.pause()
else:
await self.fs_device.play()