Skip to content

Commit

Permalink
Merge pull request #19 from HarvsG/patch-1
Browse files Browse the repository at this point in the history
Support `unique_id:`
  • Loading branch information
lolouk44 authored Jan 4, 2023
2 parents 9af75a1 + 59472a7 commit b9550d0
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [0.2.4] - 2022-10-12
### Changed
- Now supports `unique_id:` as a configuration option.

## [0.2.3] - 2022-09-18
### Changed
- Gracefully handle when unable to read serial port ([fixes #17](https://github.com/lolouk44/CurrentCost_HA_CC/issues/17))
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ sensor:
- platform: currentcost
serial_port: /dev/serial/by-id/usb-Prolific_Technology_Inc._USB-Serial_Controller-if00-port0
name: Current Cost
unique_id: abaaa250-fd59-46e1-abd8-07545fb2b297
baudrate: 57600
devices:
- 0
Expand Down
4 changes: 2 additions & 2 deletions custom_components/currentcost/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"domain": "currentcost",
"name": "Current Cost",
"version": "0.2.3",
"version": "0.2.4",
"documentation": "https://github.com/lolouk44/CurrentCost_HA_CC",
"requirements": [
"pyserial-asyncio==0.4",
Expand All @@ -12,4 +12,4 @@
"codeowners": [
"@lolouk44"
]
}
}
9 changes: 6 additions & 3 deletions custom_components/currentcost/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import voluptuous as vol

from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity, STATE_CLASS_MEASUREMENT
from homeassistant.const import CONF_NAME, CONF_DEVICES, EVENT_HOMEASSISTANT_STOP, POWER_WATT, DEVICE_CLASS_POWER
from homeassistant.const import CONF_NAME, CONF_UNIQUE_ID, CONF_DEVICES, EVENT_HOMEASSISTANT_STOP, POWER_WATT, DEVICE_CLASS_POWER
import homeassistant.helpers.config_validation as cv

_LOGGER = logging.getLogger(__name__)
Expand All @@ -27,6 +27,7 @@
vol.Required(CONF_SERIAL_PORT): cv.string,
vol.Optional(CONF_BAUDRATE, default=DEFAULT_BAUDRATE): cv.positive_int,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_UNIQUE_ID): cv.string,
vol.Optional(CONF_DEVICES, default=DEFAULT_DEVICES): vol.All(cv.ensure_list, [vol.Range(min=0, max=9)]),
}
)
Expand All @@ -35,12 +36,13 @@
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the Current Cost sensor platform."""
name = config.get(CONF_NAME)
unique_id = config.get(CONF_UNIQUE_ID)
port = config.get(CONF_SERIAL_PORT)
baudrate = config.get(CONF_BAUDRATE)
devices = config.get(CONF_DEVICES)
_LOGGER.debug("devices: %s", config.get(CONF_DEVICES))
#sensor = []
sensor = CurrentCostSensor(name, port, baudrate, devices)
sensor = CurrentCostSensor(name, unique_id, port, baudrate, devices)
#for variable in devices:
# sensor.append(CurrentCostSensor(f"{name}_appliance_{variable}", port, baudrate))
#sensor.append(CurrentCostSensor(f"{name}_temperature", port, baudrate))
Expand All @@ -52,9 +54,10 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
class CurrentCostSensor(SensorEntity):
"""Representation of a Current Cost sensor."""

def __init__(self, name, port, baudrate, devices):
def __init__(self, name, unique_id, port, baudrate, devices):
"""Initialize the Current Cost sensor."""
self._name = name
self._attr_unique_id = None if unique_id is None else f"current-cost-{unique_id}"
self._unit = POWER_WATT
self._icon = "mdi:flash-circle"
self._device_class = DEVICE_CLASS_POWER
Expand Down

0 comments on commit b9550d0

Please sign in to comment.