mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 19:27:45 +00:00
Improve modbus
generic typing (#84737)
This commit is contained in:
parent
d849dab7ba
commit
77e71cf18b
@ -3,7 +3,7 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import logging
|
import logging
|
||||||
from typing import Any
|
from typing import Any, Optional
|
||||||
|
|
||||||
from homeassistant.components.binary_sensor import BinarySensorEntity
|
from homeassistant.components.binary_sensor import BinarySensorEntity
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
@ -59,8 +59,8 @@ class ModbusBinarySensor(BasePlatform, RestoreEntity, BinarySensorEntity):
|
|||||||
def __init__(self, hub: ModbusHub, entry: dict[str, Any], slave_count: int) -> None:
|
def __init__(self, hub: ModbusHub, entry: dict[str, Any], slave_count: int) -> None:
|
||||||
"""Initialize the Modbus binary sensor."""
|
"""Initialize the Modbus binary sensor."""
|
||||||
self._count = slave_count + 1
|
self._count = slave_count + 1
|
||||||
self._coordinator: DataUpdateCoordinator[Any] | None = None
|
self._coordinator: DataUpdateCoordinator[list[int] | None] | None = None
|
||||||
self._result: list = []
|
self._result: list[int] = []
|
||||||
super().__init__(hub, entry)
|
super().__init__(hub, entry)
|
||||||
|
|
||||||
async def async_setup_slaves(
|
async def async_setup_slaves(
|
||||||
@ -121,11 +121,18 @@ class ModbusBinarySensor(BasePlatform, RestoreEntity, BinarySensorEntity):
|
|||||||
self._coordinator.async_set_updated_data(self._result)
|
self._coordinator.async_set_updated_data(self._result)
|
||||||
|
|
||||||
|
|
||||||
class SlaveSensor(CoordinatorEntity, RestoreEntity, BinarySensorEntity):
|
class SlaveSensor(
|
||||||
|
CoordinatorEntity[DataUpdateCoordinator[Optional[list[int]]]],
|
||||||
|
RestoreEntity,
|
||||||
|
BinarySensorEntity,
|
||||||
|
):
|
||||||
"""Modbus slave binary sensor."""
|
"""Modbus slave binary sensor."""
|
||||||
|
|
||||||
def __init__(
|
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:
|
) -> None:
|
||||||
"""Initialize the Modbus binary sensor."""
|
"""Initialize the Modbus binary sensor."""
|
||||||
idx += 1
|
idx += 1
|
||||||
|
@ -3,7 +3,7 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import logging
|
import logging
|
||||||
from typing import Any
|
from typing import Any, Optional
|
||||||
|
|
||||||
from homeassistant.components.sensor import CONF_STATE_CLASS, SensorEntity
|
from homeassistant.components.sensor import CONF_STATE_CLASS, SensorEntity
|
||||||
from homeassistant.const import CONF_NAME, CONF_SENSORS, CONF_UNIT_OF_MEASUREMENT
|
from homeassistant.const import CONF_NAME, CONF_SENSORS, CONF_UNIT_OF_MEASUREMENT
|
||||||
@ -58,7 +58,7 @@ class ModbusRegisterSensor(BaseStructPlatform, RestoreEntity, SensorEntity):
|
|||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the modbus register sensor."""
|
"""Initialize the modbus register sensor."""
|
||||||
super().__init__(hub, entry)
|
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_native_unit_of_measurement = entry.get(CONF_UNIT_OF_MEASUREMENT)
|
||||||
self._attr_state_class = entry.get(CONF_STATE_CLASS)
|
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)
|
result = self.unpack_structure_result(raw_result.registers)
|
||||||
if self._coordinator:
|
if self._coordinator:
|
||||||
if result:
|
if result:
|
||||||
result_array = result.split(",")
|
result_array = list(map(int, result.split(",")))
|
||||||
self._attr_native_value = result_array[0]
|
self._attr_native_value = result_array[0]
|
||||||
self._coordinator.async_set_updated_data(result_array)
|
self._coordinator.async_set_updated_data(result_array)
|
||||||
else:
|
else:
|
||||||
@ -126,11 +126,18 @@ class ModbusRegisterSensor(BaseStructPlatform, RestoreEntity, SensorEntity):
|
|||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
|
|
||||||
class SlaveSensor(CoordinatorEntity, RestoreEntity, SensorEntity):
|
class SlaveSensor(
|
||||||
|
CoordinatorEntity[DataUpdateCoordinator[Optional[list[int]]]],
|
||||||
|
RestoreEntity,
|
||||||
|
SensorEntity,
|
||||||
|
):
|
||||||
"""Modbus slave binary sensor."""
|
"""Modbus slave binary sensor."""
|
||||||
|
|
||||||
def __init__(
|
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:
|
) -> None:
|
||||||
"""Initialize the Modbus binary sensor."""
|
"""Initialize the Modbus binary sensor."""
|
||||||
idx += 1
|
idx += 1
|
||||||
|
Loading…
x
Reference in New Issue
Block a user