Improve syncthru generic typing (#84648)

This commit is contained in:
Marc Mueller 2022-12-28 23:37:12 +01:00 committed by GitHub
parent de5c7b0414
commit 345081ba15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 54 deletions

View File

@ -50,7 +50,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
)
return printer
coordinator: DataUpdateCoordinator = DataUpdateCoordinator(
coordinator = DataUpdateCoordinator[SyncThru](
hass,
_LOGGER,
name=DOMAIN,

View File

@ -38,9 +38,11 @@ async def async_setup_entry(
) -> None:
"""Set up from config entry."""
coordinator: DataUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id]
coordinator: DataUpdateCoordinator[SyncThru] = hass.data[DOMAIN][
config_entry.entry_id
]
name = config_entry.data[CONF_NAME]
name: str = config_entry.data[CONF_NAME]
entities = [
SyncThruOnlineSensor(coordinator, name),
SyncThruProblemSensor(coordinator, name),
@ -49,14 +51,16 @@ async def async_setup_entry(
async_add_entities(entities)
class SyncThruBinarySensor(CoordinatorEntity, BinarySensorEntity):
class SyncThruBinarySensor(
CoordinatorEntity[DataUpdateCoordinator[SyncThru]], BinarySensorEntity
):
"""Implementation of an abstract Samsung Printer binary sensor platform."""
def __init__(self, coordinator, name):
def __init__(self, coordinator: DataUpdateCoordinator[SyncThru], name: str) -> None:
"""Initialize the sensor."""
super().__init__(coordinator)
self.syncthru: SyncThru = coordinator.data
self._name = name
self.syncthru = coordinator.data
self._attr_name = name
self._id_suffix = ""
@property
@ -65,11 +69,6 @@ class SyncThruBinarySensor(CoordinatorEntity, BinarySensorEntity):
serial = self.syncthru.serial_number()
return f"{serial}{self._id_suffix}" if serial else None
@property
def name(self):
"""Return the name of the sensor."""
return self._name
@property
def device_info(self) -> DeviceInfo | None:
"""Return device information."""
@ -85,9 +84,9 @@ class SyncThruOnlineSensor(SyncThruBinarySensor):
_attr_device_class = BinarySensorDeviceClass.CONNECTIVITY
def __init__(self, syncthru, name):
def __init__(self, coordinator: DataUpdateCoordinator[SyncThru], name: str) -> None:
"""Initialize the sensor."""
super().__init__(syncthru, name)
super().__init__(coordinator, name)
self._id_suffix = "_online"
@property

View File

@ -46,7 +46,9 @@ async def async_setup_entry(
) -> None:
"""Set up from config entry."""
coordinator: DataUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id]
coordinator: DataUpdateCoordinator[SyncThru] = hass.data[DOMAIN][
config_entry.entry_id
]
printer: SyncThru = coordinator.data
supp_toner = printer.toner_status(filter_supported=True)
@ -54,7 +56,7 @@ async def async_setup_entry(
supp_tray = printer.input_tray_status(filter_supported=True)
supp_output_tray = printer.output_tray_status()
name = config_entry.data[CONF_NAME]
name: str = config_entry.data[CONF_NAME]
entities: list[SyncThruSensor] = [
SyncThruMainSensor(coordinator, name),
SyncThruActiveAlertSensor(coordinator, name),
@ -72,16 +74,16 @@ async def async_setup_entry(
async_add_entities(entities)
class SyncThruSensor(CoordinatorEntity, SensorEntity):
class SyncThruSensor(CoordinatorEntity[DataUpdateCoordinator[SyncThru]], SensorEntity):
"""Implementation of an abstract Samsung Printer sensor platform."""
def __init__(self, coordinator, name):
_attr_icon = "mdi:printer"
def __init__(self, coordinator: DataUpdateCoordinator[SyncThru], name: str) -> None:
"""Initialize the sensor."""
super().__init__(coordinator)
self.syncthru: SyncThru = coordinator.data
self._name = name
self._icon = "mdi:printer"
self._unit_of_measurement = None
self.syncthru = coordinator.data
self._attr_name = name
self._id_suffix = ""
@property
@ -90,21 +92,6 @@ class SyncThruSensor(CoordinatorEntity, SensorEntity):
serial = self.syncthru.serial_number()
return f"{serial}{self._id_suffix}" if serial else None
@property
def name(self):
"""Return the name of the sensor."""
return self._name
@property
def icon(self):
"""Return the icon of the device."""
return self._icon
@property
def native_unit_of_measurement(self):
"""Return the unit of measuremnt."""
return self._unit_of_measurement
@property
def device_info(self) -> DeviceInfo | None:
"""Return device information."""
@ -123,7 +110,7 @@ class SyncThruMainSensor(SyncThruSensor):
the displayed current status message.
"""
def __init__(self, coordinator, name):
def __init__(self, coordinator: DataUpdateCoordinator[SyncThru], name: str) -> None:
"""Initialize the sensor."""
super().__init__(coordinator, name)
self._id_suffix = "_main"
@ -149,12 +136,15 @@ class SyncThruMainSensor(SyncThruSensor):
class SyncThruTonerSensor(SyncThruSensor):
"""Implementation of a Samsung Printer toner sensor platform."""
def __init__(self, coordinator, name, color):
_attr_native_unit_of_measurement = PERCENTAGE
def __init__(
self, coordinator: DataUpdateCoordinator[SyncThru], name: str, color: str
) -> None:
"""Initialize the sensor."""
super().__init__(coordinator, name)
self._name = f"{name} Toner {color}"
self._attr_name = f"{name} Toner {color}"
self._color = color
self._unit_of_measurement = PERCENTAGE
self._id_suffix = f"_toner_{color}"
@property
@ -171,12 +161,15 @@ class SyncThruTonerSensor(SyncThruSensor):
class SyncThruDrumSensor(SyncThruSensor):
"""Implementation of a Samsung Printer drum sensor platform."""
def __init__(self, syncthru, name, color):
_attr_native_unit_of_measurement = PERCENTAGE
def __init__(
self, coordinator: DataUpdateCoordinator[SyncThru], name: str, color: str
) -> None:
"""Initialize the sensor."""
super().__init__(syncthru, name)
self._name = f"{name} Drum {color}"
super().__init__(coordinator, name)
self._attr_name = f"{name} Drum {color}"
self._color = color
self._unit_of_measurement = PERCENTAGE
self._id_suffix = f"_drum_{color}"
@property
@ -193,10 +186,12 @@ class SyncThruDrumSensor(SyncThruSensor):
class SyncThruInputTraySensor(SyncThruSensor):
"""Implementation of a Samsung Printer input tray sensor platform."""
def __init__(self, syncthru, name, number):
def __init__(
self, coordinator: DataUpdateCoordinator[SyncThru], name: str, number: str
) -> None:
"""Initialize the sensor."""
super().__init__(syncthru, name)
self._name = f"{name} Tray {number}"
super().__init__(coordinator, name)
self._attr_name = f"{name} Tray {number}"
self._number = number
self._id_suffix = f"_tray_{number}"
@ -219,10 +214,12 @@ class SyncThruInputTraySensor(SyncThruSensor):
class SyncThruOutputTraySensor(SyncThruSensor):
"""Implementation of a Samsung Printer output tray sensor platform."""
def __init__(self, syncthru, name, number):
def __init__(
self, coordinator: DataUpdateCoordinator[SyncThru], name: str, number: int
) -> None:
"""Initialize the sensor."""
super().__init__(syncthru, name)
self._name = f"{name} Output Tray {number}"
super().__init__(coordinator, name)
self._attr_name = f"{name} Output Tray {number}"
self._number = number
self._id_suffix = f"_output_tray_{number}"
@ -245,10 +242,10 @@ class SyncThruOutputTraySensor(SyncThruSensor):
class SyncThruActiveAlertSensor(SyncThruSensor):
"""Implementation of a Samsung Printer active alerts sensor platform."""
def __init__(self, syncthru, name):
def __init__(self, coordinator: DataUpdateCoordinator[SyncThru], name: str) -> None:
"""Initialize the sensor."""
super().__init__(syncthru, name)
self._name = f"{name} Active Alerts"
super().__init__(coordinator, name)
self._attr_name = f"{name} Active Alerts"
self._id_suffix = "_active_alerts"
@property