1+ from datetime import datetime
12import logging
3+ from typing import List
24
35from homeassistant .const import (
46 STATE_UNAVAILABLE ,
79)
810from homeassistant .core import HomeAssistant , callback
911
12+ from homeassistant .util .dt import (now )
1013from homeassistant .helpers .update_coordinator import (
1114 CoordinatorEntity
1215)
13- from homeassistant .components .number import RestoreNumber , NumberDeviceClass , NumberMode
14- from homeassistant .util .dt import (now )
16+ from homeassistant .components .sensor import (
17+ RestoreSensor ,
18+ SensorDeviceClass ,
19+ SensorStateClass ,
20+ )
1521
1622from .base import (BaseOctopusEnergyHeatPumpSensor )
1723from ..utils .attributes import dict_to_typed_dict
1824from ..api_client .heat_pump import HeatPump
1925from ..coordinators .heat_pump_configuration_and_status import HeatPumpCoordinatorResult
20- from ..api_client import OctopusEnergyApiClient
2126
2227_LOGGER = logging .getLogger (__name__ )
2328
24- class OctopusEnergyHeatPumpFixedTargetFlowTemperature (CoordinatorEntity , RestoreNumber , BaseOctopusEnergyHeatPumpSensor ):
25- """Sensor for reading and setting the fixed target flow temperature of a heat pump."""
29+ class OctopusEnergyHeatPumpSensorFixedTargetFlowTemperature (CoordinatorEntity , BaseOctopusEnergyHeatPumpSensor , RestoreSensor ):
30+ """Sensor for displaying the live heat output of a heat pump."""
2631
27- def __init__ (self , hass : HomeAssistant , client : OctopusEnergyApiClient , coordinator , heat_pump_id : str , heat_pump : HeatPump ):
32+ def __init__ (self , hass : HomeAssistant , coordinator , heat_pump_id : str , heat_pump : HeatPump ):
2833 """Init sensor."""
2934 # Pass coordinator to base class
3035 CoordinatorEntity .__init__ (self , coordinator )
31- BaseOctopusEnergyHeatPumpSensor .__init__ (self , hass , heat_pump_id , heat_pump , entity_domain = "number" )
36+ BaseOctopusEnergyHeatPumpSensor .__init__ (self , hass , heat_pump_id , heat_pump )
3237
33-
3438 self ._state = None
35- self ._client = client
3639 self ._last_updated = None
37- self ._attr_native_min_value = 30
38- self ._attr_native_max_value = 70
39- self ._attr_native_step = 1
40- self ._attr_mode = NumberMode .BOX
4140
4241 @property
4342 def unique_id (self ):
@@ -49,45 +48,35 @@ def name(self):
4948 """Name of the sensor."""
5049 return f"Fixed Target Flow Temperature Heat Pump ({ self ._heat_pump_id } )"
5150
51+ @property
52+ def state_class (self ):
53+ """The state class of sensor"""
54+ return SensorStateClass .MEASUREMENT
55+
5256 @property
5357 def device_class (self ):
5458 """The type of sensor"""
55- return NumberDeviceClass .TEMPERATURE
56-
59+ return SensorDeviceClass .TEMPERATURE
60+
61+ @property
62+ def icon (self ):
63+ """Icon of the sensor."""
64+ return "mdi:thermometer"
65+
5766 @property
5867 def native_unit_of_measurement (self ):
59- """The unit of measurement of sensor"""
68+ """Unit of measurement of the sensor. """
6069 return UnitOfTemperature .CELSIUS
6170
6271 @property
6372 def extra_state_attributes (self ):
6473 """Attributes of the sensor."""
6574 return self ._attributes
66-
75+
6776 @property
68- def native_value (self ) -> float :
77+ def native_value (self ):
6978 return self ._state
7079
71- async def async_set_native_value (self , value : float ) -> None :
72- """Set new value."""
73- if value and value % self ._attr_native_step == 0 :
74- try :
75- await self ._client .async_set_heat_pump_fixed_target_flow_temp (
76- self ._heat_pump_id ,
77- value
78- )
79- except Exception as e :
80- if self ._is_mocked :
81- _LOGGER .warning (f'Suppress async_set_native_value error due to mocking mode: { e } ' )
82- else :
83- raise
84-
85- self ._state = value
86- self ._last_updated = now ()
87- self .async_write_ha_state ()
88- else :
89- raise Exception (f"Value must be between { self ._attr_native_min_value } and { self ._attr_native_max_value } and be a multiple of { self ._attr_native_step } " )
90-
9180 @callback
9281 def _handle_coordinator_update (self ) -> None :
9382 """Retrieve the configured fixed target flow temperature for the heat pump."""
@@ -100,7 +89,7 @@ def _handle_coordinator_update(self) -> None:
10089 and result .data .octoHeatPumpControllerConfiguration .heatPump is not None
10190 and result .data .octoHeatPumpControllerConfiguration .heatPump .heatingFlowTemperature is not None
10291 and result .data .octoHeatPumpControllerConfiguration .heatPump .heatingFlowTemperature .currentTemperature is not None ):
103- _LOGGER .debug (f"Updating OctopusEnergyHeatPumpNumberFixedTargetFlowTemperature for '{ self ._heat_pump_id } '" )
92+ _LOGGER .debug (f"Updating OctopusEnergyHeatPumpSensorFixedTargetFlowTemperature for '{ self ._heat_pump_id } '" )
10493
10594 self ._state = float (result .data .octoHeatPumpControllerConfiguration .heatPump .heatingFlowTemperature .currentTemperature .value )
10695 self ._last_updated = current
@@ -119,4 +108,4 @@ async def async_added_to_hass(self):
119108 self ._state = None if state .state in (STATE_UNAVAILABLE , STATE_UNKNOWN ) else last_sensor_state .native_value
120109 self ._attributes = dict_to_typed_dict (state .attributes , [])
121110
122- _LOGGER .debug (f'Restored OctopusEnergyHeatPumpNumberFixedTargetFlowTemperature state: { self ._state } ' )
111+ _LOGGER .debug (f'Restored OctopusEnergyHeatPumpSensorFixedTargetFlowTemperature state: { self ._state } ' )
0 commit comments