Skip to content

Commit

Permalink
Added optional homekit_compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
linsvensson committed Dec 19, 2022
1 parent 39d0b54 commit 2ec4079
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ sensor:
sold_measure: 6
time_format: '%H:%M'
date_format: '%d/%m/%Y'
homekit_compatible: true
```
### Configuration variables
Expand All @@ -57,7 +58,7 @@ key | type | description
**platform (Required)** | string | `greenely`
**email (Required)** | string | Your Greenely username.
**password (Required)** | string | Your Greenely password.
**facility_id (Optional)** | string | If you have more than one facility and know the facility ID you want data from, put it here. Default `primary`.
**facility_id (Optional)** | string | If you have more than one facility and know the facility ID you want data from, put it here. Note: The facility id's can be found eg. by installing Android Studio and in a Play Store enabled device emulator (indicated by the Play Icon) install the Greenely app. Run app, login with your Greenely credentials, and use Logcat in Android Studio, filtered on Greenely to find the facility ID. In the app, switch between the registered facilities to find out the different facility IDs. Or, if you use Postman, import [this](https://www.dropbox.com/s/jmoo24tikbre4ep/greenely%20test.postman_collection.json?dl=0) and send login and then facilities. Default `primary`.
**daily_usage (Optional)** | boolean | Creates a sensor showing daily usage data. The state of this sensor is yesterday's total usage. Default `true`.
**hourly_usage (Optional)** | boolean | Creates a sensor showing yesterday's hourly usage data. Default `false`.
**sold (Optional)** | boolean | Creates a sensor showing sold electricity data. The state of this sensor is the total value. Default `false`.
Expand All @@ -68,6 +69,7 @@ key | type | description
**sold_daily (Optional)** | boolean | Show daily sold data instead of monthly. Default `false`.
**date_format (Optional)** | string | Default `%b %d %Y`, shows up as `Jan 18 2020`. [References](https://strftime.org/)
**time_format (Optional)** | string | Default `%H:%M`, shows up as `10:00`. [References](https://strftime.org/)
**homekit_compatible (Optional)** | boolean | If you're using Homekit and need the current price data in the format `x.x °C`, enable this. Default `false`.

## Lovelace
**Example chart with [ApexCharts Card](https://github.com/RomRider/apexcharts-card):**
Expand Down
3 changes: 2 additions & 1 deletion custom_components/greenely/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
CONF_DATE_FORMAT = 'date_format'
CONF_TIME_FORMAT = 'time_format'
CONF_HOURLY_OFFSET_DAYS = 'hourly_offset_days'
CONF_FACILITY_ID = 'facility_id'
CONF_FACILITY_ID = 'facility_id'
CONF_HOMEKIT_COMPATIBLE = 'homekit_compatible'
2 changes: 1 addition & 1 deletion custom_components/greenely/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"domain": "greenely",
"name": "Greenely Sensors",
"version": "1.0.5",
"version": "1.0.6",
"documentation": "https://github.com/linsvensson/sensor.greenely",
"issue_tracker": "https://github.com/linsvensson/sensor.greenely/issues",
"codeowners": ["@linsvensson"],
Expand Down
20 changes: 15 additions & 5 deletions custom_components/greenely/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
SENSOR_SOLD_NAME, SENSOR_PRICES_NAME, CONF_HOURLY_USAGE,
CONF_DAILY_USAGE, CONF_SOLD, CONF_PRICES, CONF_DATE_FORMAT,
CONF_TIME_FORMAT, CONF_USAGE_DAYS, CONF_SOLD_MEASURE,
CONF_SOLD_DAILY, CONF_HOURLY_OFFSET_DAYS, CONF_FACILITY_ID)
CONF_SOLD_DAILY, CONF_HOURLY_OFFSET_DAYS, CONF_FACILITY_ID, CONF_HOMEKIT_COMPATIBLE)

NAME = DOMAIN
ISSUEURL = "https://github.com/linsvensson/sensor.greenely/issues"
Expand Down Expand Up @@ -59,6 +59,8 @@
cv.positive_int,
vol.Optional(CONF_FACILITY_ID, default='primary'):
cv.string,
vol.Optional(CONF_HOMEKIT_COMPATIBLE, default=False):
cv.boolean,
})

SCAN_INTERVAL = timedelta(minutes=60)
Expand All @@ -82,6 +84,7 @@ async def async_setup_platform(hass,
sold_daily = config.get(CONF_SOLD_DAILY)
hourly_offset_days = config.get(CONF_HOURLY_OFFSET_DAYS)
facility_id = config.get(CONF_FACILITY_ID)
homekit_compatible = config.get(CONF_HOMEKIT_COMPATIBLE)

api = GreenelyApi(email, password, facility_id)

Expand All @@ -104,20 +107,21 @@ async def async_setup_platform(hass,
if show_prices:
sensors.append(
GreenelyPricesSensor(SENSOR_PRICES_NAME, api, date_format,
time_format))
time_format, homekit_compatible))
async_add_entities(sensors, True)


class GreenelyPricesSensor(Entity):

def __init__(self, name, api, date_format, time_format):
def __init__(self, name, api, date_format, time_format, homekit_compatible):
self._name = name
self._icon = "mdi:account-cash"
self._state = 0
self._state_attributes = {}
self._unit_of_measurement = 'kr'
self._unit_of_measurement = 'kr' if homekit_compatible != True else '°C'
self._date_format = date_format
self._time_format = time_format
self._homekit_compatible = homekit_compatible
self._api = api

@property
Expand Down Expand Up @@ -198,14 +202,20 @@ def make_attribute(self, response, value):
newPoint['date'] = dt_object.strftime(self._date_format)
newPoint['time'] = dt_object.strftime(self._time_format)
if price != None:
rounded = round(((price / 1000) / 100), 4)
rounded = self.format_price(price)
newPoint['price'] = rounded
if dt_object.hour == today.hour and dt_object.day == today.day:
self._state = rounded
else:
newPoint['price'] = 0
return newPoint

def format_price(self, price):
if self._homekit_compatible == True:
return round(price / 1000)
else:
return round(((price / 1000) / 100), 4)

def make_data_attribute(self, name, response, nameOfPriceAttr):
if response:
points = response.get('points', None)
Expand Down

0 comments on commit 2ec4079

Please sign in to comment.