Upgrade frontier_silicon library to AFSAPI 0.2.4 (#69371)

This commit is contained in:
Thijs W 2022-05-30 18:31:57 +02:00 committed by GitHub
parent 640f53ce21
commit d5fc7e3174
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 92 additions and 62 deletions

View File

@ -407,6 +407,7 @@ omit =
homeassistant/components/fritzbox_callmonitor/const.py homeassistant/components/fritzbox_callmonitor/const.py
homeassistant/components/fritzbox_callmonitor/base.py homeassistant/components/fritzbox_callmonitor/base.py
homeassistant/components/fritzbox_callmonitor/sensor.py homeassistant/components/fritzbox_callmonitor/sensor.py
homeassistant/components/frontier_silicon/const.py
homeassistant/components/frontier_silicon/media_player.py homeassistant/components/frontier_silicon/media_player.py
homeassistant/components/futurenow/light.py homeassistant/components/futurenow/light.py
homeassistant/components/garadget/cover.py homeassistant/components/garadget/cover.py

View File

@ -364,6 +364,7 @@ build.json @home-assistant/supervisor
/tests/components/fronius/ @nielstron @farmio /tests/components/fronius/ @nielstron @farmio
/homeassistant/components/frontend/ @home-assistant/frontend /homeassistant/components/frontend/ @home-assistant/frontend
/tests/components/frontend/ @home-assistant/frontend /tests/components/frontend/ @home-assistant/frontend
/homeassistant/components/frontier_silicon/ @wlcrs
/homeassistant/components/garages_amsterdam/ @klaasnicolaas /homeassistant/components/garages_amsterdam/ @klaasnicolaas
/tests/components/garages_amsterdam/ @klaasnicolaas /tests/components/garages_amsterdam/ @klaasnicolaas
/homeassistant/components/gdacs/ @exxamalte /homeassistant/components/gdacs/ @exxamalte

View File

@ -0,0 +1,6 @@
"""Constants for the Frontier Silicon Media Player integration."""
DOMAIN = "frontier_silicon"
DEFAULT_PIN = "1234"
DEFAULT_PORT = 80

View File

@ -2,7 +2,7 @@
"domain": "frontier_silicon", "domain": "frontier_silicon",
"name": "Frontier Silicon", "name": "Frontier Silicon",
"documentation": "https://www.home-assistant.io/integrations/frontier_silicon", "documentation": "https://www.home-assistant.io/integrations/frontier_silicon",
"requirements": ["afsapi==0.0.4"], "requirements": ["afsapi==0.2.4"],
"codeowners": [], "codeowners": ["@wlcrs"],
"iot_class": "local_push" "iot_class": "local_polling"
} }

View File

