Skip to content

Commit

Permalink
Merge pull request #11 from Lyr3x/fix_entity_id_setup
Browse files Browse the repository at this point in the history
Fix entity id setup
  • Loading branch information
Lyr3x authored Apr 29, 2023
2 parents 6e26fa1 + 12f7038 commit 916b04f
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 29 deletions.
11 changes: 3 additions & 8 deletions custom_components/duet3d_printer/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@
from homeassistant.core import HomeAssistant
from . import DuetDataUpdateCoordinator

from homeassistant.const import (
CONF_NAME,
)

from .const import DOMAIN, BINARY_SENSOR_TYPES, SENSOR_TYPES, PRINTER_STATUS
from .const import DOMAIN, SENSOR_TYPES

_LOGGER = logging.getLogger(__name__)

Expand All @@ -23,15 +19,14 @@ async def async_setup_entry(
async_add_entities: AddEntitiesCallback,
):
"""Set up the available Duet3D binary sensors."""
name = config_entry.data[CONF_NAME]
coordinator: DuetDataUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id][
"coordinator"
]
device_id = config_entry.entry_id
assert device_id is not None

entities: list[BinarySensorEntity] = [
DuetPrintingSensor(coordinator, f"{name} Printing", device_id),
DuetPrintingSensor(coordinator, "Printing", device_id),
]
async_add_entities(entities)

Expand All @@ -50,7 +45,7 @@ def __init__(
"""Initialize a new Duet3D sensor."""
super().__init__(coordinator)
self._device_id = device_id
self._attr_name = f"{sensor_name}"
self._attr_name = f"{self.device_info['name']} {sensor_name}"
self._attr_unique_id = device_id

@property
Expand Down
6 changes: 0 additions & 6 deletions custom_components/duet3d_printer/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
DOMAIN = "duet3d_printer"

DEFAULT_NAME = "Duet3D"
ATTR_NAME = ""
CONF_NAME = "name"
CONF_NUMBER_OF_TOOLS = "number_of_tools"
CONF_BED = "bed"
Expand Down Expand Up @@ -62,11 +61,6 @@
},
}

BINARY_SENSOR_TYPES = {
# API Endpoint, Group, Key, unit
"Printing": ["state", "state.status", "status", None, None],
}

PRINTER_STATUS = {
"starting",
"simulating",
Expand Down
19 changes: 10 additions & 9 deletions custom_components/duet3d_printer/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,30 @@ async def async_setup_entry(
async_add_entities: AddEntitiesCallback,
):
"""Set up the Duet3D light platform."""
name = config_entry.data[CONF_NAME]
lightIncluded = config_entry.data[CONF_LIGHT]
if lightIncluded:
coordinator: DuetDataUpdateCoordinator = hass.data[DOMAIN][
config_entry.entry_id
]["coordinator"]
device_id = config_entry.entry_id
assert device_id is not None
async_add_entities([Duet3DLight(coordinator, device_id, f"{name} LED")], True)
entities: list[LightEntity] = [
Duet3DLight(coordinator, "LED", device_id),
]
async_add_entities(entities)


class Duet3DLightBase(CoordinatorEntity[DuetDataUpdateCoordinator], LightEntity):
"""Representation of a light connected to a Duet3D printer."""

def __init__(
self, coordinator: DuetDataUpdateCoordinator, device_id: str, name
self, coordinator: DuetDataUpdateCoordinator, light_name: str, device_id: str
) -> None:
"""Initialize the light."""
super().__init__(coordinator)
self._device_id = device_id
self._attr_name = f"Duet3D {name}"
self._attr_unique_id = f"{name}-{device_id}"
self._attr_name = f"{self.device_info['name']} {light_name}"
self._attr_unique_id = device_id

@property
def device_info(self):
Expand All @@ -66,10 +68,9 @@ def device_info(self):

class Duet3DLight(Duet3DLightBase):
def __init__(
self, coordinator: DuetDataUpdateCoordinator, device_id: str, name: str
self, coordinator: DuetDataUpdateCoordinator, name: str, device_id: str
) -> None:
super().__init__(coordinator, device_id, name)
self._name = name
super().__init__(coordinator, name, f"{name}-{device_id}")
self._state = False
self._brightness = 255
self._rgb_color = (255, 255, 255)
Expand All @@ -78,7 +79,7 @@ def __init__(
@property
def name(self):
"""Return the name of the light."""
return self._name
return self._attr_name

@property
def should_poll(self):
Expand Down
7 changes: 1 addition & 6 deletions custom_components/duet3d_printer/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,10 @@
NOTIFICATION_ID = "duet3d_notification"
NOTIFICATION_TITLE = "Duet3d sensor setup error"

from homeassistant.const import (
CONF_NAME,
)
from .const import (
DOMAIN,
SENSOR_TYPES,
PRINTER_STATUS,
ATTR_NAME,
)


Expand All @@ -36,7 +32,6 @@ async def async_setup_entry(
async_add_entities: AddEntitiesCallback,
):
"""Set up the available Duet3D sensors."""
ATTR_NAME = config_entry.data[CONF_NAME]
coordinator: DuetDataUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id][
"coordinator"
]
Expand Down Expand Up @@ -124,7 +119,7 @@ def __init__(
"""Initialize a new Duet3D sensor."""
super().__init__(coordinator)
self._device_id = device_id
self._attr_name = f"{ATTR_NAME} {sensor_name}"
self._attr_name = f"{self.device_info['name']} {sensor_name}"
self._attr_unique_id = device_id

@property
Expand Down

0 comments on commit 916b04f

Please sign in to comment.