Skip to content

Commit e1ef2a3

Browse files
authored
Merge pull request #61 from alepee/copilot/fix-default-base-temperature
Default base temperature and improve configuration UI
2 parents 35cd20f + 8c9480c commit e1ef2a3

File tree

5 files changed

+46
-53
lines changed

5 files changed

+46
-53
lines changed

custom_components/heating_cooling_degree_days/config_flow.py

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
from homeassistant import config_entries
88
from homeassistant.components.sensor import SensorDeviceClass
9-
from homeassistant.const import UnitOfTemperature
109
from homeassistant.helpers import selector
1110

1211
from .const import (
@@ -16,24 +15,18 @@
1615
CONF_INCLUDE_WEEKLY,
1716
CONF_TEMPERATURE_SENSOR,
1817
CONF_TEMPERATURE_UNIT,
19-
DEFAULT_BASE_TEMPERATURE,
18+
DEFAULT_BASE_TEMPERATURE_CELSIUS,
2019
DEFAULT_INCLUDE_COOLING,
2120
DEFAULT_INCLUDE_MONTHLY,
2221
DEFAULT_INCLUDE_WEEKLY,
22+
DEFAULT_NAME_WITH_HEATING,
23+
DEFAULT_NAME_WITH_HEATING_AND_COOLING,
2324
DOMAIN,
25+
MAP_DEFAULT_BASE_TEMPERATURE,
2426
)
2527

2628
_LOGGER = logging.getLogger(__name__)
2729

28-
TEMPERATURE_UNIT_MAPPING = {
29-
"celsius": UnitOfTemperature.CELSIUS,
30-
"fahrenheit": UnitOfTemperature.FAHRENHEIT,
31-
}
32-
33-
# Fixed titles in English
34-
TITLE_STANDARD = "Heating Degree Days"
35-
TITLE_WITH_COOLING = "Heating & Cooling Degree Days"
36-
3730

3831
class HDDConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
3932
"""Handle a config flow for Heating & Cooling Degree Days."""
@@ -54,17 +47,17 @@ async def async_step_user(self, user_input=None):
5447
errors["base"] = "invalid_temperature_sensor"
5548

5649
if not errors:
57-
# Map the temperature unit selection to the actual unit
58-
user_input[CONF_TEMPERATURE_UNIT] = TEMPERATURE_UNIT_MAPPING[
59-
user_input[CONF_TEMPERATURE_UNIT]
60-
]
50+
# Set the temperature unit to the user's preferred unit
51+
user_input[CONF_TEMPERATURE_UNIT] = (
52+
self.hass.config.units.temperature_unit
53+
)
6154

6255
include_cooling = user_input.get(
6356
CONF_INCLUDE_COOLING, DEFAULT_INCLUDE_COOLING
6457
)
6558

6659
# Use simple fixed titles
67-
title = TITLE_WITH_COOLING if include_cooling else TITLE_STANDARD
60+
title = self._get_default_name(include_cooling)
6861

6962
_LOGGER.debug("Creating integration with title: %s", title)
7063

@@ -84,16 +77,9 @@ async def async_step_user(self, user_input=None):
8477
),
8578
),
8679
vol.Required(
87-
CONF_BASE_TEMPERATURE, default=DEFAULT_BASE_TEMPERATURE
80+
CONF_BASE_TEMPERATURE,
81+
default=self._get_default_base_temperature(),
8882
): vol.Coerce(float),
89-
vol.Required(
90-
CONF_TEMPERATURE_UNIT, default="celsius"
91-
): selector.SelectSelector(
92-
selector.SelectSelectorConfig(
93-
options=["celsius", "fahrenheit"],
94-
translation_key="temperature_unit",
95-
)
96-
),
9783
vol.Required(
9884
CONF_INCLUDE_COOLING, default=DEFAULT_INCLUDE_COOLING
9985
): selector.BooleanSelector(),
@@ -108,6 +94,20 @@ async def async_step_user(self, user_input=None):
10894
errors=errors,
10995
)
11096

97+
def _get_default_base_temperature(self) -> float:
98+
"""Get the default base temperature based on user preferred unit system."""
99+
return MAP_DEFAULT_BASE_TEMPERATURE.get(
100+
self.hass.config.units.temperature_unit, DEFAULT_BASE_TEMPERATURE_CELSIUS
101+
)
102+
103+
def _get_default_name(self, include_cooling: bool) -> str:
104+
"""Get the default name based on heating and cooling degree days configuration."""
105+
return (
106+
DEFAULT_NAME_WITH_HEATING_AND_COOLING
107+
if include_cooling
108+
else DEFAULT_NAME_WITH_HEATING
109+
)
110+
111111
def _validate_sensor(self, entity_id):
112112
"""Validate the temperature sensor entity exists."""
113113
state = self.hass.states.get(entity_id)

custom_components/heating_cooling_degree_days/const.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from datetime import timedelta
44

5+
from homeassistant.const import UnitOfTemperature
6+
57
DOMAIN = "heating_cooling_degree_days"
68

