Strictly type modbus climate.py (#56380)

This commit is contained in:
jan iversen 2021-09-27 12:25:05 +02:00 committed by GitHub
parent b612e16120
commit 50f97b26eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,7 @@
"""Support for Generic Modbus Thermostats.""" """Support for Generic Modbus Thermostats."""
from __future__ import annotations from __future__ import annotations
from datetime import datetime
import logging import logging
import struct import struct
from typing import Any from typing import Any
@ -12,7 +13,6 @@ from homeassistant.components.climate.const import (
) )
from homeassistant.const import ( from homeassistant.const import (
CONF_NAME, CONF_NAME,
CONF_STRUCTURE,
CONF_TEMPERATURE_UNIT, CONF_TEMPERATURE_UNIT,
PRECISION_TENTHS, PRECISION_TENTHS,
PRECISION_WHOLE, PRECISION_WHOLE,
@ -20,6 +20,7 @@ from homeassistant.const import (
TEMP_FAHRENHEIT, TEMP_FAHRENHEIT,
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.restore_state import RestoreEntity from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
@ -50,9 +51,9 @@ _LOGGER = logging.getLogger(__name__)
async def async_setup_platform( async def async_setup_platform(
hass: HomeAssistant, hass: HomeAssistant,
config: ConfigType, config: ConfigType,
async_add_entities, async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None, discovery_info: DiscoveryInfoType | None = None,
): ) -> None:
"""Read configuration and create Modbus climate.""" """Read configuration and create Modbus climate."""
if discovery_info is None: if discovery_info is None:
return return
@ -77,7 +78,6 @@ class ModbusThermostat(BaseStructPlatform, RestoreEntity, ClimateEntity):
super().__init__(hub, config) super().__init__(hub, config)
self._target_temperature_register = config[CONF_TARGET_TEMP] self._target_temperature_register = config[CONF_TARGET_TEMP]
self._unit = config[CONF_TEMPERATURE_UNIT] self._unit = config[CONF_TEMPERATURE_UNIT]
self._structure = config[CONF_STRUCTURE]
self._attr_supported_features = SUPPORT_TARGET_TEMPERATURE self._attr_supported_features = SUPPORT_TARGET_TEMPERATURE
self._attr_hvac_mode = HVAC_MODE_AUTO self._attr_hvac_mode = HVAC_MODE_AUTO
@ -95,7 +95,7 @@ class ModbusThermostat(BaseStructPlatform, RestoreEntity, ClimateEntity):
self._attr_target_temperature_step = config[CONF_TARGET_TEMP] self._attr_target_temperature_step = config[CONF_TARGET_TEMP]
self._attr_target_temperature_step = config[CONF_STEP] self._attr_target_temperature_step = config[CONF_STEP]
async def async_added_to_hass(self): async def async_added_to_hass(self) -> None:
"""Handle entity which will be added.""" """Handle entity which will be added."""
await self.async_base_added_to_hass() await self.async_base_added_to_hass()
state = await self.async_get_last_state() state = await self.async_get_last_state()
@ -107,12 +107,12 @@ class ModbusThermostat(BaseStructPlatform, RestoreEntity, ClimateEntity):
# Home Assistant expects this method. # Home Assistant expects this method.
# We'll keep it here to avoid getting exceptions. # We'll keep it here to avoid getting exceptions.
async def async_set_temperature(self, **kwargs): async def async_set_temperature(self, **kwargs: Any) -> None:
"""Set new target temperature.""" """Set new target temperature."""
if ATTR_TEMPERATURE not in kwargs: if ATTR_TEMPERATURE not in kwargs:
return return
target_temperature = ( target_temperature = (
float(kwargs.get(ATTR_TEMPERATURE)) - self._offset float(kwargs[ATTR_TEMPERATURE]) - self._offset
) / self._scale ) / self._scale
if self._data_type in ( if self._data_type in (
DATA_TYPE_INT16, DATA_TYPE_INT16,
@ -138,7 +138,7 @@ class ModbusThermostat(BaseStructPlatform, RestoreEntity, ClimateEntity):
self._attr_available = result is not None self._attr_available = result is not None
await self.async_update() await self.async_update()
async def async_update(self, now=None): async def async_update(self, now: datetime | None = None) -> None:
"""Update Target & Current Temperature.""" """Update Target & Current Temperature."""
# remark "now" is a dummy parameter to avoid problems with # remark "now" is a dummy parameter to avoid problems with
# async_track_time_interval # async_track_time_interval
@ -156,7 +156,9 @@ class ModbusThermostat(BaseStructPlatform, RestoreEntity, ClimateEntity):
self._call_active = False self._call_active = False
self.async_write_ha_state() self.async_write_ha_state()
async def _async_read_register(self, register_type, register) -> float | None: async def _async_read_register(
self, register_type: str, register: int
) -> float | None:
"""Read register using the Modbus hub slave.""" """Read register using the Modbus hub slave."""
result = await self._hub.async_pymodbus_call( result = await self._hub.async_pymodbus_call(
self._slave, register, self._count, register_type self._slave, register, self._count, register_type
@ -172,7 +174,6 @@ class ModbusThermostat(BaseStructPlatform, RestoreEntity, ClimateEntity):
self._lazy_errors = self._lazy_error_count self._lazy_errors = self._lazy_error_count
self._value = self.unpack_structure_result(result.registers) self._value = self.unpack_structure_result(result.registers)
self._attr_available = True self._attr_available = True
if not self._value:
if self._value is None:
return None return None
return float(self._value) return float(self._value)