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 all 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
9 changes: 9 additions & 0 deletions homeassistant/components/sense/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity

Expand Down Expand Up @@ -66,6 +67,14 @@ def __init__(
self._attr_unique_id = f"{sense_monitor_id}-{self._id}"
self._attr_icon = sense_to_mdi(device.icon)
self._device = device
self._attr_device_info = DeviceInfo(
name=f"Sense {sense_monitor_id} - {device.name}",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should name this device the device name as that is user recognisable. They can view to which monitor it belongs via the device info page

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also because i think it's a good idea that we migrate to has entity name = True, where all entity names are appended to the device name

identifiers={(DOMAIN, f"{sense_monitor_id}:{device.id}")},
model="Sense",
manufacturer="Sense Labs, Inc.",
configuration_url="https://home.sense.com",
via_device=(DOMAIN, f"{sense_monitor_id}"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
via_device=(DOMAIN, f"{sense_monitor_id}"),
via_device=(DOMAIN, sense_monitor_id),

)

@property
def old_unique_id(self) -> str:
Expand Down
15 changes: 15 additions & 0 deletions homeassistant/components/sense/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,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,6 +283,14 @@ 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",
via_device=(DOMAIN, f"{sense_monitor_id}"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
via_device=(DOMAIN, f"{sense_monitor_id}"),
via_device=(DOMAIN, sense_monitor_id),

)

@property
def native_value(self) -> float:
Expand Down
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"
13 changes: 1 addition & 12 deletions tests/components/sense/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from homeassistant.util.dt import utcnow

from . import setup_platform
from .const import DEVICE_1_NAME, DEVICE_1_POWER, DEVICE_2_NAME, DEVICE_2_POWER
from .const import DEVICE_1_NAME, DEVICE_2_NAME, DEVICE_2_POWER

from tests.common import MockConfigEntry, async_fire_time_changed, snapshot_platform

Expand Down Expand Up @@ -52,17 +52,6 @@ async def test_device_power_sensors(
state = hass.states.get(f"sensor.{DEVICE_2_NAME.lower()}_{CONSUMPTION_ID}")
assert state.state == "0"

device_1.power_w = DEVICE_1_POWER
async_fire_time_changed(hass, utcnow() + timedelta(seconds=ACTIVE_UPDATE_RATE))
await hass.async_block_till_done()

state = hass.states.get(f"sensor.{DEVICE_1_NAME.lower()}_{CONSUMPTION_ID}")
assert state.state == f"{DEVICE_1_POWER:.1f}"

state = hass.states.get(f"sensor.{DEVICE_2_NAME.lower()}_{CONSUMPTION_ID}")
assert state.state == "0"

device_1.power_w = 0
device_2.power_w = DEVICE_2_POWER
async_fire_time_changed(hass, utcnow() + timedelta(seconds=ACTIVE_UPDATE_RATE))
await hass.async_block_till_done()
Expand Down