Skip to content

Commit 1ed4e9f

Browse files
Added a current power sensor
1 parent a14f0e5 commit 1ed4e9f

File tree

6 files changed

+73
-11
lines changed

6 files changed

+73
-11
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ This is a custom component to allow control of Panasonic Comfort Cloud devices i
1919
* Sensors for inside and outside temperature (where available)
2020
* Switch for toggling Nanoe
2121
* Daily energy sensor (optional)
22+
* Current Power sensor (Calculated from energy reading)
2223

2324

2425
# Configuration

custom_components/panasonic_cc/const.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
ATTR_TARGET_TEMPERATURE = "target_temperature"
1111
ATTR_INSIDE_TEMPERATURE = "inside_temperature"
1212
ATTR_OUTSIDE_TEMPERATURE = "outside_temperature"
13+
ATTR_DAILY_ENERGY = "daily_energy"
14+
ATTR_CURRENT_POWER = "current_power"
1315

1416
ATTR_STATE_ON = "on"
1517
ATTR_STATE_OFF = "off"
@@ -37,6 +39,18 @@
3739
CONF_TYPE: SENSOR_TYPE_TEMPERATURE,
3840
},
3941
}
42+
ENERGY_SENSOR_TYPES = {
43+
ATTR_DAILY_ENERGY: {
44+
CONF_NAME: "Daily Energy",
45+
CONF_ICON: "mdi:flash",
46+
CONF_TYPE: "kWh",
47+
},
48+
ATTR_CURRENT_POWER: {
49+
CONF_NAME: "Current Power",
50+
CONF_ICON: "mdi:flash",
51+
CONF_TYPE: "W",
52+
},
53+
}
4054

