1+ from datetime import datetime
2+ import logging
3+ from typing import List
4+
5+ from homeassistant .const import (
6+ STATE_UNAVAILABLE ,
7+ STATE_UNKNOWN ,
8+ UnitOfTemperature
9+ )
10+ from homeassistant .core import HomeAssistant , callback
11+
12+ from homeassistant .util .dt import (now )
13+ from homeassistant .helpers .update_coordinator import (
14+ CoordinatorEntity
15+ )
16+ from homeassistant .components .sensor import (
17+ RestoreSensor ,
18+ SensorDeviceClass ,
19+ SensorStateClass ,
20+ )
21+
22+ from .base import (BaseOctopusEnergyHeatPumpSensor )
23+ from ..utils .attributes import dict_to_typed_dict
24+ from ..api_client .heat_pump import HeatPump
25+ from ..coordinators .heat_pump_configuration_and_status import HeatPumpCoordinatorResult
26+
27+ _LOGGER = logging .getLogger (__name__ )
28+
29+ class OctopusEnergyHeatPumpSensorFixedTargetFlowTemperature (CoordinatorEntity , BaseOctopusEnergyHeatPumpSensor , RestoreSensor ):
30+ """Sensor for displaying the live heat output of a heat pump."""
31+
32+ def __init__ (self , hass : HomeAssistant , coordinator , heat_pump_id : str , heat_pump : HeatPump ):
33+ """Init sensor."""
34+ # Pass coordinator to base class
35+ CoordinatorEntity .__init__ (self , coordinator )
36+ BaseOctopusEnergyHeatPumpSensor .__init__ (self , hass , heat_pump_id , heat_pump )
37+
38+ self ._state = None
39+ self ._last_updated = None
40+
41+ @property
42+ def unique_id (self ):
43+ """The id of the sensor."""
44+ return f"octopus_energy_heat_pump_{ self ._heat_pump_id } _fixed_target_flow_temperature"
45+
46+ @property
47+ def name (self ):
48+ """Name of the sensor."""
49+ return f"Fixed Target Flow Temperature Heat Pump ({ self ._heat_pump_id } )"
50+
51+ @property
52+ def state_class (self ):
53+ """The state class of sensor"""
54+ return SensorStateClass .MEASUREMENT
55+
56+ @property
57+ def device_class (self ):
58+ """The type of sensor"""
59+ return SensorDeviceClass .TEMPERATURE
60+
61+ @property
62+ def icon (self ):
63+ """Icon of the sensor."""
64+ return "mdi:thermometer"
65+
66+ @property
67+ def native_unit_of_measurement (self ):
68+ """Unit of measurement of the sensor."""
69+ return UnitOfTemperature .CELSIUS
70+
71+ @property
72+ def extra_state_attributes (self ):
73+ """Attributes of the sensor."""
74+ return self ._attributes
75+
76+ @property
77+ def native_value (self ):
78+ return self ._state
79+
80+ @callback
81+ def _handle_coordinator_update (self ) -> None :
82+ """Retrieve the configured fixed target flow temperature for the heat pump."""
83+ current = now ()
84+ result : HeatPumpCoordinatorResult = self .coordinator .data if self .coordinator is not None and self .coordinator .data is not None else None
85+
86+ if (result is not None
87+ and result .data is not None
88+ and result .data .octoHeatPumpControllerConfiguration is not None
89+ and result .data .octoHeatPumpControllerConfiguration .heatPump is not None
90+ and result .data .octoHeatPumpControllerConfiguration .heatPump .heatingFlowTemperature is not None
91+ and result .data .octoHeatPumpControllerConfiguration .heatPump .heatingFlowTemperature .currentTemperature is not None ):
92+ _LOGGER .debug (f"Updating OctopusEnergyHeatPumpSensorFixedTargetFlowTemperature for '{ self ._heat_pump_id } '" )
93+
94+ self ._state = float (result .data .octoHeatPumpControllerConfiguration .heatPump .heatingFlowTemperature .currentTemperature .value )
95+ self ._last_updated = current
96+
97+ self ._attributes = dict_to_typed_dict (self ._attributes )
98+ super ()._handle_coordinator_update ()
99+
100+ async def async_added_to_hass (self ):
101+ """Call when entity about to be added to hass."""
102+ # If not None, we got an initial value.
103+ await super ().async_added_to_hass ()
104+ state = await self .async_get_last_state ()
105+ last_sensor_state = await self .async_get_last_sensor_data ()
106+
107+ if state is not None and last_sensor_state is not None and self ._state is None :
108+ self ._state = None if state .state in (STATE_UNAVAILABLE , STATE_UNKNOWN ) else last_sensor_state .native_value
109+ self ._attributes = dict_to_typed_dict (state .attributes , [])
110+
111+ _LOGGER .debug (f'Restored OctopusEnergyHeatPumpSensorFixedTargetFlowTemperature state: { self ._state } ' )
0 commit comments