1- """
2- Support for Xiaomi Mi Home Air Conditioner Companion (AC Partner)
1+ """Support for Xiaomi Mi Home Air Conditioner Companion (AC Partner).
32
43For more details about this platform, please refer to the documentation
54https://home-assistant.io/components/climate.xiaomi_miio
4140from homeassistant .exceptions import PlatformNotReady
4241from homeassistant .helpers .event import async_track_state_change_event
4342from homeassistant .util .dt import utcnow
43+ from miio import AirConditioningCompanion , DeviceException
44+ from miio .airconditioningcompanion import FanSpeed , Led , Power , SwingMode
45+ from miio .airconditioningcompanion import OperationMode as MiioOperationMode
4446
4547_LOGGER = logging .getLogger (__name__ )
4648
130132# pylint: disable=unused-argument
131133async def async_setup_platform (hass , config , async_add_devices , discovery_info = None ):
132134 """Set up the air conditioning companion from config."""
133- from miio import AirConditioningCompanion , DeviceException
134-
135135 if DATA_KEY not in hass .data :
136136 hass .data [DATA_KEY ] = {}
137137
@@ -150,7 +150,7 @@ async def async_setup_platform(hass, config, async_add_devices, discovery_info=N
150150 device = AirConditioningCompanion (host , token )
151151 device_info = device .info ()
152152 model = device_info .model
153- unique_id = "{ }-{}" . format ( model , device_info .mac_address )
153+ unique_id = f" { model } -{ device_info .mac_address } "
154154 _LOGGER .info (
155155 "%s %s %s detected" ,
156156 model ,
@@ -159,7 +159,7 @@ async def async_setup_platform(hass, config, async_add_devices, discovery_info=N
159159 )
160160 except DeviceException as ex :
161161 _LOGGER .error ("Device unavailable or token incorrect: %s" , ex )
162- raise PlatformNotReady
162+ raise PlatformNotReady from ex
163163
164164 air_conditioning_companion = XiaomiAirConditioningCompanion (
165165 hass ,
@@ -201,14 +201,16 @@ async def async_service_handler(service):
201201 if update_tasks :
202202 await asyncio .wait (update_tasks )
203203
204- for service in SERVICE_TO_METHOD :
205- schema = SERVICE_TO_METHOD [ service ] .get ("schema" , SERVICE_SCHEMA )
204+ for service , value in SERVICE_TO_METHOD . items () :
205+ schema = value .get ("schema" , SERVICE_SCHEMA )
206206 hass .services .async_register (
207207 DOMAIN , service , async_service_handler , schema = schema
208208 )
209209
210210
211211class OperationMode (enum .Enum ):
212+ """Supported HVAC operation modes."""
213+
212214 Heat = HVACMode .HEAT
213215 Cool = HVACMode .COOL
214216 Auto = HVACMode .AUTO
@@ -319,8 +321,6 @@ async def _async_power_sensor_changed(self, entity_id, old_state, new_state):
319321
320322 async def _try_command (self , mask_error , func , * args , ** kwargs ):
321323 """Call a AC companion command handling error messages."""
322- from miio import DeviceException
323-
324324 try :
325325 result = await self .hass .async_add_executor_job (
326326 partial (func , * args , ** kwargs )
@@ -334,7 +334,7 @@ async def _try_command(self, mask_error, func, *args, **kwargs):
334334 self ._available = False
335335 return False
336336
337- async def async_turn_on (self , speed : str = None , ** kwargs ) -> None :
337+ async def async_turn_on (self , speed : str | None = None , ** kwargs ) -> None :
338338 """Turn the miio device on."""
339339 result = await self ._try_command (
340340 "Turning the miio device on failed." , self ._device .on
@@ -354,8 +354,6 @@ async def async_turn_off(self, **kwargs) -> None:
354354
355355 async def async_update (self ):
356356 """Update the state of this climate device."""
357- from miio import DeviceException
358-
359357 try :
360358 state = await self .hass .async_add_executor_job (self ._device .status )
361359 _LOGGER .debug ("Got new state: %s" , state )
@@ -456,7 +454,7 @@ def hvac_mode(self):
456454
457455 @property
458456 def last_on_operation (self ):
459- """Return the last operation when the AC is on (ie heat, cool, fan only)"""
457+ """Return the last operation when the AC is on (ie heat, cool, fan only). """
460458 return self ._last_on_operation
461459
462460 @property
@@ -472,8 +470,6 @@ def fan_mode(self):
472470 @property
473471 def fan_modes (self ):
474472 """Return the list of available fan modes."""
475- from miio .airconditioningcompanion import FanSpeed
476-
477473 return [speed .name .lower () for speed in FanSpeed ]
478474
479475 async def async_set_temperature (self , ** kwargs ):
@@ -487,15 +483,11 @@ async def async_set_temperature(self, **kwargs):
487483
488484 async def async_set_swing_mode (self , swing_mode ):
489485 """Set the swing mode."""
490- from miio .airconditioningcompanion import SwingMode
491-
492486 self ._swing_mode = SwingMode [swing_mode .title ()]
493487 await self ._send_configuration ()
494488
495489 async def async_set_fan_mode (self , fan_mode ):
496490 """Set the fan mode."""
497- from miio .airconditioningcompanion import FanSpeed
498-
499491 self ._fan_mode = FanSpeed [fan_mode .title ()]
500492 await self ._send_configuration ()
501493
@@ -522,14 +514,9 @@ def swing_mode(self):
522514 @property
523515 def swing_modes (self ):
524516 """List of available swing modes."""
525- from miio .airconditioningcompanion import SwingMode
526-
527517 return [mode .name .lower () for mode in SwingMode ]
528518
529519 async def _send_configuration (self ):
530- from miio .airconditioningcompanion import Led
531- from miio .airconditioningcompanion import OperationMode as MiioOperationMode
532- from miio .airconditioningcompanion import Power
533520
534521 if self ._air_condition_model is not None :
535522 await self ._try_command (
@@ -565,7 +552,7 @@ async def async_learn_command(self, slot, timeout):
565552 message = message [0 ]
566553 _LOGGER .debug ("Message received from device: '%s'" , message )
567554 if message .startswith ("FE" ):
568- log_msg = "Received command is: {}" . format ( message )
555+ log_msg = f "Received command is: { message } "
569556 _LOGGER .info (log_msg )
570557 self .hass .async_create_task (
571558 self .hass .services .async_call (
0 commit comments