22from __future__ import annotations
33
44import logging
5+ from statistics import mean
56from typing import Any
67
78import voluptuous as vol
4243 CONF_TEMPERATURE_UNIT ,
4344 CONF_UNIQUE_ID ,
4445 STATE_UNAVAILABLE ,
45- TEMP_CELSIUS ,
46- TEMP_FAHRENHEIT ,
4746)
4847from homeassistant .core import Event , HomeAssistant , callback
4948from homeassistant .helpers import config_validation as cv , entity_registry as er
7069 {
7170 vol .Optional (CONF_NAME , default = DEFAULT_NAME ): cv .string ,
7271 vol .Optional (CONF_UNIQUE_ID ): cv .string ,
73- vol .Optional (CONF_TEMPERATURE_UNIT , default = TEMP_CELSIUS ): cv .string ,
72+ vol .Optional (CONF_TEMPERATURE_UNIT ): cv .temperature_unit ,
7473 vol .Required (CONF_ENTITIES ): cv .entities_domain (DOMAIN ),
7574 }
7675)
9291 HVACAction .FAN ,
9392 HVACAction .IDLE ,
9493 HVACAction .OFF ,
95- None ,
9694]
9795
9896
@@ -109,7 +107,7 @@ async def async_setup_platform(
109107 config .get (CONF_UNIQUE_ID ),
110108 config [CONF_NAME ],
111109 config [CONF_ENTITIES ],
112- config [ CONF_TEMPERATURE_UNIT ] ,
110+ config . get ( CONF_TEMPERATURE_UNIT , hass . config . units . temperature_unit ) ,
113111 )
114112 ]
115113 )
@@ -120,7 +118,7 @@ async def async_setup_entry(
120118 config_entry : ConfigEntry ,
121119 async_add_entities : AddEntitiesCallback ,
122120) -> None :
123- """Initialize Light Group config entry."""
121+ """Initialize Climate Group config entry."""
124122 registry = er .async_get (hass )
125123 entities = er .async_validate_entity_ids (
126124 registry , config_entry .options [CONF_ENTITIES ]
@@ -132,7 +130,9 @@ async def async_setup_entry(
132130 config_entry .entry_id ,
133131 config_entry .title ,
134132 entities ,
135- config_entry .options [CONF_TEMPERATURE_UNIT ],
133+ config_entry .options .get (
134+ CONF_TEMPERATURE_UNIT , hass .config .units .temperature_unit
135+ ),
136136 )
137137 ]
138138 )
@@ -145,7 +145,11 @@ class ClimateGroup(GroupEntity, ClimateEntity):
145145 _attr_assumed_state : bool = True
146146
147147 def __init__ (
148- self , unique_id : str | None , name : str , entity_ids : list [str ], unit : str
148+ self ,
149+ unique_id : str | None ,
150+ name : str ,
151+ entity_ids : list [str ],
152+ temperature_unit : str ,
149153 ) -> None :
150154 """Initialize a climate group."""
151155 self ._entity_ids = entity_ids
@@ -154,10 +158,7 @@ def __init__(
154158 self ._attr_unique_id = unique_id
155159 self ._attr_extra_state_attributes = {ATTR_ENTITY_ID : entity_ids }
156160
157- if "c" in unit .lower ():
158- self ._attr_temperature_unit = TEMP_CELSIUS
159- else :
160- self ._attr_temperature_unit = TEMP_FAHRENHEIT
161+ self ._attr_temperature_unit = temperature_unit
161162
162163 # Set some defaults (will be overwritten on update)
163164 self ._attr_supported_features = 0
@@ -206,28 +207,24 @@ def async_update_group_state(self) -> None:
206207 # Set group as unavailable if all members are unavailable or missing
207208 self ._attr_available = any (state .state != STATE_UNAVAILABLE for state in states )
208209
209- def _mean (* args : float ) -> float :
210- """Return the mean of the supplied values."""
211- return sum (args ) / len (args )
212-
213210 # Temperature settings
214211 self ._attr_target_temperature = reduce_attribute (
215- states , ATTR_TEMPERATURE , reduce = _mean
212+ states , ATTR_TEMPERATURE , reduce = lambda * data : mean ( data )
216213 )
217214
218215 self ._attr_target_temperature_step = reduce_attribute (
219216 states , ATTR_TARGET_TEMP_STEP , reduce = max
220217 )
221218
222219 self ._attr_target_temperature_low = reduce_attribute (
223- states , ATTR_TARGET_TEMP_LOW , reduce = _mean
220+ states , ATTR_TARGET_TEMP_LOW , reduce = lambda * data : mean ( data )
224221 )
225222 self ._attr_target_temperature_high = reduce_attribute (
226- states , ATTR_TARGET_TEMP_HIGH , reduce = _mean
223+ states , ATTR_TARGET_TEMP_HIGH , reduce = lambda * data : mean ( data )
227224 )
228225
229226 self ._attr_current_temperature = reduce_attribute (
230- states , ATTR_CURRENT_TEMPERATURE , reduce = _mean
227+ states , ATTR_CURRENT_TEMPERATURE , reduce = lambda * data : mean ( data )
231228 )
232229
233230 self ._attr_min_temp = reduce_attribute (states , ATTR_MIN_TEMP , reduce = max )
0 commit comments