Skip to content

Commit

Permalink
Get current entity_id in config
Browse files Browse the repository at this point in the history
  • Loading branch information
Snuffy2 committed Jul 4, 2023
1 parent 202459d commit 371b053
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions custom_components/places/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
DOMAIN = "places"
VERSION = "v2.4"
EVENT_TYPE = DOMAIN + "_state_update"
PLATFORM = Platform.SENSOR
ENTITY_ID_FORMAT = Platform.SENSOR + ".{}"

# Defaults
Expand Down
16 changes: 13 additions & 3 deletions custom_components/places/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from datetime import datetime, timedelta

import homeassistant.helpers.config_validation as cv
import homeassistant.helpers.entity_registry as er
import requests
import voluptuous as vol
from homeassistant import config_entries, core
Expand Down Expand Up @@ -136,6 +137,7 @@
JSON_ATTRIBUTE_LIST,
JSON_IGNORE_ATTRIBUTE_LIST,
PLACE_NAME_DUPLICATE_LIST,
PLATFORM,
RESET_ATTRIBUTE_LIST,
TRACKING_DOMAINS,
TRACKING_DOMAINS_NEED_LATLONG,
Expand Down Expand Up @@ -383,13 +385,21 @@ def __init__(self, hass, config, config_entry, name, unique_id):
self._config = config
self._config_entry = config_entry
self._hass = hass
self.entity_id = generate_entity_id(
ENTITY_ID_FORMAT, slugify(name.lower()), hass=self._hass
)
self.set_attr(CONF_NAME, name)
self._attr_name = name
self.set_attr(CONF_UNIQUE_ID, unique_id)
self._attr_unique_id = unique_id
registry = er.async_get(self._hass)
current_entity_id = registry.async_get_entity_id(
PLATFORM, DOMAIN, self._attr_unique_id
)
if current_entity_id is not None:
self.entity_id = current_entity_id
else:
self.entity_id = generate_entity_id(
ENTITY_ID_FORMAT, slugify(name.lower()), hass=self._hass
)
_LOGGER.debug(f"({self._attr_name}) entity_id: {self.entity_id}")
self.set_attr(CONF_ICON, DEFAULT_ICON)
self._attr_icon = DEFAULT_ICON
self.set_attr(CONF_API_KEY, config.get(CONF_API_KEY))
Expand Down

0 comments on commit 371b053

Please sign in to comment.