Skip to content

Commit

Permalink
[Draft] Use user's preferred temperature unit instead of hardcoding c…
Browse files Browse the repository at this point in the history
…elsius (#460)

* Use user's preferred temperature unit instead of hardcoding celsius

* Fix warnings about using is instead of == in tests
  • Loading branch information
mbbush committed May 13, 2024
1 parent 189418e commit 6bd1b11
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 33 deletions.
8 changes: 4 additions & 4 deletions custom_components/versatile_thermostat/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ def __init__(
self.entity_id = f"{NUMBER_DOMAIN}.{slugify(name)}_preset_{preset_name}"
self._attr_unique_id = f"central_configuration_preset_{preset_name}"
self._attr_device_class = NumberDeviceClass.TEMPERATURE
self._attr_native_unit_of_measurement = UnitOfTemperature.CELSIUS
self._attr_native_unit_of_measurement = hass.config.units.temperature_unit

self._attr_native_step = entry_infos.get(CONF_STEP_TEMPERATURE, 0.5)
self._attr_native_min_value = entry_infos.get(CONF_TEMP_MIN)
Expand Down Expand Up @@ -371,7 +371,7 @@ def native_unit_of_measurement(self) -> str | None:
# TODO Kelvin ? It seems not because all internal values are stored in
# ° Celsius but only the render in front can be in °K depending on the
# user configuration.
return UnitOfTemperature.CELSIUS
return self.hass.config.units.temperature_unit


class TemperatureNumber( # pylint: disable=abstract-method
Expand Down Expand Up @@ -400,7 +400,7 @@ def __init__(

self._attr_unique_id = f"{self._device_name}_preset_{preset_name}"
self._attr_device_class = NumberDeviceClass.TEMPERATURE
self._attr_native_unit_of_measurement = UnitOfTemperature.CELSIUS
self._attr_native_unit_of_measurement = hass.config.units.temperature_unit

self._has_central_main_attributes = entry_infos.get(
CONF_USE_MAIN_CENTRAL_CONFIG, False
Expand Down Expand Up @@ -498,7 +498,7 @@ def __str__(self):
def native_unit_of_measurement(self) -> str | None:
"""The unit of measurement"""
if not self.my_climate:
return UnitOfTemperature.CELSIUS
return self.hass.config.units.temperature_unit
return self.my_climate.temperature_unit

def init_min_max_step(self, entry_infos=None):
Expand Down
4 changes: 2 additions & 2 deletions custom_components/versatile_thermostat/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ def state_class(self) -> SensorStateClass | None:
@property
def native_unit_of_measurement(self) -> str | None:
if not self.my_climate:
return UnitOfTemperature.CELSIUS
return self.hass.config.units.temperature_unit
return self.my_climate.temperature_unit

@property
Expand Down Expand Up @@ -621,7 +621,7 @@ def state_class(self) -> SensorStateClass | None:
@property
def native_unit_of_measurement(self) -> str | None:
if not self.my_climate:
return UnitOfTemperature.CELSIUS
return self.hass.config.units.temperature_unit
return self.my_climate.temperature_unit

@property
Expand Down
2 changes: 1 addition & 1 deletion custom_components/versatile_thermostat/underlyings.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ def swing_modes(self) -> list[str]:
def temperature_unit(self) -> str:
"""Get the temperature_unit"""
if not self.is_initialized:
return UnitOfTemperature.CELSIUS
return self._hass.config.units.temperature_unit
return self._underlying_climate.temperature_unit

@property
Expand Down
2 changes: 1 addition & 1 deletion tests/test_central_boiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ async def test_add_a_central_config_with_boiler(
assert api.nb_active_device_for_boiler == 0

assert api.nb_active_device_for_boiler_threshold_entity is not None
assert api.nb_active_device_for_boiler_threshold is 1 # the default value is 1
assert api.nb_active_device_for_boiler_threshold == 1 # the default value is 1


async def test_update_central_boiler_state_simple(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_central_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,4 +514,4 @@ async def test_migration_of_central_config(
assert api.nb_active_device_for_boiler == 0

assert api.nb_active_device_for_boiler_threshold_entity is not None
assert api.nb_active_device_for_boiler_threshold is 1 # the default value is 1
assert api.nb_active_device_for_boiler_threshold == 1 # the default value is 1
48 changes: 24 additions & 24 deletions tests/test_movement.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ async def test_movement_management_time_not_enough(
await send_ext_temperature_change_event(entity, 10, event_timestamp)

await send_presence_change_event(entity, True, False, event_timestamp)
assert entity.presence_state is "on"
assert entity.presence_state == "on"

# starts detecting motion with time not enough
with patch(
Expand All @@ -110,7 +110,7 @@ async def test_movement_management_time_not_enough(
assert entity.target_temperature == 18
# state is not changed if time is not enough
assert entity.motion_state is None
assert entity.presence_state is "on"
assert entity.presence_state == "on"

assert mock_send_event.call_count == 0
# Change is not confirmed
Expand Down Expand Up @@ -141,8 +141,8 @@ async def test_movement_management_time_not_enough(
assert entity.preset_mode is PRESET_ACTIVITY
# because motion is detected yet
assert entity.target_temperature == 19
assert entity.motion_state is "on"
assert entity.presence_state is "on"
assert entity.motion_state == "on"
assert entity.presence_state == "on"

# stop detecting motion with off delay too low
with patch(
Expand All @@ -167,8 +167,8 @@ async def test_movement_management_time_not_enough(
assert entity.preset_mode is PRESET_ACTIVITY
# because no motion is detected yet
assert entity.target_temperature == 19
assert entity.motion_state is "on"
assert entity.presence_state is "on"
assert entity.motion_state == "on"
assert entity.presence_state == "on"

assert mock_send_event.call_count == 0
# The heater must heat now
Expand Down Expand Up @@ -199,8 +199,8 @@ async def test_movement_management_time_not_enough(
assert entity.preset_mode is PRESET_ACTIVITY
# because no motion is detected yet
assert entity.target_temperature == 18
assert entity.motion_state is "off"
assert entity.presence_state is "on"
assert entity.motion_state == "off"
assert entity.presence_state == "on"

assert mock_send_event.call_count == 0
# The heater must stop heating now
Expand Down Expand Up @@ -280,7 +280,7 @@ async def test_movement_management_time_enough_and_presence(
await send_ext_temperature_change_event(entity, 10, event_timestamp)

await send_presence_change_event(entity, True, False, event_timestamp)
assert entity.presence_state is "on"
assert entity.presence_state == "on"

# starts detecting motion
with patch(
Expand All @@ -302,8 +302,8 @@ async def test_movement_management_time_enough_and_presence(
assert entity.preset_mode is PRESET_ACTIVITY
# because motion is detected yet -> switch to Boost mode
assert entity.target_temperature == 19
assert entity.motion_state is "on"
assert entity.presence_state is "on"
assert entity.motion_state == "on"
assert entity.presence_state == "on"

assert mock_send_event.call_count == 0
# Change is confirmed. Heater should be started
Expand Down Expand Up @@ -331,8 +331,8 @@ async def test_movement_management_time_enough_and_presence(
assert entity.preset_mode is PRESET_ACTIVITY
# because no motion is detected yet
assert entity.target_temperature == 18
assert entity.motion_state is "off"
assert entity.presence_state is "on"
assert entity.motion_state == "off"
assert entity.presence_state == "on"

assert mock_send_event.call_count == 0
assert mock_heater_on.call_count == 0
Expand Down Expand Up @@ -412,7 +412,7 @@ async def test_movement_management_time_enoughand_not_presence(
await send_ext_temperature_change_event(entity, 10, event_timestamp)

await send_presence_change_event(entity, False, True, event_timestamp)
assert entity.presence_state is "off"
assert entity.presence_state == "off"

# starts detecting motion
with patch(
Expand All @@ -434,8 +434,8 @@ async def test_movement_management_time_enoughand_not_presence(
assert entity.preset_mode is PRESET_ACTIVITY
# because motion is detected yet -> switch to Boost away mode
assert entity.target_temperature == 19.1
assert entity.motion_state is "on"
assert entity.presence_state is "off"
assert entity.motion_state == "on"
assert entity.presence_state == "off"

assert mock_send_event.call_count == 0
# Change is confirmed. Heater should be started
Expand Down Expand Up @@ -463,8 +463,8 @@ async def test_movement_management_time_enoughand_not_presence(
assert entity.preset_mode is PRESET_ACTIVITY
# because no motion is detected yet
assert entity.target_temperature == 18.1
assert entity.motion_state is "off"
assert entity.presence_state is "off"
assert entity.motion_state == "off"
assert entity.presence_state == "off"

assert mock_send_event.call_count == 0
# 18.1 starts heating with a low on_percent
Expand Down Expand Up @@ -546,7 +546,7 @@ async def test_movement_management_with_stop_during_condition(
await send_ext_temperature_change_event(entity, 10, event_timestamp)

await send_presence_change_event(entity, False, True, event_timestamp)
assert entity.presence_state is "off"
assert entity.presence_state == "off"

# starts detecting motion
with patch(
Expand All @@ -569,7 +569,7 @@ async def test_movement_management_with_stop_during_condition(
# because motion is detected yet -> switch to Boost mode
assert entity.target_temperature == 18
assert entity.motion_state is None
assert entity.presence_state is "off"
assert entity.presence_state == "off"

# Send a stop detection
event_timestamp = now - timedelta(minutes=4)
Expand All @@ -580,7 +580,7 @@ async def test_movement_management_with_stop_during_condition(
assert entity.preset_mode is PRESET_ACTIVITY
assert entity.target_temperature == 18
assert entity.motion_state is None
assert entity.presence_state is "off"
assert entity.presence_state == "off"

# Resend a start detection
event_timestamp = now - timedelta(minutes=3)
Expand All @@ -592,10 +592,10 @@ async def test_movement_management_with_stop_during_condition(
# still no motion detected
assert entity.target_temperature == 18
assert entity.motion_state is None
assert entity.presence_state is "off"
assert entity.presence_state == "off"

await try_condition1(None)
# We should have switch this time
assert entity.target_temperature == 19 # Boost
assert entity.motion_state is "on" # switch to movement on
assert entity.presence_state is "off" # Non change
assert entity.motion_state == "on" # switch to movement on
assert entity.presence_state == "off" # Non change

0 comments on commit 6bd1b11

Please sign in to comment.