79
CONF_TEMPERATURE_SENSOR = "temperature_sensor"
@@ -11,8 +13,16 @@
1113
CONF_INCLUDE_WEEKLY = "include_weekly"
1214
CONF_INCLUDE_MONTHLY = "include_monthly"
1315

14-
DEFAULT_BASE_TEMPERATURE = 65.0
15-
DEFAULT_NAME = "Heating & Cooling Degree Days"
16+
DEFAULT_BASE_TEMPERATURE_CELSIUS = 18.0
17+
DEFAULT_BASE_TEMPERATURE_FAHRENHEIT = 65.0
18+
MAP_DEFAULT_BASE_TEMPERATURE = {
19+
UnitOfTemperature.CELSIUS: DEFAULT_BASE_TEMPERATURE_CELSIUS,
20+
UnitOfTemperature.FAHRENHEIT: DEFAULT_BASE_TEMPERATURE_FAHRENHEIT,
21+
}
22+
23+
DEFAULT_NAME_WITH_HEATING = "Heating Degree Days"
24+
DEFAULT_NAME_WITH_HEATING_AND_COOLING = "Heating & Cooling Degree Days"
25+
1626
DEFAULT_INCLUDE_COOLING = False
1727
DEFAULT_INCLUDE_WEEKLY = True
1828
DEFAULT_INCLUDE_MONTHLY = True

custom_components/heating_cooling_degree_days/sensor.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
SensorStateClass,
1010
)
1111
from homeassistant.config_entries import ConfigEntry
12-
from homeassistant.const import UnitOfTemperature
1312
from homeassistant.core import HomeAssistant
1413
from homeassistant.helpers.entity_platform import AddEntitiesCallback
1514
from homeassistant.helpers.update_coordinator import CoordinatorEntity
@@ -109,11 +108,7 @@ def __init__(
109108
sensor_type_desc = "Heating"
110109

111110
# Set the unit of measurement based on temperature unit
112-
if coordinator.temperature_unit == UnitOfTemperature.FAHRENHEIT:
113-
self._attr_native_unit_of_measurement = "°F·d"
114-
else:
115-
# Default to Celsius
116-
self._attr_native_unit_of_measurement = "°C·d"
111+
self._attr_native_unit_of_measurement = f"{coordinator.temperature_unit}·d"
117112

118113
_LOGGER.debug(
119114
"Initialized %s Degree Days sensor: %s with unit %s",

custom_components/heating_cooling_degree_days/translations/en.json

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,19 @@
77
"data": {
88
"temperature_sensor": "Outdoor Temperature Sensor",
99
"base_temperature": "Base Temperature",
10-
"temperature_unit": "Temperature Unit",
1110
"include_cooling": "Include Cooling Degree Days (CDD)",
1211
"include_weekly": "Include Weekly Sensors",
1312
"include_monthly": "Include Monthly Sensors"
13+
},
14+
"data_description": {
15+
"base_temperature": "The default base temperature is 18°C for Celsius or 65°F for Fahrenheit, depending on your unit system"
1416
}
1517
}
1618
},
1719
"error": {
1820
"invalid_temperature_sensor": "Invalid temperature sensor. Please select a valid temperature sensor."
1921
}
2022
},
21-
"selector": {
22-
"temperature_unit": {
23-
"options": {
24-
"celsius": "Celsius (°C)",
25-
"fahrenheit": "Fahrenheit (°F)"
26-
}
27-
}
28-
},
2923
"entity": {
3024
"sensor": {
3125
"hdd_daily": {
@@ -48,4 +42,4 @@
4842
}
4943
}
5044
}
51-
}
45+
}

custom_components/heating_cooling_degree_days/translations/fr.json

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,19 @@
77
"data": {
88
"temperature_sensor": "Capteur de température extérieure",
99
"base_temperature": "Température de base",
10-
"temperature_unit": "Unité de température",
1110
"include_cooling": "Inclure les degrés-jours de refroidissement (DJR)",
1211
"include_weekly": "Inclure les capteurs hebdomadaires",
1312
"include_monthly": "Inclure les capteurs mensuels"
13+
},
14+
"data_description": {
15+
"base_temperature": "La température de base par défaut est de 18°C pour Celsius ou 65°F pour Fahrenheit, selon votre système d'unités"
1416
}
1517
}
1618
},
1719
"error": {
1820
"invalid_temperature_sensor": "Capteur de température invalide. Veuillez sélectionner un capteur de température valide."
1921
}
2022
},
21-
"selector": {
22-
"temperature_unit": {
23-
"options": {
24-
"celsius": "Celsius (°C)",
25-
"fahrenheit": "Fahrenheit (°F)"
26-
}
27-
}
28-
},
2923
"entity": {
3024
"sensor": {
3125
"hdd_daily": {
@@ -48,4 +42,4 @@
4842
}
4943
}
5044
}
51-
}
45+
}

0 commit comments

Comments
 (0)