@ -3,8 +3,7 @@ from __future__ import annotations
import logging import logging
from afsapi import AFSAPI from afsapi import AFSAPI, ConnectionError as FSConnectionError, PlayState
import requests
import voluptuous as vol import voluptuous as vol
from homeassistant.components.media_player import ( from homeassistant.components.media_player import (
@ -20,25 +19,27 @@ from homeassistant.const import (
CONF_PORT, CONF_PORT,
STATE_IDLE, STATE_IDLE,
STATE_OFF, STATE_OFF,
STATE_OPENING,
STATE_PAUSED, STATE_PAUSED,
STATE_PLAYING, STATE_PLAYING,
STATE_UNAVAILABLE,
STATE_UNKNOWN, 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
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
_LOGGER = logging.getLogger(__name__) from .const import DEFAULT_PIN, DEFAULT_PORT, DOMAIN
DEFAULT_PORT = 80 _LOGGER = logging.getLogger(__name__)
DEFAULT_PASSWORD = "1234"
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{ {
vol.Required(CONF_HOST): cv.string, vol.Required(CONF_HOST): cv.string,
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port, vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
vol.Optional(CONF_PASSWORD, default=DEFAULT_PASSWORD): cv.string, vol.Optional(CONF_PASSWORD, default=DEFAULT_PIN): cv.string,
vol.Optional(CONF_NAME): cv.string, vol.Optional(CONF_NAME): cv.string,
} }
) )
@ -52,8 +53,14 @@ async def async_setup_platform(
) -> None: ) -> None:
"""Set up the Frontier Silicon platform.""" """Set up the Frontier Silicon platform."""
if discovery_info is not None: if discovery_info is not None:
webfsapi_url = await AFSAPI.get_webfsapi_endpoint(
discovery_info["ssdp_description"]
)
afsapi = AFSAPI(webfsapi_url, DEFAULT_PIN)
name = await afsapi.get_friendly_name()
async_add_entities( async_add_entities(
[AFSAPIDevice(discovery_info["ssdp_description"], DEFAULT_PASSWORD, None)], [AFSAPIDevice(name, afsapi)],
True, True,
) )
return return
@ -64,11 +71,12 @@ async def async_setup_platform(
name = config.get(CONF_NAME) name = config.get(CONF_NAME)
try: try:
async_add_entities( webfsapi_url = await AFSAPI.get_webfsapi_endpoint(
[AFSAPIDevice(f"http://{host}:{port}/device", password, name)], True f"http://{host}:{port}/device"
) )
_LOGGER.debug("FSAPI device %s:%s -> %s", host, port, password) afsapi = AFSAPI(webfsapi_url, password)
except requests.exceptions.RequestException: async_add_entities([AFSAPIDevice(name, afsapi)], True)
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
) )
@ -93,10 +101,15 @@ class AFSAPIDevice(MediaPlayerEntity):
| MediaPlayerEntityFeature.SELECT_SOURCE | MediaPlayerEntityFeature.SELECT_SOURCE
) )
def __init__(self, device_url, password, name): def __init__(self, name: str | None, afsapi: AFSAPI) -> None:
"""Initialize the Frontier Silicon API device.""" """Initialize the Frontier Silicon API device."""
self._device_url = device_url self.fs_device = afsapi
self._password = password
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, afsapi.webfsapi_endpoint)},
name=name,
)
self._state = None self._state = None
self._name = name self._name = name
@ -110,17 +123,7 @@ class AFSAPIDevice(MediaPlayerEntity):
self._max_volume = None self._max_volume = None
self._volume_level = None self._volume_level = None
# Properties self.__modes_by_label = None
@property
def fs_device(self):
"""
Create a fresh fsapi session.
A new session is created for each request in case someone else
connected to the device in between the updates and invalidated the
existing session (i.e UNDOK).
"""
return AFSAPI(self._device_url, self._password)
@property @property
def name(self): def name(self):
@ -175,43 +178,61 @@ class AFSAPIDevice(MediaPlayerEntity):
async def async_update(self): async def async_update(self):
"""Get the latest date and update device state.""" """Get the latest date and update device state."""
fs_device = self.fs_device afsapi = self.fs_device
try:
if not self._name: if await afsapi.get_power():
self._name = await fs_device.get_friendly_name() status = await afsapi.get_play_status()
self._state = {
if not self._source_list: PlayState.PLAYING: STATE_PLAYING,
self._source_list = await fs_device.get_mode_list() PlayState.PAUSED: STATE_PAUSED,
PlayState.STOPPED: STATE_IDLE,
# The API seems to include 'zero' in the number of steps (e.g. if the range is PlayState.LOADING: STATE_OPENING,
# 0-40 then get_volume_steps returns 41) subtract one to get the max volume. None: STATE_IDLE,
# If call to get_volume fails set to 0 and try again next time. }.get(status, STATE_UNKNOWN)
if not self._max_volume: else:
self._max_volume = int(await fs_device.get_volume_steps() or 1) - 1 self._state = STATE_OFF
except FSConnectionError:
if await fs_device.get_power(): if self._attr_available:
status = await fs_device.get_play_status() _LOGGER.warning(
self._state = { "Could not connect to %s. Did it go offline?",
"playing": STATE_PLAYING, self._name or afsapi.webfsapi_endpoint,
"paused": STATE_PAUSED, )
"stopped": STATE_IDLE, self._state = STATE_UNAVAILABLE
"unknown": STATE_UNKNOWN, self._attr_available = False
None: STATE_IDLE,
}.get(status, STATE_UNKNOWN)
else: else:
self._state = STATE_OFF if not self._attr_available:
_LOGGER.info(
"Reconnected to %s",
self._name or afsapi.webfsapi_endpoint,
)
if self._state != STATE_OFF: self._attr_available = True
info_name = await fs_device.get_play_name() if not self._name:
info_text = await fs_device.get_play_text() self._name = await afsapi.get_friendly_name()
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())
# 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 not in [STATE_OFF, STATE_UNAVAILABLE]:
info_name = await afsapi.get_play_name()
info_text = await afsapi.get_play_text()
self._title = " - ".join(filter(None, [info_name, info_text])) self._title = " - ".join(filter(None, [info_name, info_text]))
self._artist = await fs_device.get_play_artist() self._artist = await afsapi.get_play_artist()
self._album_name = await fs_device.get_play_album() self._album_name = await afsapi.get_play_album()
self._source = await fs_device.get_mode() self._source = (await afsapi.get_mode()).label
self._mute = await fs_device.get_mute() self._mute = await afsapi.get_mute()
self._media_image_url = await fs_device.get_play_graphic() self._media_image_url = await afsapi.get_play_graphic()
volume = await self.fs_device.get_volume() volume = await self.fs_device.get_volume()
@ -296,4 +317,5 @@ class AFSAPIDevice(MediaPlayerEntity):
async def async_select_source(self, source): async def async_select_source(self, source):
"""Select input source.""" """Select input source."""
await self.fs_device.set_mode(source) await self.fs_device.set_power(True)
await self.fs_device.set_mode(self.__modes_by_label.get(source))

View File

@ -86,7 +86,7 @@ adguardhome==0.5.1
advantage_air==0.3.1 advantage_air==0.3.1
# homeassistant.components.frontier_silicon # homeassistant.components.frontier_silicon
afsapi==0.0.4 afsapi==0.2.4
# homeassistant.components.agent_dvr # homeassistant.components.agent_dvr
agent-py==0.0.23 agent-py==0.0.23