Home Assistant Unofficial Reference 2024.12.1
water_heater.py
Go to the documentation of this file.
1 """Support for OSO Energy water heaters."""
2 
3 import datetime as dt
4 from typing import Any
5 
6 from apyosoenergyapi import OSOEnergy
7 from apyosoenergyapi.helper.const import OSOEnergyWaterHeaterData
8 import voluptuous as vol
9 
11  STATE_ECO,
12  STATE_ELECTRIC,
13  STATE_HIGH_DEMAND,
14  STATE_OFF,
15  WaterHeaterEntity,
16  WaterHeaterEntityFeature,
17 )
18 from homeassistant.config_entries import ConfigEntry
19 from homeassistant.const import UnitOfTemperature
20 from homeassistant.core import HomeAssistant, ServiceResponse, SupportsResponse
21 from homeassistant.helpers import config_validation as cv, entity_platform
22 from homeassistant.helpers.entity_platform import AddEntitiesCallback
23 import homeassistant.util.dt as dt_util
24 from homeassistant.util.json import JsonValueType
25 
26 from .const import DOMAIN
27 from .entity import OSOEnergyEntity
28 
29 ATTR_UNTIL_TEMP_LIMIT = "until_temp_limit"
30 ATTR_V40MIN = "v40_min"
31 CURRENT_OPERATION_MAP: dict[str, Any] = {
32  "default": {
33  "off": STATE_OFF,
34  "powersave": STATE_OFF,
35  "extraenergy": STATE_HIGH_DEMAND,
36  },
37  "oso": {
38  "auto": STATE_ECO,
39  "off": STATE_OFF,
40  "powersave": STATE_OFF,
41  "extraenergy": STATE_HIGH_DEMAND,
42  },
43 }
44 SERVICE_GET_PROFILE = "get_profile"
45 SERVICE_SET_PROFILE = "set_profile"
46 SERVICE_SET_V40MIN = "set_v40_min"
47 SERVICE_TURN_OFF = "turn_off"
48 SERVICE_TURN_ON = "turn_on"
49 
50 
52  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
53 ) -> None:
54  """Set up OSO Energy heater based on a config entry."""
55  osoenergy = hass.data[DOMAIN][entry.entry_id]
56  devices = osoenergy.session.device_list.get("water_heater")
57  if not devices:
58  return
59  async_add_entities((OSOEnergyWaterHeater(osoenergy, dev) for dev in devices), True)
60 
61  platform = entity_platform.async_get_current_platform()
62 
63  platform.async_register_entity_service(
64  SERVICE_GET_PROFILE,
65  {},
66  OSOEnergyWaterHeater.async_get_profile.__name__,
67  supports_response=SupportsResponse.ONLY,
68  )
69 
70  service_set_profile_schema = cv.make_entity_service_schema(
71  {
72  vol.Optional(f"hour_{hour:02d}"): vol.All(
73  vol.Coerce(int), vol.Range(min=10, max=75)
74  )
75  for hour in range(24)
76  }
77  )
78 
79  platform.async_register_entity_service(
80  SERVICE_SET_PROFILE,
81  service_set_profile_schema,
82  OSOEnergyWaterHeater.async_set_profile.__name__,
83  )
84 
85  platform.async_register_entity_service(
86  SERVICE_SET_V40MIN,
87  {
88  vol.Required(ATTR_V40MIN): vol.All(
89  vol.Coerce(float), vol.Range(min=200, max=550)
90  ),
91  },
92  OSOEnergyWaterHeater.async_set_v40_min.__name__,
93  )
94 
95  platform.async_register_entity_service(
96  SERVICE_TURN_OFF,
97  {vol.Required(ATTR_UNTIL_TEMP_LIMIT): vol.All(cv.boolean)},
98  OSOEnergyWaterHeater.async_oso_turn_off.__name__,
99  )
100 
101  platform.async_register_entity_service(
102  SERVICE_TURN_ON,
103  {vol.Required(ATTR_UNTIL_TEMP_LIMIT): vol.All(cv.boolean)},
104  OSOEnergyWaterHeater.async_oso_turn_on.__name__,
105  )
106 
107 
108 def _get_utc_hour(local_hour: int) -> dt.datetime:
109  """Convert the requested local hour to a utc hour for the day.
110 
111  Args:
112  local_hour: the local hour (0-23) for the current day to be converted.
113 
114  Returns:
115  Datetime representation for the requested hour in utc time for the day.
116 
117  """
118  now = dt_util.now()
119  local_time = now.replace(hour=local_hour, minute=0, second=0, microsecond=0)
120  return dt_util.as_utc(local_time)
121 
122 
123 def _get_local_hour(utc_hour: int) -> dt.datetime:
124  """Convert the requested utc hour to a local hour for the day.
125 
126  Args:
127  utc_hour: the utc hour (0-23) for the current day to be converted.
128 
129  Returns:
130  Datetime representation for the requested hour in local time for the day.
131 
132  """
133  utc_now = dt_util.utcnow()
134  utc_time = utc_now.replace(hour=utc_hour, minute=0, second=0, microsecond=0)
135  return dt_util.as_local(utc_time)
136 
137 
138 def _convert_profile_to_local(values: list[float]) -> list[JsonValueType]:
139  """Convert UTC profile to local.
140 
141  Receives a device temperature schedule - 24 values for the day where the index represents the hour of the day in UTC.
142  Converts the schedule to local time.
143 
144  Args:
145  values: list of floats representing the 24 hour temperature schedule for the device
146  Returns:
147  The device temperature schedule in local time.
148 
149  """
150  profile: list[JsonValueType] = [0.0] * 24
151  for hour in range(24):
152  local_hour = _get_local_hour(hour)
153  profile[local_hour.hour] = float(values[hour])
154 
155  return profile
156 
157 
159  OSOEnergyEntity[OSOEnergyWaterHeaterData], WaterHeaterEntity
160 ):
161  """OSO Energy Water Heater Device."""
162 
163  _attr_name = None
164  _attr_supported_features = (
165  WaterHeaterEntityFeature.TARGET_TEMPERATURE | WaterHeaterEntityFeature.ON_OFF
166  )
167  _attr_temperature_unit = UnitOfTemperature.CELSIUS
168 
169  def __init__(
170  self,
171  instance: OSOEnergy,
172  entity_data: OSOEnergyWaterHeaterData,
173  ) -> None:
174  """Initialize the OSO Energy water heater."""
175  super().__init__(instance, entity_data)
176  self._attr_unique_id_attr_unique_id = entity_data.device_id
177 
178  @property
179  def available(self) -> bool:
180  """Return if the device is available."""
181  return self.entity_dataentity_data.available
182 
183  @property
184  def current_operation(self) -> str:
185  """Return current operation."""
186  status = self.entity_dataentity_data.current_operation
187  if status == "off":
188  return STATE_OFF
189 
190  optimization_mode = self.entity_dataentity_data.optimization_mode.lower()
191  heater_mode = self.entity_dataentity_data.heater_mode.lower()
192  if optimization_mode in CURRENT_OPERATION_MAP:
193  return CURRENT_OPERATION_MAP[optimization_mode].get(
194  heater_mode, STATE_ELECTRIC
195  )
196 
197  return CURRENT_OPERATION_MAP["default"].get(heater_mode, STATE_ELECTRIC)
198 
199  @property
200  def current_temperature(self) -> float:
201  """Return the current temperature of the heater."""
202  return self.entity_dataentity_data.current_temperature
203 
204  @property
205  def target_temperature(self) -> float:
206  """Return the temperature we try to reach."""
207  return self.entity_dataentity_data.target_temperature
208 
209  @property
210  def target_temperature_high(self) -> float:
211  """Return the temperature we try to reach."""
212  return self.entity_dataentity_data.target_temperature_high
213 
214  @property
215  def target_temperature_low(self) -> float:
216  """Return the temperature we try to reach."""
217  return self.entity_dataentity_data.target_temperature_low
218 
219  @property
220  def min_temp(self) -> float:
221  """Return the minimum temperature."""
222  return self.entity_dataentity_data.min_temperature
223 
224  @property
225  def max_temp(self) -> float:
226  """Return the maximum temperature."""
227  return self.entity_dataentity_data.max_temperature
228 
229  async def async_turn_on(self, **kwargs) -> None:
230  """Turn on hotwater."""
231  await self.osoenergy.hotwater.turn_on(self.entity_dataentity_data, True)
232 
233  async def async_turn_off(self, **kwargs) -> None:
234  """Turn off hotwater."""
235  await self.osoenergy.hotwater.turn_off(self.entity_dataentity_data, True)
236 
237  async def async_set_temperature(self, **kwargs: Any) -> None:
238  """Set new target temperature."""
239  target_temperature = int(kwargs.get("temperature", self.target_temperaturetarget_temperaturetarget_temperature))
240  profile = [target_temperature] * 24
241 
242  await self.osoenergy.hotwater.set_profile(self.entity_dataentity_data, profile)
243 
244  async def async_get_profile(self) -> ServiceResponse:
245  """Return the current temperature profile of the device."""
246 
247  profile = self.entity_dataentity_data.profile
248  return {"profile": _convert_profile_to_local(profile)}
249 
250  async def async_set_profile(self, **kwargs: Any) -> None:
251  """Handle the service call."""
252  profile = self.entity_dataentity_data.profile
253 
254  for hour in range(24):
255  hour_key = f"hour_{hour:02d}"
256 
257  if hour_key in kwargs:
258  profile[_get_utc_hour(hour).hour] = kwargs[hour_key]
259 
260  await self.osoenergy.hotwater.set_profile(self.entity_dataentity_data, profile)
261 
262  async def async_set_v40_min(self, v40_min) -> None:
263  """Handle the service call."""
264  await self.osoenergy.hotwater.set_v40_min(self.entity_dataentity_data, v40_min)
265 
266  async def async_oso_turn_off(self, until_temp_limit) -> None:
267  """Handle the service call."""
268  await self.osoenergy.hotwater.turn_off(self.entity_dataentity_data, until_temp_limit)
269 
270  async def async_oso_turn_on(self, until_temp_limit) -> None:
271  """Handle the service call."""
272  await self.osoenergy.hotwater.turn_on(self.entity_dataentity_data, until_temp_limit)
273 
274  async def async_update(self) -> None:
275  """Update all Node data from Hive."""
276  await self.osoenergy.session.update_data()
277  self.entity_dataentity_data = await self.osoenergy.hotwater.get_water_heater(
278  self.entity_dataentity_data
279  )
None __init__(self, OSOEnergy instance, OSOEnergyWaterHeaterData entity_data)
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
list[JsonValueType] _convert_profile_to_local(list[float] values)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: water_heater.py:53