Improve modbus
generic typing (#84737)
This commit is contained in:
parent
d849dab7ba
commit
77e71cf18b
2 changed files with 24 additions and 10 deletions
|
@ -3,7 +3,7 @@ from __future__ import annotations
|
|||
|
||||
from datetime import datetime
|
||||
import logging
|
||||
from typing import Any
|
||||
from typing import Any, Optional
|
||||
|
||||
from homeassistant.components.sensor import CONF_STATE_CLASS, SensorEntity
|
||||
from homeassistant.const import CONF_NAME, CONF_SENSORS, CONF_UNIT_OF_MEASUREMENT
|
||||
|
@ -58,7 +58,7 @@ class ModbusRegisterSensor(BaseStructPlatform, RestoreEntity, SensorEntity):
|
|||
) -> None:
|
||||
"""Initialize the modbus register sensor."""
|
||||
super().__init__(hub, entry)
|
||||
self._coordinator: DataUpdateCoordinator[Any] | None = None
|
||||
self._coordinator: DataUpdateCoordinator[list[int] | None] | None = None
|
||||
self._attr_native_unit_of_measurement = entry.get(CONF_UNIT_OF_MEASUREMENT)
|
||||
self._attr_state_class = entry.get(CONF_STATE_CLASS)
|
||||
|
||||
|
@ -110,7 +110,7 @@ class ModbusRegisterSensor(BaseStructPlatform, RestoreEntity, SensorEntity):
|
|||
result = self.unpack_structure_result(raw_result.registers)
|
||||
if self._coordinator:
|
||||
if result:
|
||||
result_array = result.split(",")
|
||||
result_array = list(map(int, result.split(",")))
|
||||
self._attr_native_value = result_array[0]
|
||||
self._coordinator.async_set_updated_data(result_array)
|
||||
else:
|
||||
|
@ -126,11 +126,18 @@ class ModbusRegisterSensor(BaseStructPlatform, RestoreEntity, SensorEntity):
|
|||
self.async_write_ha_state()
|
||||
|
||||
|
||||
class SlaveSensor(CoordinatorEntity, RestoreEntity, SensorEntity):
|
||||
class SlaveSensor(
|
||||
CoordinatorEntity[DataUpdateCoordinator[Optional[list[int]]]],
|
||||
RestoreEntity,
|
||||
SensorEntity,
|
||||
):
|
||||
"""Modbus slave binary sensor."""
|
||||
|
||||
def __init__(
|
||||
self, coordinator: DataUpdateCoordinator[Any], idx: int, entry: dict[str, Any]
|
||||
self,
|
||||
coordinator: DataUpdateCoordinator[list[int] | None],
|
||||
idx: int,
|
||||
entry: dict[str, Any],
|
||||
) -> None:
|
||||
"""Initialize the Modbus binary sensor."""
|
||||
idx += 1
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue