Improve type hints in epson media player (#77129)

This commit is contained in:
epenet 2022-08-22 11:34:20 +02:00 committed by GitHub
parent 8cd04750fc
commit 361f82f5fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,6 +3,7 @@ from __future__ import annotations
import logging import logging
from epson_projector import Projector
from epson_projector.const import ( from epson_projector.const import (
BACK, BACK,
BUSY, BUSY,
@ -52,13 +53,11 @@ async def async_setup_entry(
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Set up the Epson projector from a config entry.""" """Set up the Epson projector from a config entry."""
entry_id = config_entry.entry_id projector: Projector = hass.data[DOMAIN][config_entry.entry_id]
unique_id = config_entry.unique_id
projector = hass.data[DOMAIN][entry_id]
projector_entity = EpsonProjectorMediaPlayer( projector_entity = EpsonProjectorMediaPlayer(
projector=projector, projector=projector,
name=config_entry.title, name=config_entry.title,
unique_id=unique_id, unique_id=config_entry.unique_id,
entry=config_entry, entry=config_entry,
) )
async_add_entities([projector_entity], True) async_add_entities([projector_entity], True)
@ -83,23 +82,30 @@ class EpsonProjectorMediaPlayer(MediaPlayerEntity):
| MediaPlayerEntityFeature.PREVIOUS_TRACK | MediaPlayerEntityFeature.PREVIOUS_TRACK
) )
def __init__(self, projector, name, unique_id, entry): def __init__(
self, projector: Projector, name: str, unique_id: str | None, entry: ConfigEntry
) -> None:
"""Initialize entity to control Epson projector.""" """Initialize entity to control Epson projector."""
self._projector = projector self._projector = projector
self._entry = entry self._entry = entry
self._name = name self._attr_name = name
self._available = False self._attr_available = False
self._cmode = None self._cmode = None
self._source_list = list(DEFAULT_SOURCES.values()) self._attr_source_list = list(DEFAULT_SOURCES.values())
self._source = None self._attr_unique_id = unique_id
self._volume = None if unique_id:
self._state = None self._attr_device_info = DeviceInfo(
self._unique_id = unique_id identifiers={(DOMAIN, unique_id)},
manufacturer="Epson",
model="Epson",
name="Epson projector",
via_device=(DOMAIN, unique_id),
)
async def set_unique_id(self): async def set_unique_id(self) -> bool:
"""Set unique id for projector config entry.""" """Set unique id for projector config entry."""
_LOGGER.debug("Setting unique_id for projector") _LOGGER.debug("Setting unique_id for projector")
if self._unique_id: if self.unique_id:
return False return False
if uid := await self._projector.get_serial_number(): if uid := await self._projector.get_serial_number():
self.hass.config_entries.async_update_entry(self._entry, unique_id=uid) self.hass.config_entries.async_update_entry(self._entry, unique_id=uid)
@ -113,96 +119,48 @@ class EpsonProjectorMediaPlayer(MediaPlayerEntity):
self.hass.config_entries.async_reload(self._entry.entry_id) self.hass.config_entries.async_reload(self._entry.entry_id)
) )
return True return True
return False
async def async_update(self) -> None: async def async_update(self) -> None:
"""Update state of device.""" """Update state of device."""
power_state = await self._projector.get_power() power_state = await self._projector.get_power()
_LOGGER.debug("Projector status: %s", power_state) _LOGGER.debug("Projector status: %s", power_state)
if not power_state or power_state == EPSON_STATE_UNAVAILABLE: if not power_state or power_state == EPSON_STATE_UNAVAILABLE:
self._available = False self._attr_available = False
return return
self._available = True self._attr_available = True
if power_state == EPSON_CODES[POWER]: if power_state == EPSON_CODES[POWER]:
self._state = STATE_ON self._attr_state = STATE_ON
if await self.set_unique_id(): if await self.set_unique_id():
return return
self._source_list = list(DEFAULT_SOURCES.values()) self._attr_source_list = list(DEFAULT_SOURCES.values())
cmode = await self._projector.get_property(CMODE) cmode = await self._projector.get_property(CMODE)
self._cmode = CMODE_LIST.get(cmode, self._cmode) self._cmode = CMODE_LIST.get(cmode, self._cmode)
source = await self._projector.get_property(SOURCE) source = await self._projector.get_property(SOURCE)
self._source = SOURCE_LIST.get(source, self._source) self._attr_source = SOURCE_LIST.get(source, self._attr_source)
volume = await self._projector.get_property(VOLUME) if volume := await self._projector.get_property(VOLUME):
if volume:
try: try:
self._volume = float(volume) self._attr_volume_level = float(volume)
except ValueError: except ValueError:
self._volume = None self._attr_volume_level = None
elif power_state == BUSY: elif power_state == BUSY:
self._state = STATE_ON self._attr_state = STATE_ON
else: else:
self._state = STATE_OFF self._attr_state = STATE_OFF
@property
def device_info(self) -> DeviceInfo | None:
"""Get attributes about the device."""
if not self._unique_id:
return None
return DeviceInfo(
identifiers={(DOMAIN, self._unique_id)},
manufacturer="Epson",
model="Epson",
name="Epson projector",
via_device=(DOMAIN, self._unique_id),
)
@property
def name(self):
"""Return the name of the device."""
return self._name
@property
def unique_id(self):
"""Return unique ID."""
return self._unique_id
@property
def state(self):
"""Return the state of the device."""
return self._state
@property
def available(self):
"""Return if projector is available."""
return self._available
async def async_turn_on(self) -> None: async def async_turn_on(self) -> None:
"""Turn on epson.""" """Turn on epson."""
if self._state == STATE_OFF: if self.state == STATE_OFF:
await self._projector.send_command(TURN_ON) await self._projector.send_command(TURN_ON)
self._state = STATE_ON self._attr_state = STATE_ON
async def async_turn_off(self) -> None: async def async_turn_off(self) -> None:
"""Turn off epson.""" """Turn off epson."""
if self._state == STATE_ON: if self.state == STATE_ON:
await self._projector.send_command(TURN_OFF) await self._projector.send_command(TURN_OFF)
self._state = STATE_OFF self._attr_state = STATE_OFF
@property async def select_cmode(self, cmode: str) -> None:
def source_list(self):
"""List of available input sources."""
return self._source_list
@property
def source(self):
"""Get current input sources."""
return self._source
@property
def volume_level(self):
"""Return the volume level of the media player (0..1)."""
return self._volume
async def select_cmode(self, cmode):
"""Set color mode in Epson.""" """Set color mode in Epson."""
await self._projector.send_command(CMODE_LIST_SET[cmode]) await self._projector.send_command(CMODE_LIST_SET[cmode])
@ -240,7 +198,7 @@ class EpsonProjectorMediaPlayer(MediaPlayerEntity):
await self._projector.send_command(BACK) await self._projector.send_command(BACK)
@property @property
def extra_state_attributes(self): def extra_state_attributes(self) -> dict[str, str]:
"""Return device specific state attributes.""" """Return device specific state attributes."""
if self._cmode is None: if self._cmode is None:
return {} return {}