Skip to content

Commit

Permalink
Fixed updating of the energy sensors.
Browse files Browse the repository at this point in the history
  • Loading branch information
StephanU committed Nov 22, 2023
1 parent a2435bf commit b20d553
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 19 deletions.
3 changes: 2 additions & 1 deletion custom_components/mygekko/const.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"""Constants for MyGekko."""
# Base component constants
NAME = "MyGekko"
MANUFACTURER = "myGEKKO | Ekon GmbH"
DOMAIN = "mygekko"
DOMAIN_DATA = f"{DOMAIN}_data"
VERSION = "0.0.34"
VERSION = "0.0.36"

ATTRIBUTION = "Data provided by http://jsonplaceholder.typicode.com/"
ISSUE_URL = "https://github.com/stephanu/mygekko/issues"
Expand Down
2 changes: 0 additions & 2 deletions custom_components/mygekko/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from .const import DOMAIN
from .const import NAME
from .const import VERSION


class MyGekkoEntity(CoordinatorEntity):
Expand All @@ -21,6 +20,5 @@ def device_info(self):
return {
"identifiers": {(DOMAIN, self.unique_id)},
"name": self.entity.name,
"model": VERSION,
"manufacturer": NAME,
}
24 changes: 18 additions & 6 deletions custom_components/mygekko/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,40 @@

from homeassistant.components.scene import Scene
from homeassistant.core import callback
from homeassistant.helpers.entity import DeviceInfo
from PyMyGekko.resources.Actions import Action
from PyMyGekko.resources.Actions import ActionState

from .const import DOMAIN
from .const import SCENE
from .entity import MyGekkoEntity
from .const import MANUFACTURER


async def async_setup_entry(hass, entry, async_add_devices):
"""Setup scene platform."""
coordinator = hass.data[DOMAIN][entry.entry_id]
actions = coordinator.api.get_actions()
globals_network = coordinator.api.get_globals_network()
if actions is not None:
async_add_devices(MyGekkoScene(coordinator, action) for action in actions)
async_add_devices(MyGekkoScene(action, globals_network) for action in actions)


class MyGekkoScene(MyGekkoEntity, Scene):
class MyGekkoScene(Scene):
"""mygekko Scene class."""

def __init__(self, coordinator, action: Action):
super().__init__(coordinator, action, SCENE)
def __init__(self, action: Action, globals_network):
self._attr_unique_id = "actions_" + action.id
self._attr_name = action.name
self._action = action
self._attr_device_info = DeviceInfo(
identifiers={
(DOMAIN, "mygekko_controller_" + globals_network["gekkoname"])
},
name=globals_network["gekkoname"],
manufacturer=MANUFACTURER,
sw_version=globals_network["version"],
hw_version=globals_network["hardware"],
model=globals_network["hardware"],
)

@callback
def _handle_coordinator_update(self) -> None:
Expand Down
27 changes: 17 additions & 10 deletions custom_components/mygekko/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from PyMyGekko.resources.EnergyCosts import EnergyCost

from .const import DOMAIN
from .const import MANUFACTURER
from .const import NAME

_LOGGER: logging.Logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -120,12 +121,12 @@ async def async_setup_entry(hass, entry, async_add_devices):
if energy_costs is not None:
for energy_cost in energy_costs:
if energy_cost.sensor_data and "values" in energy_cost.sensor_data:
for sensor in energy_cost.sensor_data["values"]:
for index, sensor in enumerate(energy_cost.sensor_data["values"]):
if sensor and "name" in sensor and sensor["name"] in SENSORS:
async_add_devices(
[
MyGekkoEnergySensor(
energy_cost, sensor, SENSORS[sensor["name"]]
energy_cost, index, SENSORS[sensor["name"]]
)
]
)
Expand All @@ -152,9 +153,10 @@ def __init__(self, alarms_logic: AlarmsLogic, globals_network):
(DOMAIN, "mygekko_controller_" + globals_network["gekkoname"])
},
name=globals_network["gekkoname"],
manufacturer=NAME,
manufacturer=MANUFACTURER,
sw_version=globals_network["version"],
hw_version=globals_network["hardware"],
model=globals_network["hardware"],
)

_LOGGER.debug("Added sensor %s %s", alarms_logic.name, self.unique_id)
Expand All @@ -173,15 +175,18 @@ class MyGekkoEnergySensor(SensorEntity):
def __init__(
self,
energy_cost: EnergyCost,
sensor_data,
index,
sensorEntityDescription: SensorEntityDescription,
):
self.entity_description = sensorEntityDescription
self._attr_unique_id = (
"energy_cost_" + energy_cost.id + "_" + sensor_data["name"]
"energy_cost_"
+ energy_cost.id
+ "_"
+ energy_cost.sensor_data["values"][index]["name"]
)
self._energy_cost = energy_cost
self._sensor_data = sensor_data
self._index = index
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, "energy_cost_" + energy_cost.id)},
name=energy_cost.name,
Expand All @@ -190,20 +195,22 @@ def __init__(

_LOGGER.debug(
"Added sensor %s %s %s",
sensor_data["name"],
energy_cost.sensor_data["values"][index]["name"],
self.unique_id,
sensor_data["unit"],
energy_cost.sensor_data["values"][index]["unit"],
)

@property
def state(self):
"""Return the state of the sensor."""
return self._sensor_data["value"]
return self._energy_cost.sensor_data["values"][self._index]["value"]

@property
def native_unit_of_measurement(self) -> str | None:
"""Return the unit of measurement."""
if (unit := self._sensor_data["unit"]) is None or unit == 0:
if (
unit := self._energy_cost.sensor_data["values"][self._index]["unit"]
) is None or unit == 0:
return None

return SENSOR_UNIT_MAPPING[unit]

0 comments on commit b20d553

Please sign in to comment.