Skip to content
This repository has been archived by the owner on Mar 6, 2022. It is now read-only.

Commit

Permalink
Fix current price sensor, change device_class to monetary
Browse files Browse the repository at this point in the history
  • Loading branch information
fbjerggaard committed Jan 12, 2022
1 parent e36f577 commit b7ee3a3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
15 changes: 7 additions & 8 deletions custom_components/barry/pybarry.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ def __init__(
@staticmethod
def hour_rounder(t):
# Rounds to nearest hour by adding a timedelta hour if minute >= 30
return (t.replace(second=0, microsecond=0, minute=0, hour=t.hour)
+ timedelta(hours=t.minute // 30))
return (t.replace(second=0, microsecond=0, minute=0, hour=t.hour))

@staticmethod
def get_currency(data):
Expand Down Expand Up @@ -65,11 +64,11 @@ def get_current_co2_emission(self, price_code):
def get_current_spot_price(self, price_code):
current_time = self.hour_rounder(
datetime.utcnow().replace(microsecond=0)).isoformat() + 'Z'
last_hour_date_time = self.hour_rounder(
(datetime.utcnow() - timedelta(hours=1)).replace(microsecond=0)).isoformat() + 'Z'
next_hour_date_time = self.hour_rounder(
(datetime.utcnow() + timedelta(hours=1)).replace(microsecond=0)).isoformat() + 'Z'

data = '{ "jsonrpc": "2.0", "id": 0, "method": "co.getbarry.api.v1.OpenApiController.getPrice", "params": [ "%s", "%s", "%s" ] }' % (
price_code, last_hour_date_time, current_time)
price_code, current_time, next_hour_date_time)
response = requests.post(
self.endpoint, headers=self.headers, data=data)
json_res = response.json()
Expand All @@ -85,11 +84,11 @@ def get_current_spot_price(self, price_code):
def get_current_total_price(self, mpid):
current_time = self.hour_rounder(
datetime.utcnow().replace(microsecond=0)).isoformat() + 'Z'
last_hour_date_time = self.hour_rounder(
(datetime.utcnow() - timedelta(hours=1)).replace(microsecond=0)).isoformat() + 'Z'
next_hour_date_time = self.hour_rounder(
(datetime.utcnow() + timedelta(hours=1)).replace(microsecond=0)).isoformat() + 'Z'

data = '{ "jsonrpc": "2.0", "id": 0, "method": "co.getbarry.api.v1.OpenApiController.getTotalKwHPrice", "params": [ "%s", "%s", "%s" ] }' % (
mpid, last_hour_date_time, current_time)
mpid, current_time, next_hour_date_time)
response = requests.post(
self.endpoint, headers=self.headers, data=data)
json_res = response.json()
Expand Down
8 changes: 4 additions & 4 deletions custom_components/barry/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,6 @@ def unique_id(self):
def state(self) -> float:
return self.current_total_price

@property
def icon(self) -> str:
return "mdi:flash"

@property
def unit(self) -> str:
return self._price_type
Expand Down Expand Up @@ -159,6 +155,10 @@ def today(self) -> list:
def today(self) -> list:
return self._tomorrow

@property
def device_class(self) -> str:
return "monetary"

def _update_current_price(self) -> None:
_LOGGER.debug("Updating current price")
_LOGGER.debug("barry_home: %s", self._barry_home)
Expand Down

0 comments on commit b7ee3a3

Please sign in to comment.