Use shorthand attributes in Gree (#99332)

This commit is contained in:
Joost Lekkerkerker 2023-08-30 15:16:45 +02:00 committed by GitHub
parent 66ad605d3e
commit 587928223a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 93 deletions

View file

@ -115,40 +115,33 @@ class GreeClimateEntity(CoordinatorEntity[DeviceDataUpdateCoordinator], ClimateE
| ClimateEntityFeature.PRESET_MODE
| ClimateEntityFeature.SWING_MODE
)
_attr_target_temperature_step = TARGET_TEMPERATURE_STEP
_attr_hvac_modes = [*HVAC_MODES_REVERSE, HVACMode.OFF]
_attr_preset_modes = PRESET_MODES
_attr_fan_modes = [*FAN_MODES_REVERSE]
_attr_swing_modes = SWING_MODES
def __init__(self, coordinator: DeviceDataUpdateCoordinator) -> None:
"""Initialize the Gree device."""
super().__init__(coordinator)
self._name = coordinator.device.device_info.name
self._mac = coordinator.device.device_info.mac
@property
def name(self) -> str:
"""Return the name of the device."""
return self._name
@property
def unique_id(self) -> str:
"""Return a unique id for the device."""
return self._mac
@property
def device_info(self) -> DeviceInfo:
"""Return device specific attributes."""
return DeviceInfo(
connections={(CONNECTION_NETWORK_MAC, self._mac)},
identifiers={(DOMAIN, self._mac)},
self._attr_name = coordinator.device.device_info.name
mac = coordinator.device.device_info.mac
self._attr_unique_id = mac
self._attr_device_info = DeviceInfo(
connections={(CONNECTION_NETWORK_MAC, mac)},
identifiers={(DOMAIN, mac)},
manufacturer="Gree",
name=self._name,
name=self._attr_name,
)
@property
def temperature_unit(self) -> str:
"""Return the temperature units for the device."""
units = self.coordinator.device.temperature_units
if units == TemperatureUnits.C:
return UnitOfTemperature.CELSIUS
return UnitOfTemperature.FAHRENHEIT
self._attr_temperature_unit = UnitOfTemperature.CELSIUS
self._attr_min_temp = TEMP_MIN
self._attr_max_temp = TEMP_MAX
else:
self._attr_temperature_unit = UnitOfTemperature.FAHRENHEIT
self._attr_min_temp = TEMP_MIN_F
self._attr_max_temp = TEMP_MAX_F
@property
def current_temperature(self) -> float:
@ -169,32 +162,13 @@ class GreeClimateEntity(CoordinatorEntity[DeviceDataUpdateCoordinator], ClimateE
_LOGGER.debug(
"Setting temperature to %d for %s",
temperature,
self._name,
self._attr_name,
)
self.coordinator.device.target_temperature = round(temperature)
await self.coordinator.push_state_update()
self.async_write_ha_state()
@property
def min_temp(self) -> float:
"""Return the minimum temperature supported by the device."""
if self.temperature_unit == UnitOfTemperature.CELSIUS:
return TEMP_MIN
return TEMP_MIN_F
@property
def max_temp(self) -> float:
"""Return the maximum temperature supported by the device."""
if self.temperature_unit == UnitOfTemperature.CELSIUS:
return TEMP_MAX
return TEMP_MAX_F
@property
def target_temperature_step(self) -> float:
"""Return the target temperature step support by the device."""
return TARGET_TEMPERATURE_STEP
@property
def hvac_mode(self) -> HVACMode | None:
"""Return the current HVAC mode for the device."""
@ -211,7 +185,7 @@ class GreeClimateEntity(CoordinatorEntity[DeviceDataUpdateCoordinator], ClimateE
_LOGGER.debug(
"Setting HVAC mode to %s for device %s",
hvac_mode,
self._name,
self._attr_name,
)
if hvac_mode == HVACMode.OFF:
@ -229,7 +203,7 @@ class GreeClimateEntity(CoordinatorEntity[DeviceDataUpdateCoordinator], ClimateE
async def async_turn_on(self) -> None:
"""Turn on the device."""
_LOGGER.debug("Turning on HVAC for device %s", self._name)
_LOGGER.debug("Turning on HVAC for device %s", self._attr_name)
self.coordinator.device.power = True
await self.coordinator.push_state_update()
@ -237,19 +211,12 @@ class GreeClimateEntity(CoordinatorEntity[DeviceDataUpdateCoordinator], ClimateE
async def async_turn_off(self) -> None:
"""Turn off the device."""
_LOGGER.debug("Turning off HVAC for device %s", self._name)
_LOGGER.debug("Turning off HVAC for device %s", self._attr_name)
self.coordinator.device.power = False
await self.coordinator.push_state_update()
self.async_write_ha_state()
@property
def hvac_modes(self) -> list[HVACMode]:
"""Return the HVAC modes support by the device."""
modes = [*HVAC_MODES_REVERSE]
modes.append(HVACMode.OFF)
return modes
@property
def preset_mode(self) -> str:
"""Return the current preset mode for the device."""
@ -271,7 +238,7 @@ class GreeClimateEntity(CoordinatorEntity[DeviceDataUpdateCoordinator], ClimateE
_LOGGER.debug(
"Setting preset mode to %s for device %s",
preset_mode,
self._name,
self._attr_name,
)
self.coordinator.device.steady_heat = False
@ -291,11 +258,6 @@ class GreeClimateEntity(CoordinatorEntity[DeviceDataUpdateCoordinator], ClimateE
await self.coordinator.push_state_update()
self.async_write_ha_state()
@property
def preset_modes(self) -> list[str]:
"""Return the preset modes support by the device."""
return PRESET_MODES
@property
def fan_mode(self) -> str | None:
"""Return the current fan mode for the device."""
@ -311,11 +273,6 @@ class GreeClimateEntity(CoordinatorEntity[DeviceDataUpdateCoordinator], ClimateE
await self.coordinator.push_state_update()
self.async_write_ha_state()
@property
def fan_modes(self) -> list[str]:
"""Return the fan modes support by the device."""
return [*FAN_MODES_REVERSE]
@property
def swing_mode(self) -> str:
"""Return the current swing mode for the device."""
@ -338,7 +295,7 @@ class GreeClimateEntity(CoordinatorEntity[DeviceDataUpdateCoordinator], ClimateE
_LOGGER.debug(
"Setting swing mode to %s for device %s",
swing_mode,
self._name,
self._attr_name,
)
self.coordinator.device.horizontal_swing = HorizontalSwing.Center
@ -350,8 +307,3 @@ class GreeClimateEntity(CoordinatorEntity[DeviceDataUpdateCoordinator], ClimateE
await self.coordinator.push_state_update()
self.async_write_ha_state()
@property
def swing_modes(self) -> list[str]:
"""Return the swing modes currently supported for this device."""
return SWING_MODES

View file

@ -13,25 +13,13 @@ class GreeEntity(CoordinatorEntity[DeviceDataUpdateCoordinator]):
"""Initialize the entity."""
super().__init__(coordinator)
self._desc = desc
self._name = f"{coordinator.device.device_info.name}"
self._mac = coordinator.device.device_info.mac
@property
def name(self):
"""Return the name of the node."""
return f"{self._name} {self._desc}"
@property
def unique_id(self):
"""Return the unique id based for the node."""
return f"{self._mac}_{self._desc}"
@property
def device_info(self) -> DeviceInfo:
"""Return info about the device."""
return DeviceInfo(
connections={(CONNECTION_NETWORK_MAC, self._mac)},
identifiers={(DOMAIN, self._mac)},
name = coordinator.device.device_info.name
mac = coordinator.device.device_info.mac
self._attr_name = f"{name} {desc}"
self._attr_unique_id = f"{mac}_{desc}"
self._attr_device_info = DeviceInfo(
connections={(CONNECTION_NETWORK_MAC, mac)},
identifiers={(DOMAIN, mac)},
manufacturer="Gree",
name=self._name,
name=name,
)