Updated amberelectic attributes to reflect unit change to $/kWh (#57109)

This commit is contained in:
Lawrence 2021-10-06 14:34:23 +11:00 committed by GitHub
parent d51d70d3be
commit 205b40cf16
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 31 deletions

View File

@ -37,6 +37,11 @@ ICONS = {
UNIT = f"{CURRENCY_DOLLAR}/{ENERGY_KILO_WATT_HOUR}" UNIT = f"{CURRENCY_DOLLAR}/{ENERGY_KILO_WATT_HOUR}"
def format_cents_to_dollars(cents: float) -> float:
"""Return a formatted conversion from cents to dollars."""
return round(cents / 100, 2)
def friendly_channel_type(channel_type: str) -> str: def friendly_channel_type(channel_type: str) -> str:
"""Return a human readable version of the channel type.""" """Return a human readable version of the channel type."""
if channel_type == "controlled_load": if channel_type == "controlled_load":
@ -70,13 +75,13 @@ class AmberPriceSensor(AmberSensor):
"""Amber Price Sensor.""" """Amber Price Sensor."""
@property @property
def native_value(self) -> str | None: def native_value(self) -> float | None:
"""Return the current price in $/kWh.""" """Return the current price in $/kWh."""
interval = self.coordinator.data[self.entity_description.key][self.channel_type] interval = self.coordinator.data[self.entity_description.key][self.channel_type]
if interval.channel_type == ChannelType.FEED_IN: if interval.channel_type == ChannelType.FEED_IN:
return round(interval.per_kwh, 0) / 100 * -1 return format_cents_to_dollars(interval.per_kwh) * -1
return round(interval.per_kwh, 0) / 100 return format_cents_to_dollars(interval.per_kwh)
@property @property
def device_state_attributes(self) -> Mapping[str, Any] | None: def device_state_attributes(self) -> Mapping[str, Any] | None:
@ -89,11 +94,11 @@ class AmberPriceSensor(AmberSensor):
data["duration"] = interval.duration data["duration"] = interval.duration
data["date"] = interval.date.isoformat() data["date"] = interval.date.isoformat()
data["per_kwh"] = round(interval.per_kwh) data["per_kwh"] = format_cents_to_dollars(interval.per_kwh)
if interval.channel_type == ChannelType.FEED_IN: if interval.channel_type == ChannelType.FEED_IN:
data["per_kwh"] = data["per_kwh"] * -1 data["per_kwh"] = data["per_kwh"] * -1
data["nem_date"] = interval.nem_time.isoformat() data["nem_date"] = interval.nem_time.isoformat()
data["spot_per_kwh"] = round(interval.spot_per_kwh) data["spot_per_kwh"] = format_cents_to_dollars(interval.spot_per_kwh)
data["start_time"] = interval.start_time.isoformat() data["start_time"] = interval.start_time.isoformat()
data["end_time"] = interval.end_time.isoformat() data["end_time"] = interval.end_time.isoformat()
data["renewables"] = round(interval.renewables) data["renewables"] = round(interval.renewables)
@ -102,8 +107,8 @@ class AmberPriceSensor(AmberSensor):
data["channel_type"] = interval.channel_type.value data["channel_type"] = interval.channel_type.value
if interval.range is not None: if interval.range is not None:
data["range_min"] = interval.range.min data["range_min"] = format_cents_to_dollars(interval.range.min)
data["range_max"] = interval.range.max data["range_max"] = format_cents_to_dollars(interval.range.max)
return data return data
@ -112,7 +117,7 @@ class AmberForecastSensor(AmberSensor):
"""Amber Forecast Sensor.""" """Amber Forecast Sensor."""
@property @property
def native_value(self) -> str | None: def native_value(self) -> float | None:
"""Return the first forecast price in $/kWh.""" """Return the first forecast price in $/kWh."""
intervals = self.coordinator.data[self.entity_description.key].get( intervals = self.coordinator.data[self.entity_description.key].get(
self.channel_type self.channel_type
@ -122,8 +127,8 @@ class AmberForecastSensor(AmberSensor):
interval = intervals[0] interval = intervals[0]
if interval.channel_type == ChannelType.FEED_IN: if interval.channel_type == ChannelType.FEED_IN:
return round(interval.per_kwh, 0) / 100 * -1 return format_cents_to_dollars(interval.per_kwh) * -1
return round(interval.per_kwh, 0) / 100 return format_cents_to_dollars(interval.per_kwh)
@property @property
def device_state_attributes(self) -> Mapping[str, Any] | None: def device_state_attributes(self) -> Mapping[str, Any] | None:
@ -146,18 +151,18 @@ class AmberForecastSensor(AmberSensor):
datum["duration"] = interval.duration datum["duration"] = interval.duration
datum["date"] = interval.date.isoformat() datum["date"] = interval.date.isoformat()
datum["nem_date"] = interval.nem_time.isoformat() datum["nem_date"] = interval.nem_time.isoformat()
datum["per_kwh"] = round(interval.per_kwh) datum["per_kwh"] = format_cents_to_dollars(interval.per_kwh)
if interval.channel_type == ChannelType.FEED_IN: if interval.channel_type == ChannelType.FEED_IN:
datum["per_kwh"] = datum["per_kwh"] * -1 datum["per_kwh"] = datum["per_kwh"] * -1
datum["spot_per_kwh"] = round(interval.spot_per_kwh) datum["spot_per_kwh"] = format_cents_to_dollars(interval.spot_per_kwh)
datum["start_time"] = interval.start_time.isoformat() datum["start_time"] = interval.start_time.isoformat()
datum["end_time"] = interval.end_time.isoformat() datum["end_time"] = interval.end_time.isoformat()
datum["renewables"] = round(interval.renewables) datum["renewables"] = round(interval.renewables)
datum["spike_status"] = interval.spike_status.value datum["spike_status"] = interval.spike_status.value
if interval.range is not None: if interval.range is not None:
datum["range_min"] = interval.range.min datum["range_min"] = format_cents_to_dollars(interval.range.min)
datum["range_max"] = interval.range.max datum["range_max"] = format_cents_to_dollars(interval.range.max)
data["forecasts"].append(datum) data["forecasts"].append(datum)

View File

@ -108,9 +108,9 @@ async def test_general_price_sensor(hass: HomeAssistant, setup_general: Mock) ->
attributes = price.attributes attributes = price.attributes
assert attributes["duration"] == 30 assert attributes["duration"] == 30
assert attributes["date"] == "2021-09-21" assert attributes["date"] == "2021-09-21"
assert attributes["per_kwh"] == 8 assert attributes["per_kwh"] == 0.08
assert attributes["nem_date"] == "2021-09-21T08:30:00+10:00" assert attributes["nem_date"] == "2021-09-21T08:30:00+10:00"
assert attributes["spot_per_kwh"] == 1 assert attributes["spot_per_kwh"] == 0.01
assert attributes["start_time"] == "2021-09-21T08:00:00+10:00" assert attributes["start_time"] == "2021-09-21T08:00:00+10:00"
assert attributes["end_time"] == "2021-09-21T08:30:00+10:00" assert attributes["end_time"] == "2021-09-21T08:30:00+10:00"
assert attributes["renewables"] == 51 assert attributes["renewables"] == 51
@ -132,8 +132,8 @@ async def test_general_price_sensor(hass: HomeAssistant, setup_general: Mock) ->
price = hass.states.get("sensor.mock_title_general_price") price = hass.states.get("sensor.mock_title_general_price")
assert price assert price
attributes = price.attributes attributes = price.attributes
assert attributes.get("range_min") == 7.8 assert attributes.get("range_min") == 0.08
assert attributes.get("range_max") == 12.4 assert attributes.get("range_max") == 0.12
async def test_general_and_controlled_load_price_sensor( async def test_general_and_controlled_load_price_sensor(
@ -141,16 +141,15 @@ async def test_general_and_controlled_load_price_sensor(
) -> None: ) -> None:
"""Test the Controlled Price sensor.""" """Test the Controlled Price sensor."""
assert len(hass.states.async_all()) == 6 assert len(hass.states.async_all()) == 6
print(hass.states)
price = hass.states.get("sensor.mock_title_controlled_load_price") price = hass.states.get("sensor.mock_title_controlled_load_price")
assert price assert price
assert price.state == "0.08" assert price.state == "0.08"
attributes = price.attributes attributes = price.attributes
assert attributes["duration"] == 30 assert attributes["duration"] == 30
assert attributes["date"] == "2021-09-21" assert attributes["date"] == "2021-09-21"
assert attributes["per_kwh"] == 8 assert attributes["per_kwh"] == 0.08
assert attributes["nem_date"] == "2021-09-21T08:30:00+10:00" assert attributes["nem_date"] == "2021-09-21T08:30:00+10:00"
assert attributes["spot_per_kwh"] == 1 assert attributes["spot_per_kwh"] == 0.01
assert attributes["start_time"] == "2021-09-21T08:00:00+10:00" assert attributes["start_time"] == "2021-09-21T08:00:00+10:00"
assert attributes["end_time"] == "2021-09-21T08:30:00+10:00" assert attributes["end_time"] == "2021-09-21T08:30:00+10:00"
assert attributes["renewables"] == 51 assert attributes["renewables"] == 51
@ -172,9 +171,9 @@ async def test_general_and_feed_in_price_sensor(
attributes = price.attributes attributes = price.attributes
assert attributes["duration"] == 30 assert attributes["duration"] == 30
assert attributes["date"] == "2021-09-21" assert attributes["date"] == "2021-09-21"
assert attributes["per_kwh"] == -8 assert attributes["per_kwh"] == -0.08
assert attributes["nem_date"] == "2021-09-21T08:30:00+10:00" assert attributes["nem_date"] == "2021-09-21T08:30:00+10:00"
assert attributes["spot_per_kwh"] == 1 assert attributes["spot_per_kwh"] == 0.01
assert attributes["start_time"] == "2021-09-21T08:00:00+10:00" assert attributes["start_time"] == "2021-09-21T08:00:00+10:00"
assert attributes["end_time"] == "2021-09-21T08:30:00+10:00" assert attributes["end_time"] == "2021-09-21T08:30:00+10:00"
assert attributes["renewables"] == 51 assert attributes["renewables"] == 51
@ -199,9 +198,9 @@ async def test_general_forecast_sensor(
first_forecast = attributes["forecasts"][0] first_forecast = attributes["forecasts"][0]
assert first_forecast["duration"] == 30 assert first_forecast["duration"] == 30
assert first_forecast["date"] == "2021-09-21" assert first_forecast["date"] == "2021-09-21"
assert first_forecast["per_kwh"] == 9 assert first_forecast["per_kwh"] == 0.09
assert first_forecast["nem_date"] == "2021-09-21T09:00:00+10:00" assert first_forecast["nem_date"] == "2021-09-21T09:00:00+10:00"
assert first_forecast["spot_per_kwh"] == 1 assert first_forecast["spot_per_kwh"] == 0.01
assert first_forecast["start_time"] == "2021-09-21T08:30:00+10:00" assert first_forecast["start_time"] == "2021-09-21T08:30:00+10:00"
assert first_forecast["end_time"] == "2021-09-21T09:00:00+10:00" assert first_forecast["end_time"] == "2021-09-21T09:00:00+10:00"
assert first_forecast["renewables"] == 50 assert first_forecast["renewables"] == 50
@ -222,8 +221,8 @@ async def test_general_forecast_sensor(
assert price assert price
attributes = price.attributes attributes = price.attributes
first_forecast = attributes["forecasts"][0] first_forecast = attributes["forecasts"][0]
assert first_forecast.get("range_min") == 7.8 assert first_forecast.get("range_min") == 0.08
assert first_forecast.get("range_max") == 12.4 assert first_forecast.get("range_max") == 0.12
async def test_controlled_load_forecast_sensor( async def test_controlled_load_forecast_sensor(
@ -241,9 +240,9 @@ async def test_controlled_load_forecast_sensor(
first_forecast = attributes["forecasts"][0] first_forecast = attributes["forecasts"][0]
assert first_forecast["duration"] == 30 assert first_forecast["duration"] == 30
assert first_forecast["date"] == "2021-09-21" assert first_forecast["date"] == "2021-09-21"
assert first_forecast["per_kwh"] == 9 assert first_forecast["per_kwh"] == 0.09
assert first_forecast["nem_date"] == "2021-09-21T09:00:00+10:00" assert first_forecast["nem_date"] == "2021-09-21T09:00:00+10:00"
assert first_forecast["spot_per_kwh"] == 1 assert first_forecast["spot_per_kwh"] == 0.01
assert first_forecast["start_time"] == "2021-09-21T08:30:00+10:00" assert first_forecast["start_time"] == "2021-09-21T08:30:00+10:00"
assert first_forecast["end_time"] == "2021-09-21T09:00:00+10:00" assert first_forecast["end_time"] == "2021-09-21T09:00:00+10:00"
assert first_forecast["renewables"] == 50 assert first_forecast["renewables"] == 50
@ -265,9 +264,9 @@ async def test_feed_in_forecast_sensor(
first_forecast = attributes["forecasts"][0] first_forecast = attributes["forecasts"][0]
assert first_forecast["duration"] == 30 assert first_forecast["duration"] == 30
assert first_forecast["date"] == "2021-09-21" assert first_forecast["date"] == "2021-09-21"
assert first_forecast["per_kwh"] == -9 assert first_forecast["per_kwh"] == -0.09
assert first_forecast["nem_date"] == "2021-09-21T09:00:00+10:00" assert first_forecast["nem_date"] == "2021-09-21T09:00:00+10:00"
assert first_forecast["spot_per_kwh"] == 1 assert first_forecast["spot_per_kwh"] == 0.01
assert first_forecast["start_time"] == "2021-09-21T08:30:00+10:00" assert first_forecast["start_time"] == "2021-09-21T08:30:00+10:00"
assert first_forecast["end_time"] == "2021-09-21T09:00:00+10:00" assert first_forecast["end_time"] == "2021-09-21T09:00:00+10:00"
assert first_forecast["renewables"] == 50 assert first_forecast["renewables"] == 50