4155
SUPPORT_FLAGS = (
4256
SUPPORT_TARGET_TEMPERATURE |

custom_components/panasonic_cc/panasonic.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ def __init__(self, hass: HomeAssistantType, api, device, force_outside_sensor, e
3838
self.group = device['group']
3939
self.data = None
4040
self.energy_data = None
41+
self.last_energy_reading = 0
42+
self.last_energy_reading_time = None
43+
self.current_power_value = 0
44+
self.current_power_counter = 0
4145
self._available = True
4246
self.constants = constants
4347

@@ -68,6 +72,7 @@ async def do_update(self):
6872
async def do_update_energy(self):
6973
try:
7074
data= await self.hass.async_add_executor_job(self._api.history,self.id,"Day",datetime.now().strftime("%Y%m%d"))
75+
7176
except:
7277
_LOGGER.debug("Error trying to get device {id} state, probably expired token, trying to update it...".format(**self.device))
7378
await self.hass.async_add_executor_job(self._api.login)
@@ -76,6 +81,24 @@ async def do_update_energy(self):
7681
if data is None:
7782
_LOGGER.debug("Received no energy data for device {id}".format(**self.device))
7883
return
84+
t1 = datetime.now()
85+
c_energy = data['parameters']['energyConsumption']
86+
if self.last_energy_reading_time is not None:
87+
if c_energy != self.last_energy_reading:
88+
d = (t1 - self.last_energy_reading_time).total_seconds() / 60 / 60
89+
p = round((c_energy - self.last_energy_reading)*1000 / d)
90+
self.last_energy_reading = c_energy
91+
self.last_energy_reading_time = t1
92+
if p >= 0:
93+
self.current_power_value = p
94+
self.current_power_counter = 0
95+
else:
96+
self.current_power_counter += 1
97+
if self.current_power_counter > 30:
98+
self.current_power_value = 0
99+
else:
100+
self.last_energy_reading = c_energy
101+
self.last_energy_reading_time = t1
79102
self.energy_data = data
80103

81104
@property
@@ -165,6 +188,12 @@ def daily_energy(self):
165188
return self.energy_data['parameters']['energyConsumption']
166189
return None
167190

191+
@property
192+
def current_power(self):
193+
if not self.enable_energy_sensor:
194+
return None
195+
return self.current_power_value
196+
168197
async def turn_off(self):
169198
await self.hass.async_add_executor_job(
170199
self.set_device,

custom_components/panasonic_cc/sensor.py

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
"""Support for Panasonic sensors."""
22
import logging
33

4-
from homeassistant.const import CONF_ICON, CONF_NAME, TEMP_CELSIUS
4+
from homeassistant.const import CONF_ICON, CONF_NAME, TEMP_CELSIUS, CONF_TYPE
55
from homeassistant.helpers.entity import Entity
66

77
from . import DOMAIN as PANASONIC_DOMAIN, PANASONIC_DEVICES
8-
from .const import ATTR_INSIDE_TEMPERATURE, ATTR_OUTSIDE_TEMPERATURE, SENSOR_TYPES
8+
from .const import (
9+
ATTR_INSIDE_TEMPERATURE,
10+
ATTR_OUTSIDE_TEMPERATURE,
11+
SENSOR_TYPES,
12+
13+
ATTR_DAILY_ENERGY,
14+
ATTR_CURRENT_POWER,
15+
ENERGY_SENSOR_TYPES
16+
)
917

1018
_LOGGER = logging.getLogger(__name__)
1119

@@ -19,7 +27,8 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
1927
sensors.append(ATTR_OUTSIDE_TEMPERATURE)
2028
entities = [PanasonicClimateSensor(device, sensor) for sensor in sensors]
2129
if device.energy_sensor_enabled:
22-
entities.append(PanasonicEnergySensor(device))
30+
entities.append(PanasonicEnergySensor(device, ATTR_DAILY_ENERGY))
31+
entities.append(PanasonicEnergySensor(device, ATTR_CURRENT_POWER))
2332
add_entities(entities)
2433

2534
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
@@ -32,7 +41,8 @@ async def async_setup_entry(hass, entry, async_add_entities):
3241
sensors.append(ATTR_OUTSIDE_TEMPERATURE)
3342
entities = [PanasonicClimateSensor(device, sensor) for sensor in sensors]
3443
if device.energy_sensor_enabled:
35-
entities.append(PanasonicEnergySensor(device))
44+
entities.append(PanasonicEnergySensor(device, ATTR_DAILY_ENERGY))
45+
entities.append(PanasonicEnergySensor(device, ATTR_CURRENT_POWER))
3646
async_add_entities(entities)
3747

3848

@@ -87,21 +97,24 @@ def device_info(self):
8797
class PanasonicEnergySensor(Entity):
8898
"""Representation of a Sensor."""
8999

90-
def __init__(self, api) -> None:
100+
def __init__(self, api, monitored_state) -> None:
91101
"""Initialize the sensor."""
92102
self._api = api
93-
94-
self._name = f"{api.name} Daily Energy"
103+
self._sensor = ENERGY_SENSOR_TYPES[monitored_state]
104+
self._name = f"{api.name} {self._sensor[CONF_NAME]}"
105+
self._device_attribute = monitored_state
95106

96107
@property
97108
def unique_id(self):
98109
"""Return a unique ID."""
99-
return f"{self._api.id}-daily_energy_sensor"
110+
if self._device_attribute == ATTR_DAILY_ENERGY:
111+
return f"{self._api.id}-daily_energy_sensor"
112+
return f"{self._api.id}-{self._device_attribute}"
100113

101114
@property
102115
def icon(self):
103116
"""Icon to use in the frontend, if any."""
104-
return "mdi:flash"
117+
return self._sensor[CONF_ICON]
105118

106119
@property
107120
def name(self):
@@ -111,12 +124,16 @@ def name(self):
111124
@property
112125
def state(self):
113126
"""Return the state of the sensor."""
114-
return self._api.daily_energy
127+
if self._device_attribute == ATTR_DAILY_ENERGY:
128+
return self._api.daily_energy
129+
if self._device_attribute == ATTR_CURRENT_POWER:
130+
return self._api.current_power
131+
return None
115132

116133
@property
117134
def unit_of_measurement(self):
118135
"""Return the unit of measurement."""
119-
return "kWh"
136+
return self._sensor[CONF_TYPE]
120137

121138
async def async_update(self):
122139
"""Retrieve latest state."""

doc/entities.png

4.27 KB
Loading

info.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ This is a custom component to allow control of Panasonic Comfort Cloud devices i
1414
* Sensors for inside and outside temperature (where available)
1515
* Switch for toggling Nanoe
1616
* Daily energy sensor (optional)
17+
* Current Power sensor (Calculated from energy reading)
1718

1819

1920
# Configuration

0 commit comments

Comments
 (0)