Use shorthand attributes in enigma2 (#106318)

* enigma2: add strict typing, change property functions to _attr_*

* applied changes of review

* changes from review
This commit is contained in:
Sid
2023-12-23 23:12:03 +01:00
committed by GitHub
parent 4c912fcf1b
commit bd6e2c54e1
3 changed files with 40 additions and 88 deletions

View File

@ -124,6 +124,7 @@ homeassistant.components.elgato.*
homeassistant.components.elkm1.*
homeassistant.components.emulated_hue.*
homeassistant.components.energy.*
homeassistant.components.enigma2.*
homeassistant.components.esphome.*
homeassistant.components.event.*
homeassistant.components.evil_genius_labs.*

View File

@ -123,33 +123,11 @@ class Enigma2Device(MediaPlayerEntity):
def __init__(self, name: str, device: OpenWebIfDevice, about: dict) -> None:
"""Initialize the Enigma2 device."""
self._name = name
self._device: OpenWebIfDevice = device
self._device.mac_address = about["info"]["ifaces"][0]["mac"]
@property
def name(self):
"""Return the name of the device."""
return self._name
@property
def unique_id(self):
"""Return the unique ID for this entity."""
return self._device.mac_address
@property
def state(self) -> MediaPlayerState:
"""Return the state of the device."""
return (
MediaPlayerState.OFF
if self._device.status.in_standby
else MediaPlayerState.ON
)
@property
def available(self) -> bool:
"""Return True if the device is available."""
return not self._device.is_offline
self._attr_name = name
self._attr_unique_id = device.mac_address
async def async_turn_off(self) -> None:
"""Turn off media player."""
@ -159,49 +137,10 @@ class Enigma2Device(MediaPlayerEntity):
"""Turn the media player on."""
await self._device.turn_on()
@property
def media_title(self):
"""Title of current playing media."""
return self._device.status.currservice.station
@property
def media_series_title(self):
"""Return the title of current episode of TV show."""
return self._device.status.currservice.name
@property
def media_channel(self):
"""Channel of current playing media."""
return self._device.status.currservice.station
@property
def media_content_id(self):
"""Service Ref of current playing media."""
return self._device.status.currservice.serviceref
@property
def is_volume_muted(self):
"""Boolean if volume is currently muted."""
return self._device.status.muted
@property
def media_image_url(self):
"""Picon url for the channel."""
return self._device.picon_url
async def async_set_volume_level(self, volume: float) -> None:
"""Set volume level, range 0..1."""
await self._device.set_volume(int(volume * 100))
@property
def volume_level(self):
"""Volume level of the media player (0..1)."""
return (
self._device.status.volume / 100
if self._device.status.volume is not None
else None
)
async def async_media_stop(self) -> None:
"""Send stop command."""
await self._device.send_remote_control_action(RemoteControlCodes.STOP)
@ -226,16 +165,6 @@ class Enigma2Device(MediaPlayerEntity):
"""Mute or unmute."""
await self._device.toggle_mute()
@property
def source(self):
"""Return the current input source."""
return self._device.status.currservice.station
@property
def source_list(self):
"""List of available input sources."""
return self._device.source_list
async def async_select_source(self, source: str) -> None:
"""Select input source."""
await self._device.zap(self._device.sources[source])
@ -243,21 +172,33 @@ class Enigma2Device(MediaPlayerEntity):
async def async_update(self) -> None:
"""Update state of the media_player."""
await self._device.update()
self._attr_available = not self._device.is_offline
@property
def extra_state_attributes(self):
"""Return device specific state attributes.
if not self._device.status.in_standby:
self._attr_extra_state_attributes = {
ATTR_MEDIA_CURRENTLY_RECORDING: self._device.status.is_recording,
ATTR_MEDIA_DESCRIPTION: self._device.status.currservice.fulldescription,
ATTR_MEDIA_START_TIME: self._device.status.currservice.begin,
ATTR_MEDIA_END_TIME: self._device.status.currservice.end,
}
else:
self._attr_extra_state_attributes = {}
self._attr_media_title = self._device.status.currservice.station
self._attr_media_series_title = self._device.status.currservice.name
self._attr_media_channel = self._device.status.currservice.station
self._attr_is_volume_muted = self._device.status.muted
self._attr_media_content_id = self._device.status.currservice.serviceref
self._attr_media_image_url = self._device.picon_url
self._attr_source = self._device.status.currservice.station
self._attr_source_list = self._device.source_list
isRecording: Is the box currently recording.
currservice_fulldescription: Full program description.
currservice_begin: is in the format '21:00'.
currservice_end: is in the format '21:00'.
"""
if self._device.status.in_standby:
return {}
return {
ATTR_MEDIA_CURRENTLY_RECORDING: self._device.status.is_recording,
ATTR_MEDIA_DESCRIPTION: self._device.status.currservice.fulldescription,
ATTR_MEDIA_START_TIME: self._device.status.currservice.begin,
ATTR_MEDIA_END_TIME: self._device.status.currservice.end,
}
self._attr_state = MediaPlayerState.OFF
else:
self._attr_state = MediaPlayerState.ON
if (volume_level := self._device.status.volume) is not None:
self._attr_volume_level = volume_level / 100
else:
self._attr_volume_level = None

View File

@ -1001,6 +1001,16 @@ disallow_untyped_defs = true
warn_return_any = true
warn_unreachable = true
[mypy-homeassistant.components.enigma2.*]
check_untyped_defs = true
disallow_incomplete_defs = true
disallow_subclassing_any = true
disallow_untyped_calls = true
disallow_untyped_decorators = true
disallow_untyped_defs = true
warn_return_any = true
warn_unreachable = true
[mypy-homeassistant.components.esphome.*]
check_untyped_defs = true
disallow_incomplete_defs = true