Restructure enigma2 integration to use async (#104206)

Restructure the enigma2 integration to use async
This commit is contained in:
Sid 2023-12-23 16:08:53 +01:00 committed by GitHub
parent 6da2f98d34
commit 0af850cbb6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 62 additions and 54 deletions

View File

@ -5,5 +5,5 @@
"documentation": "https://www.home-assistant.io/integrations/enigma2", "documentation": "https://www.home-assistant.io/integrations/enigma2",
"iot_class": "local_polling", "iot_class": "local_polling",
"loggers": ["openwebif"], "loggers": ["openwebif"],
"requirements": ["openwebifpy==3.2.7"] "requirements": ["openwebifpy==4.0.0"]
} }

View File

@ -1,7 +1,8 @@
"""Support for Enigma2 media players.""" """Support for Enigma2 media players."""
from __future__ import annotations from __future__ import annotations
from openwebif.api import CreateDevice from openwebif.api import OpenWebIfDevice
from openwebif.enums import RemoteControlCodes
import voluptuous as vol import voluptuous as vol
from homeassistant.components.media_player import ( from homeassistant.components.media_player import (
@ -63,10 +64,10 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
) )
def setup_platform( async def async_setup_platform(
hass: HomeAssistant, hass: HomeAssistant,
config: ConfigType, config: ConfigType,
add_devices: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None, discovery_info: DiscoveryInfoType | None = None,
) -> None: ) -> None:
"""Set up of an enigma2 media player.""" """Set up of an enigma2 media player."""
@ -85,24 +86,26 @@ def setup_platform(
config[CONF_DEEP_STANDBY] = DEFAULT_DEEP_STANDBY config[CONF_DEEP_STANDBY] = DEFAULT_DEEP_STANDBY
config[CONF_SOURCE_BOUQUET] = DEFAULT_SOURCE_BOUQUET config[CONF_SOURCE_BOUQUET] = DEFAULT_SOURCE_BOUQUET
device = CreateDevice( device = OpenWebIfDevice(
host=config[CONF_HOST], host=config[CONF_HOST],
port=config.get(CONF_PORT), port=config.get(CONF_PORT),
username=config.get(CONF_USERNAME), username=config.get(CONF_USERNAME),
password=config.get(CONF_PASSWORD), password=config.get(CONF_PASSWORD),
is_https=config[CONF_SSL], is_https=config[CONF_SSL],
prefer_picon=config.get(CONF_USE_CHANNEL_ICON),
mac_address=config.get(CONF_MAC_ADDRESS),
turn_off_to_deep=config.get(CONF_DEEP_STANDBY), turn_off_to_deep=config.get(CONF_DEEP_STANDBY),
source_bouquet=config.get(CONF_SOURCE_BOUQUET), source_bouquet=config.get(CONF_SOURCE_BOUQUET),
) )
add_devices([Enigma2Device(config[CONF_NAME], device)], True) async_add_entities(
[Enigma2Device(config[CONF_NAME], device, await device.get_about())]
)
class Enigma2Device(MediaPlayerEntity): class Enigma2Device(MediaPlayerEntity):
"""Representation of an Enigma2 box.""" """Representation of an Enigma2 box."""
_attr_has_entity_name = True
_attr_media_content_type = MediaType.TVSHOW _attr_media_content_type = MediaType.TVSHOW
_attr_supported_features = ( _attr_supported_features = (
MediaPlayerEntityFeature.VOLUME_SET MediaPlayerEntityFeature.VOLUME_SET
@ -118,10 +121,11 @@ class Enigma2Device(MediaPlayerEntity):
) )
_attr_volume_step = 5 / 100 _attr_volume_step = 5 / 100
def __init__(self, name, device): def __init__(self, name: str, device: OpenWebIfDevice, about: dict) -> None:
"""Initialize the Enigma2 device.""" """Initialize the Enigma2 device."""
self._name = name self._name = name
self.e2_box = device self._device: OpenWebIfDevice = device
self._device.mac_address = about["info"]["ifaces"][0]["mac"]
@property @property
def name(self): def name(self):
@ -131,108 +135,114 @@ class Enigma2Device(MediaPlayerEntity):
@property @property
def unique_id(self): def unique_id(self):
"""Return the unique ID for this entity.""" """Return the unique ID for this entity."""
return self.e2_box.mac_address return self._device.mac_address
@property @property
def state(self) -> MediaPlayerState: def state(self) -> MediaPlayerState:
"""Return the state of the device.""" """Return the state of the device."""
if self.e2_box.is_recording_playback: return (
return MediaPlayerState.PLAYING MediaPlayerState.OFF
return MediaPlayerState.OFF if self.e2_box.in_standby else MediaPlayerState.ON if self._device.status.in_standby
else MediaPlayerState.ON
)
@property @property
def available(self) -> bool: def available(self) -> bool:
"""Return True if the device is available.""" """Return True if the device is available."""
return not self.e2_box.is_offline return not self._device.is_offline
def turn_off(self) -> None: async def async_turn_off(self) -> None:
"""Turn off media player.""" """Turn off media player."""
self.e2_box.turn_off() await self._device.turn_off()
def turn_on(self) -> None: async def async_turn_on(self) -> None:
"""Turn the media player on.""" """Turn the media player on."""
self.e2_box.turn_on() await self._device.turn_on()
@property @property
def media_title(self): def media_title(self):
"""Title of current playing media.""" """Title of current playing media."""
return self.e2_box.current_service_channel_name return self._device.status.currservice.station
@property @property
def media_series_title(self): def media_series_title(self):
"""Return the title of current episode of TV show.""" """Return the title of current episode of TV show."""
return self.e2_box.current_programme_name return self._device.status.currservice.name
@property @property
def media_channel(self): def media_channel(self):
"""Channel of current playing media.""" """Channel of current playing media."""
return self.e2_box.current_service_channel_name return self._device.status.currservice.station
@property @property
def media_content_id(self): def media_content_id(self):
"""Service Ref of current playing media.""" """Service Ref of current playing media."""
return self.e2_box.current_service_ref return self._device.status.currservice.serviceref
@property @property
def is_volume_muted(self): def is_volume_muted(self):
"""Boolean if volume is currently muted.""" """Boolean if volume is currently muted."""
return self.e2_box.muted return self._device.status.muted
@property @property
def media_image_url(self): def media_image_url(self):
"""Picon url for the channel.""" """Picon url for the channel."""
return self.e2_box.picon_url return self._device.picon_url
def set_volume_level(self, volume: float) -> None: async def async_set_volume_level(self, volume: float) -> None:
"""Set volume level, range 0..1.""" """Set volume level, range 0..1."""
self.e2_box.set_volume(int(volume * 100)) await self._device.set_volume(int(volume * 100))
@property @property
def volume_level(self): def volume_level(self):
"""Volume level of the media player (0..1).""" """Volume level of the media player (0..1)."""
return self.e2_box.volume return (
self._device.status.volume / 100
if self._device.status.volume is not None
else None
)
def media_stop(self) -> None: async def async_media_stop(self) -> None:
"""Send stop command.""" """Send stop command."""
self.e2_box.set_stop() await self._device.send_remote_control_action(RemoteControlCodes.STOP)
def media_play(self) -> None: async def async_media_play(self) -> None:
"""Play media.""" """Play media."""
self.e2_box.toggle_play_pause() await self._device.send_remote_control_action(RemoteControlCodes.PLAY)
def media_pause(self) -> None: async def async_media_pause(self) -> None:
"""Pause the media player.""" """Pause the media player."""
self.e2_box.toggle_play_pause() await self._device.send_remote_control_action(RemoteControlCodes.PAUSE)
def media_next_track(self) -> None: async def async_media_next_track(self) -> None:
"""Send next track command.""" """Send next track command."""
self.e2_box.set_channel_up() await self._device.send_remote_control_action(RemoteControlCodes.CHANNEL_UP)
def media_previous_track(self) -> None: async def async_media_previous_track(self) -> None:
"""Send next track command.""" """Send next track command."""
self.e2_box.set_channel_down() self._device.send_remote_control_action(RemoteControlCodes.CHANNEL_DOWN)
def mute_volume(self, mute: bool) -> None: async def async_mute_volume(self, mute: bool) -> None:
"""Mute or unmute.""" """Mute or unmute."""
self.e2_box.mute_volume() await self._device.toggle_mute()
@property @property
def source(self): def source(self):
"""Return the current input source.""" """Return the current input source."""
return self.e2_box.current_service_channel_name return self._device.status.currservice.station
@property @property
def source_list(self): def source_list(self):
"""List of available input sources.""" """List of available input sources."""
return self.e2_box.source_list return self._device.source_list
def select_source(self, source: str) -> None: async def async_select_source(self, source: str) -> None:
"""Select input source.""" """Select input source."""
self.e2_box.select_source(self.e2_box.sources[source]) await self._device.zap(self._device.sources[source])
def update(self) -> None: async def async_update(self) -> None:
"""Update state of the media_player.""" """Update state of the media_player."""
self.e2_box.update() await self._device.update()
@property @property
def extra_state_attributes(self): def extra_state_attributes(self):
@ -243,13 +253,11 @@ class Enigma2Device(MediaPlayerEntity):
currservice_begin: is in the format '21:00'. currservice_begin: is in the format '21:00'.
currservice_end: is in the format '21:00'. currservice_end: is in the format '21:00'.
""" """
if self.e2_box.in_standby: if self._device.status.in_standby:
return {} return {}
return { return {
ATTR_MEDIA_CURRENTLY_RECORDING: self.e2_box.status_info["isRecording"], ATTR_MEDIA_CURRENTLY_RECORDING: self._device.status.is_recording,
ATTR_MEDIA_DESCRIPTION: self.e2_box.status_info[ ATTR_MEDIA_DESCRIPTION: self._device.status.currservice.fulldescription,
"currservice_fulldescription" ATTR_MEDIA_START_TIME: self._device.status.currservice.begin,
], ATTR_MEDIA_END_TIME: self._device.status.currservice.end,
ATTR_MEDIA_START_TIME: self.e2_box.status_info["currservice_begin"],
ATTR_MEDIA_END_TIME: self.e2_box.status_info["currservice_end"],
} }

View File

@ -1425,7 +1425,7 @@ openhomedevice==2.2.0
opensensemap-api==0.2.0 opensensemap-api==0.2.0
# homeassistant.components.enigma2 # homeassistant.components.enigma2
openwebifpy==3.2.7 openwebifpy==4.0.0
# homeassistant.components.luci # homeassistant.components.luci
openwrt-luci-rpc==1.1.16 openwrt-luci-rpc==1.1.16