From 345081ba158f0c3db55d7b0cd3d9a2dc3d03e0e5 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Wed, 28 Dec 2022 23:37:12 +0100 Subject: [PATCH] Improve `syncthru` generic typing (#84648) --- homeassistant/components/syncthru/__init__.py | 2 +- .../components/syncthru/binary_sensor.py | 25 +++--- homeassistant/components/syncthru/sensor.py | 77 +++++++++---------- 3 files changed, 50 insertions(+), 54 deletions(-) diff --git a/homeassistant/components/syncthru/__init__.py b/homeassistant/components/syncthru/__init__.py index 5031f485ab3..c757ff0c529 100644 --- a/homeassistant/components/syncthru/__init__.py +++ b/homeassistant/components/syncthru/__init__.py @@ -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, diff --git a/homeassistant/components/syncthru/binary_sensor.py b/homeassistant/components/syncthru/binary_sensor.py index 7517c99d2b9..66ed72a3fc9 100644 --- a/homeassistant/components/syncthru/binary_sensor.py +++ b/homeassistant/components/syncthru/binary_sensor.py @@ -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 diff --git a/homeassistant/components/syncthru/sensor.py b/homeassistant/components/syncthru/sensor.py index 11e1403816e..58f1a5c2b3a 100644 --- a/homeassistant/components/syncthru/sensor.py +++ b/homeassistant/components/syncthru/sensor.py @@ -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