From 8302c7fe51d61cfa1d2787f8ddb0700e800526eb Mon Sep 17 00:00:00 2001 From: lolouk44 Date: Wed, 4 Jan 2023 20:14:02 +0000 Subject: [PATCH] 0.2.4 --- CHANGELOG.md | 7 +++++-- README.md | 1 - custom_components/currentcost/sensor.py | 17 +++++++++-------- 3 files changed, 14 insertions(+), 11 deletions(-) mode change 100644 => 100755 CHANGELOG.md mode change 100644 => 100755 README.md mode change 100644 => 100755 custom_components/currentcost/sensor.py diff --git a/CHANGELOG.md b/CHANGELOG.md old mode 100644 new mode 100755 index fc971cd..3b00afb --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ -## [0.2.4] - 2022-10-12 +## [0.2.4] - 2023-01-14 +### Added +- Support for unique_id to help sensor customisation ### Changed -- Now supports `unique_id:` as a configuration option. +- Replaced deprecated DEVICE_CLASS_* Constants ([fixes #20](https://github.com/lolouk44/CurrentCost_HA_CC/issues/20)) +- Fixed default icon (flash-circle -> lightning-bolt-circle) ## [0.2.3] - 2022-09-18 ### Changed diff --git a/README.md b/README.md old mode 100644 new mode 100755 index 7af2d7d..e8dfab4 --- a/README.md +++ b/README.md @@ -44,7 +44,6 @@ 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 diff --git a/custom_components/currentcost/sensor.py b/custom_components/currentcost/sensor.py old mode 100644 new mode 100755 index d1edf86..9d9934f --- a/custom_components/currentcost/sensor.py +++ b/custom_components/currentcost/sensor.py @@ -7,8 +7,8 @@ import serial_asyncio import voluptuous as vol -from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity, STATE_CLASS_MEASUREMENT -from homeassistant.const import CONF_NAME, CONF_UNIQUE_ID, CONF_DEVICES, EVENT_HOMEASSISTANT_STOP, POWER_WATT, DEVICE_CLASS_POWER +from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity, SensorDeviceClass, SensorStateClass +from homeassistant.const import CONF_NAME, CONF_UNIQUE_ID, CONF_DEVICES, EVENT_HOMEASSISTANT_STOP, POWER_WATT import homeassistant.helpers.config_validation as cv _LOGGER = logging.getLogger(__name__) @@ -19,6 +19,7 @@ CONF_DEVICES = "devices" DEFAULT_NAME = "Current Cost" +DEFAULT_ID = "abaaa250-fd59-46e1-abd8-07545fb2b297" DEFAULT_BAUDRATE = 57600 DEFAULT_DEVICES = [0,1,2,3,4,5,6,7,8,9] @@ -27,7 +28,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_UNIQUE_ID, default=DEFAULT_ID): cv.string, vol.Optional(CONF_DEVICES, default=DEFAULT_DEVICES): vol.All(cv.ensure_list, [vol.Range(min=0, max=9)]), } ) @@ -42,7 +43,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info= devices = config.get(CONF_DEVICES) _LOGGER.debug("devices: %s", config.get(CONF_DEVICES)) #sensor = [] - sensor = CurrentCostSensor(name, unique_id, port, baudrate, devices) + sensor = CurrentCostSensor(name, f"current-cost-{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)) @@ -57,11 +58,11 @@ class CurrentCostSensor(SensorEntity): 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._attr_unique_id = unique_id self._unit = POWER_WATT - self._icon = "mdi:flash-circle" - self._device_class = DEVICE_CLASS_POWER - self._state_class = STATE_CLASS_MEASUREMENT + self._icon = "mdi:flash-outline" + self._device_class = SensorDeviceClass.POWER + self._state_class = SensorStateClass.MEASUREMENT self._state = None self._port = port self._baudrate = baudrate