Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Sense Devices for entities #129182

Draft
wants to merge 25 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
84ccdce
Update sense-energy to 0.13.2 and library compatiblity changes
kbickar Oct 13, 2024
5a10755
Add trend sensors and devices for sense energy devices
kbickar Oct 13, 2024
6111d01
Merge branch 'dev' into sense-trend
kbickar Oct 14, 2024
a6f3ec0
Create tests for sense integration
kbickar Oct 15, 2024
6211b81
Merge branch 'dev' into sense-tests
kbickar Oct 15, 2024
79dc228
Merge branch 'sense-tests' into sense-trend
kbickar Oct 15, 2024
dbbf929
Rearrange files
kbickar Oct 16, 2024
6d12f36
Update to use snapshots
kbickar Oct 16, 2024
b057683
Merge branch 'dev' into sense-tests
kbickar Oct 16, 2024
e64e8ca
Merge branch 'sense-tests' into sense-trend
kbickar Oct 17, 2024
d42805d
Updates Tests
kbickar Oct 17, 2024
43ef50f
Merge branch 'sense-trend' into sense-devices
kbickar Oct 18, 2024
6462b9e
Update Tests
kbickar Oct 18, 2024
2f60c7a
Merge branch 'dev' into sense-trend
kbickar Oct 23, 2024
c4c086f
Add typing for sense component
kbickar Oct 25, 2024
804ac79
Merge branch 'sense-type' into sense-trend
kbickar Oct 25, 2024
864fbff
Merge branch 'dev' into sense-trend
kbickar Oct 25, 2024
74b2f90
Convert Sense realtime to use Coordinator
kbickar Oct 25, 2024
f97670d
Merge branch 'dev' into sense-rt-coord
kbickar Oct 25, 2024
890c935
Merge branch 'sense-rt-coord' into sense-devices
kbickar Oct 25, 2024
007e5d1
Add custom coordinators
kbickar Oct 25, 2024
94c77c2
Merge branch 'sense-rt-coord' into sense-devices
kbickar Oct 25, 2024
416800b
Merge coordinator update
kbickar Oct 25, 2024
81b4d68
Merge branch 'dev' into sense-devices
kbickar Oct 25, 2024
ac4c3d0
Remove added sensors
kbickar Oct 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion homeassistant/components/sense/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: SenseConfigEntry) -> boo
)

await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

return True


Expand Down
67 changes: 63 additions & 4 deletions homeassistant/components/sense/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,16 @@ async def async_setup_entry(

sense_monitor_id = data.sense_monitor_id

entities: list[SensorEntity] = [
SenseDevicePowerSensor(device, sense_monitor_id, realtime_coordinator)
for device in config_entry.runtime_data.data.devices
]
entities: list[SensorEntity] = []

for device in config_entry.runtime_data.data.devices:
entities.append(
SenseDevicePowerSensor(device, sense_monitor_id, realtime_coordinator)
)
entities += [
SenseDeviceEnergySensor(device, scale, trends_coordinator, sense_monitor_id)
for scale in Scale
]

for variant_id, variant_name in SENSOR_VARIANTS:
entities.append(
Expand Down Expand Up @@ -195,6 +201,13 @@ def __init__(
self._attr_name = f"L{index + 1} Voltage"
self._data = data
self._voltage_index = index
self._attr_device_info = DeviceInfo(
name=f"Sense {sense_monitor_id}",
identifiers={(DOMAIN, sense_monitor_id)},
model="Sense",
manufacturer="Sense Labs, Inc.",
configuration_url="https://home.sense.com",
)

@property
def native_value(self) -> float:
Expand Down Expand Up @@ -276,8 +289,54 @@ def __init__(
self._id = device.id
self._attr_icon = sense_to_mdi(device.icon)
self._device = device
self._attr_device_info = DeviceInfo(
kbickar marked this conversation as resolved.
Show resolved Hide resolved
name=f"Sense {sense_monitor_id} - {device.name}",
identifiers={(DOMAIN, f"{sense_monitor_id}:{device.id}")},
model="Sense",
manufacturer="Sense Labs, Inc.",
configuration_url="https://home.sense.com",
)

@property
def native_value(self) -> float:
"""Return the state of the sensor."""
return self._device.power_w


class SenseDeviceEnergySensor(SenseBaseSensor):
"""Implementation of a Sense device energy sensor."""

_attr_native_unit_of_measurement = UnitOfEnergy.KILO_WATT_HOUR
_attr_state_class = SensorStateClass.TOTAL_INCREASING
_attr_device_class = SensorDeviceClass.ENERGY

def __init__(
self,
device: SenseDevice,
scale: Scale,
trends_coordinator: SenseTrendCoordinator,
sense_monitor_id: str,
) -> None:
"""Initialize the Sense sensor."""
super().__init__(
trends_coordinator,
sense_monitor_id,
f"{device.id}-{TRENDS_SENSOR_TYPES[scale].lower()}",
)
self._attr_name = f"{device.name} {TRENDS_SENSOR_TYPES[scale]} Energy"
self._scale = scale
self._had_any_update = False
self._attr_icon = sense_to_mdi(device.icon)
self._device = device
self._attr_device_info = DeviceInfo(
name=f"Sense - {device.name}",
identifiers={(DOMAIN, f"{sense_monitor_id}:{device.id}")},
model="Sense",
manufacturer="Sense Labs, Inc.",
configuration_url="https://home.sense.com",
)

@property
def native_value(self) -> float:
"""Return the state of the sensor."""
return round(self._device.energy_kwh[self._scale], 2)
5 changes: 5 additions & 0 deletions tests/components/sense/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@
from unittest.mock import AsyncMock, MagicMock, PropertyMock, patch

import pytest
from sense_energy import Scale

from homeassistant.components.sense.binary_sensor import SenseDevice
from homeassistant.components.sense.const import DOMAIN

from .const import (
DEVICE_1_DAY_ENERGY,
DEVICE_1_ID,
DEVICE_1_NAME,
DEVICE_1_POWER,
DEVICE_2_DAY_ENERGY,
DEVICE_2_ID,
DEVICE_2_NAME,
DEVICE_2_POWER,
Expand Down Expand Up @@ -68,12 +71,14 @@ def mock_sense() -> Generator[MagicMock]:
device_1.icon = "car"
device_1.is_on = False
device_1.power_w = DEVICE_1_POWER
device_1.energy_kwh[Scale.DAY] = DEVICE_1_DAY_ENERGY

device_2 = SenseDevice(DEVICE_2_ID)
device_2.name = DEVICE_2_NAME
device_2.icon = "stove"
device_2.is_on = False
device_2.power_w = DEVICE_2_POWER
device_2.energy_kwh[Scale.DAY] = DEVICE_2_DAY_ENERGY
type(gateway).devices = PropertyMock(return_value=[device_1, device_2])

yield gateway
2 changes: 2 additions & 0 deletions tests/components/sense/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
DEVICE_1_ID = "abc123"
DEVICE_1_ICON = "car-electric"
DEVICE_1_POWER = 100.0
DEVICE_1_DAY_ENERGY = 500

DEVICE_2_NAME = "Oven"
DEVICE_2_ID = "def456"
DEVICE_2_ICON = "stove"
DEVICE_2_POWER = 50.0
DEVICE_2_DAY_ENERGY = 42

MONITOR_ID = "12345"
Loading