Skip to content

Commit 8c27feb

Browse files
committed
Better type checking
1 parent e2c8b27 commit 8c27feb

1 file changed

Lines changed: 57 additions & 46 deletions

File tree

custom_components/template_climate/climate.py

Lines changed: 57 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -510,55 +510,55 @@ def _async_setup_templates(self) -> None: # noqa: PLR0912
510510
super()._async_setup_templates()
511511

512512
@callback
513-
def _update_min_temp(self, temp: str | float) -> None:
513+
def _update_min_temp(self, temp: Any) -> None:
514514
if temp not in (STATE_UNKNOWN, STATE_UNAVAILABLE):
515515
try:
516516
self._attr_min_temp = float(temp)
517517
except ValueError:
518518
LOGGER.error("Could not parse min temperature from %s", temp)
519519

520520
@callback
521-
def _update_max_temp(self, temp: str | float) -> None:
521+
def _update_max_temp(self, temp: Any) -> None:
522522
if temp not in (STATE_UNKNOWN, STATE_UNAVAILABLE):
523523
try:
524524
self._attr_max_temp = float(temp)
525525
except ValueError:
526526
LOGGER.error("Could not parse max temperature from %s", temp)
527527

528528
@callback
529-
def _update_current_temp(self, temp: str | float) -> None:
529+
def _update_current_temp(self, temp: Any) -> None:
530530
if temp not in (STATE_UNKNOWN, STATE_UNAVAILABLE):
531531
try:
532532
self._attr_current_temperature = float(temp)
533533
except ValueError:
534534
LOGGER.error("Could not parse temperature from %s", temp)
535535

536536
@callback
537-
def _update_current_humidity(self, humidity: str | float) -> None:
537+
def _update_current_humidity(self, humidity: Any) -> None:
538538
if humidity not in (STATE_UNKNOWN, STATE_UNAVAILABLE):
539539
try:
540540
self._attr_current_humidity = float(humidity)
541541
except ValueError:
542542
LOGGER.error("Could not parse humidity from %s", humidity)
543543

544544
@callback
545-
def _update_min_humidity(self, humidity: str | float) -> None:
545+
def _update_min_humidity(self, humidity: Any) -> None:
546546
if humidity not in (STATE_UNKNOWN, STATE_UNAVAILABLE):
547547
try:
548548
self._attr_min_humidity = float(humidity)
549549
except ValueError:
550550
LOGGER.error("Could not parse min humidity from %s", humidity)
551551

552552
@callback
553-
def _update_max_humidity(self, humidity: str | float) -> None:
553+
def _update_max_humidity(self, humidity: Any) -> None:
554554
if humidity not in (STATE_UNKNOWN, STATE_UNAVAILABLE):
555555
try:
556556
self._attr_max_humidity = float(humidity)
557557
except ValueError:
558558
LOGGER.error("Could not parse max humidity from %s", humidity)
559559

560560
@callback
561-
def _update_target_humidity(self, humidity: str | float) -> None:
561+
def _update_target_humidity(self, humidity: Any) -> None:
562562
if humidity not in (STATE_UNKNOWN, STATE_UNAVAILABLE):
563563
try:
564564
new_humidity = float(humidity)
@@ -569,7 +569,7 @@ def _update_target_humidity(self, humidity: str | float) -> None:
569569
LOGGER.error("Could not parse target humidity from %s", humidity)
570570

571571
@callback
572-
def _update_target_temp(self, temp: str | float) -> None:
572+
def _update_target_temp(self, temp: Any) -> None:
573573
if temp not in (STATE_UNKNOWN, STATE_UNAVAILABLE):
574574
try:
575575
new_target_temp = float(temp)
@@ -580,7 +580,7 @@ def _update_target_temp(self, temp: str | float) -> None:
580580
LOGGER.error("Could not parse temperature from %s", temp)
581581

582582
@callback
583-
def _update_target_temp_high(self, temp: str | float) -> None:
583+
def _update_target_temp_high(self, temp: Any) -> None:
584584
if temp not in (STATE_UNKNOWN, STATE_UNAVAILABLE):
585585
try:
586586
new_target_temp_high = float(temp)
@@ -591,7 +591,7 @@ def _update_target_temp_high(self, temp: str | float) -> None:
591591
LOGGER.error("Could not parse temperature high from %s", temp)
592592

593593
@callback
594-
def _update_target_temp_low(self, temp: str | float) -> None:
594+
def _update_target_temp_low(self, temp: Any) -> None:
595595
if temp not in (STATE_UNKNOWN, STATE_UNAVAILABLE):
596596
try:
597597
new_target_temp_low = float(temp)
@@ -602,21 +602,30 @@ def _update_target_temp_low(self, temp: str | float) -> None:
602602
LOGGER.error("Could not parse temperature low from %s", temp)
603603

604604
@callback
605-
def _update_hvac_mode(self, hvac_mode: str | None) -> None:
606-
if hvac_mode in self._attr_hvac_modes:
607-
hvac_mode = HVACMode(hvac_mode) if hvac_mode else None
608-
if self._attr_hvac_mode != hvac_mode:
609-
self._attr_hvac_mode = hvac_mode
610-
self.async_write_ha_state()
611-
elif hvac_mode not in (STATE_UNKNOWN, STATE_UNAVAILABLE):
612-
LOGGER.error(
613-
"Received invalid hvac mode: %s. Expected: %s.",
614-
hvac_mode,
615-
self._attr_hvac_modes,
616-
)
605+
def _update_hvac_mode(self, hvac_mode_value: Any) -> None:
606+
if hvac_mode_value not in (STATE_UNKNOWN, STATE_UNAVAILABLE):
607+
try:
608+
hvac_mode = HVACMode(str(hvac_mode_value))
609+
if hvac_mode in self._attr_hvac_modes:
610+
if self._attr_hvac_mode != hvac_mode:
611+
self._attr_hvac_mode = hvac_mode
612+
self.async_write_ha_state()
613+
else:
614+
LOGGER.error(
615+
"Received invalid hvac mode: %s. Expected: %s.",
616+
hvac_mode,
617+
self._attr_hvac_modes,
618+
)
619+
except ValueError:
620+
LOGGER.error(
621+
"Received invalid hvac mode: %s. Expected: %s.",
622+
hvac_mode_value,
623+
self._attr_hvac_modes,
624+
)
617625

