Skip to content
This repository has been archived by the owner on May 4, 2024. It is now read-only.

Summation received fix #10

Merged
merged 4 commits into from
Apr 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ Integrate the [Rainforest EMU-2](https://www.rainforestautomation.com/rfa-z105-2

1. Select ```Configure | Dashboards | Energy``` to enter configuration mode
1. Choose ```ADD CONSUMPTION```
1. For ```Consumed Energy (kWh)``` select the Rainforest ```Current Perdiod Usage``` sensor
1. For ```Consumed Energy (kWh)``` select the Rainforest ```Summation Delivered``` sensor
1. Under cost, select the entity option and choose the Rainforest ```Current Price``` sensor
> This option depends on the device containing the pricing information. Chose one of the other options if the device is not loaded with pricing information.
1. If you have solar, then repeat steps 2-4 for the ```Summation Received``` sensor
1. Select ```SAVE```

# Result
Expand Down
3 changes: 2 additions & 1 deletion custom_components/rainforest_emu_2/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ async def get_device_properties(self, device_path: str) -> dict[str, str]:
return None

serial_loop_task = self.hass.loop.create_task(emu2.serial_read())
await emu2.wait_connected(5)
if await emu2.wait_connected(8) == False:
_LOGGER.debug("Failed to receive data from device")

await emu2.get_device_info()

Expand Down
9 changes: 4 additions & 5 deletions custom_components/rainforest_emu_2/emu2.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,9 @@ async def open(self) -> bool:
)
except SerialException as ex:
_LOGGER.error(ex)
self._connected = False
else:
self._connected = True

return self._connected
return False

return True

async def serial_read(self):
_LOGGER.info("Starting serial_read loop")
Expand All @@ -97,6 +95,7 @@ async def serial_read(self):
response += line
if line.startswith('</'):
try:
self._connected = True
self._process_reply(response)
response = ''
except Exception as ex:
Expand Down
2 changes: 1 addition & 1 deletion custom_components/rainforest_emu_2/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"pyserial-asyncio==0.6"
],
"iot_class": "local_polling",
"version": "1.1.4",
"version": "1.1.5",
"config_flow": true,
"usb": [
{
Expand Down
44 changes: 27 additions & 17 deletions custom_components/rainforest_emu_2/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,25 @@

from homeassistant.core import callback
from homeassistant.components.sensor import (
SensorEntity,
SensorStateClass,
SensorDeviceClass
SensorEntity,
SensorStateClass,
SensorDeviceClass,
)
from homeassistant.const import (
ATTR_IDENTIFIERS,
ATTR_NAME,
ATTR_IDENTIFIERS,
ATTR_NAME,
ATTR_MANUFACTURER,
ATTR_MODEL,
ATTR_HW_VERSION,
ATTR_SW_VERSION,
ENERGY_KILO_WATT_HOUR,
POWER_KILO_WATT,
CURRENCY_DOLLAR
CURRENCY_DOLLAR,
)

from .const import DOMAIN, DEVICE_NAME


async def async_setup_entry(hass, config_entry, async_add_entities):
device = hass.data[DOMAIN][config_entry.entry_id]

Expand All @@ -29,10 +30,11 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
Emu2CurrentPriceSensor(device),
Emu2CurrentPeriodUsageSensor(device),
Emu2SummationDeliveredSensor(device),
Emu2SummationReceivedSensor(device)
Emu2SummationReceivedSensor(device),
]
async_add_entities(entities)


class SensorEntityBase(SensorEntity):
should_poll = True

Expand All @@ -48,7 +50,7 @@ def device_info(self):
ATTR_MANUFACTURER: self._device.device_manufacturer,
ATTR_MODEL: self._device.device_model,
ATTR_HW_VERSION: self._device.device_hw_version,
ATTR_SW_VERSION: self._device.device_sw_version
ATTR_SW_VERSION: self._device.device_sw_version,
}

@property
Expand All @@ -61,11 +63,12 @@ async def async_added_to_hass(self):
async def async_will_remove_from_hass(self):
self._device.remove_callback(self._observe, self.async_write_ha_state)


class Emu2ActivePowerSensor(SensorEntityBase):
should_poll = False

def __init__(self, device):
super().__init__(device, 'InstantaneousDemand')
super().__init__(device, "InstantaneousDemand")

self._attr_unique_id = f"{self._device.device_id}_power"
self._attr_name = f"{self._device.device_name} Power"
Expand All @@ -78,16 +81,19 @@ def __init__(self, device):
def state(self):
return self._device.power


class Emu2CurrentPriceSensor(SensorEntityBase):
def __init__(self, device):
super().__init__(device, 'PriceCluster')
super().__init__(device, "PriceCluster")

self._attr_unique_id = f"{self._device.device_id}_current_price"
self._attr_name = f"{self._device.device_name} Current Price"

self._attr_device_class = SensorDeviceClass.MONETARY
self._attr_state_class = SensorStateClass.MEASUREMENT
self._attr_native_unit_of_measurement = f"{CURRENCY_DOLLAR}/{ENERGY_KILO_WATT_HOUR}"
self._attr_native_unit_of_measurement = (
f"{CURRENCY_DOLLAR}/{ENERGY_KILO_WATT_HOUR}"
)

async def async_update(self):
await self._device._emu2.get_current_price()
Expand All @@ -96,9 +102,10 @@ async def async_update(self):
def state(self):
return self._device.current_price


class Emu2CurrentPeriodUsageSensor(SensorEntityBase):
def __init__(self, device):
super().__init__(device, 'CurrentPeriodUsage')
super().__init__(device, "CurrentPeriodUsage")

self._attr_unique_id = f"{self._device.device_id}_current_period_usage"
self._attr_name = f"{self._device.device_name} Current Period Usage"
Expand All @@ -117,12 +124,13 @@ def state(self):
@property
def last_reset(self):
return self._device.current_usage_start_date



class Emu2SummationDeliveredSensor(SensorEntityBase):
should_poll = False

def __init__(self, device):
super().__init__(device, 'CurrentSummationDelivered')
super().__init__(device, "CurrentSummationDelivered")

self._attr_unique_id = f"{self._device.device_id}_summation_delivered"
self._attr_name = f"{self._device.device_name} Summation Delivered"
Expand All @@ -135,13 +143,15 @@ def __init__(self, device):
def state(self):
return self._device.summation_delivered


class Emu2SummationReceivedSensor(SensorEntityBase):
should_poll = False

def __init__(self, device):
super().__init__(device, 'CurrentSummationReceived')
# The received information is part of the Summation Delivered XML packet
super().__init__(device, "CurrentSummationDelivered")

self._attr_unique_id = f"{self._device.device_id}_summation_recieved"
self._attr_unique_id = f"{self._device.device_id}_summation_received"
self._attr_name = f"{self._device.device_name} Summation Received"

self._attr_device_class = SensorDeviceClass.ENERGY
Expand Down