618626
@callback
619-
def _update_preset_mode(self, preset_mode: str | None) -> None:
627+
def _update_preset_mode(self, preset_mode_value: Any) -> None:
628+
preset_mode = str(preset_mode_value)
620629
if self._attr_preset_modes and preset_mode in self._attr_preset_modes:
621630
if self._attr_preset_mode != preset_mode:
622631
self._attr_preset_mode = preset_mode
@@ -629,22 +638,22 @@ def _update_preset_mode(self, preset_mode: str | None) -> None:
629638
)
630639

631640
@callback
632-
def _update_fan_mode(self, fan_mode: str | None) -> None:
633-
fan_mode_str = str(fan_mode)
634-
if self._attr_fan_modes and fan_mode_str in self._attr_fan_modes:
635-
if self._attr_fan_mode != fan_mode_str:
636-
self._attr_fan_mode = fan_mode_str
641+
def _update_fan_mode(self, fan_mode_value: Any) -> None:
642+
fan_mode = str(fan_mode_value)
643+
if self._attr_fan_modes and fan_mode in self._attr_fan_modes:
644+
if self._attr_fan_mode != fan_mode:
645+
self._attr_fan_mode = fan_mode
637646
self.async_write_ha_state()
638647
elif fan_mode not in (STATE_UNKNOWN, STATE_UNAVAILABLE):
639648
LOGGER.error(
640-
"Received invalid fan mode: %s (str: %s). Expected: %s.",
641-
fan_mode,
642-
fan_mode_str,
649+
"Received invalid fan mode: %s. Expected: %s.",
650+
fan_mode_value,
643651
self._attr_fan_modes,
644652
)
645653

646654
@callback
647-
def _update_swing_mode(self, swing_mode: str | None) -> None:
655+
def _update_swing_mode(self, swing_mode_value: Any) -> None:
656+
swing_mode = str(swing_mode_value)
648657
if self._attr_swing_modes and swing_mode in self._attr_swing_modes:
649658
if self._attr_swing_mode != swing_mode:
650659
self._attr_swing_mode = swing_mode
@@ -657,17 +666,19 @@ def _update_swing_mode(self, swing_mode: str | None) -> None:
657666
)
658667

659668
@callback
660-
def _update_hvac_action(self, hvac_action: str | None) -> None:
661-
if hvac_action in HVACAction or hvac_action is None:
662-
if self._attr_hvac_action != hvac_action:
663-
self._attr_hvac_action = HVACAction(hvac_action)
664-
self.async_write_ha_state()
665-
elif hvac_action not in (STATE_UNKNOWN, STATE_UNAVAILABLE):
666-
LOGGER.error(
667-
"Received invalid hvac action: %s. Expected: %s.",
668-
hvac_action,
669-
[str(member) for member in HVACAction],
670-
)
669+
def _update_hvac_action(self, hvac_action_value: Any) -> None:
670+
if hvac_action_value not in (STATE_UNKNOWN, STATE_UNAVAILABLE):
671+
try:
672+
hvac_action = HVACAction(str(hvac_action_value))
673+
if self._attr_hvac_action != hvac_action:
674+
self._attr_hvac_action = hvac_action
675+
self.async_write_ha_state()
676+
except ValueError:
677+
LOGGER.error(
678+
"Received invalid hvac action: %s. Expected: %s.",
679+
hvac_action_value,
680+
[str(member) for member in HVACAction],
681+
)
671682

672683
@property
673684
def target_temperature(self) -> float | None:
@@ -696,7 +707,7 @@ def target_temperature_low(self) -> float | None:
696707
else None
697708
)
698709

699-
async def async_set_hvac_mode(self, hvac_mode: HVACMode | None) -> None:
710+
async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None:
700711
"""Set new operation mode."""
701712
if self._hvac_mode_template is None:
702713
self._attr_hvac_mode = hvac_mode # always optimistic
@@ -782,7 +793,7 @@ async def async_set_temperature(self, **kwargs: Any) -> None:
782793
# Handle potential HVAC mode change
783794
if operation_mode := kwargs.get(ATTR_HVAC_MODE):
784795
operation_mode = HVACMode(operation_mode) if operation_mode else None
785-
if operation_mode != self._attr_hvac_mode:
796+
if operation_mode and operation_mode != self._attr_hvac_mode:
786797
await self.async_set_hvac_mode(operation_mode)
787798

788799
# Run the set temperature script if defined
@@ -798,7 +809,7 @@ async def async_set_temperature(self, **kwargs: Any) -> None:
798809
context=self._context,
799810
)
800811

801-
async def async_set_humidity(self, humidity: float) -> None:
812+
async def async_set_humidity(self, humidity: int) -> None:
802813
"""Set new target humidity."""
803814
if self._target_humidity_template is None:
804815
self._attr_target_humidity = humidity # always optimistic

0 commit comments

Comments
 (0)