Skip to content

Commit

Permalink
Try new remove sensor logic after restart
Browse files Browse the repository at this point in the history
  • Loading branch information
MelleD committed Sep 7, 2024
1 parent 9ebaa0b commit 34e7dda
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 37 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@ jobs:
runs-on: "ubuntu-latest"
steps:
- uses: "actions/checkout@v4"
- uses: home-assistant/actions/hassfest@master
- uses: home-assistant/actions/hassfest@master
- uses: hacs/action@main
with:
category: "integration"


2 changes: 1 addition & 1 deletion custom_components/radar_warnings/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ async def async_step_user(
CONF_SHOW_ON_MAP, default=True
): cv.boolean,
vol.Optional(
CONF_API_KEY, default=None
CONF_API_KEY
): cv.string
}
),
Expand Down
2 changes: 1 addition & 1 deletion custom_components/radar_warnings/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ async def async_config_entry_first_refresh(self) -> None:
self._radius = self.config_entry.data.get(CONF_RADIUS, 10.0)
self.config_name = self.config_entry.data.get(CONF_NAME)
self.show_map = self.config_entry.data.get(CONF_SHOW_ON_MAP)
self.google_api_key = self.config_entry.data.get(CONF_API_KEY)
self.google_api_key = self.config_entry.data.get(CONF_API_KEY, None)
self.update_interval = timedelta(minutes=self.config_entry.data.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL))
session = async_get_clientsession(self.hass)
self.api = RadarWarningApi(self._latitude, self._longitude, self._radius, session, self.google_api_key)
Expand Down
36 changes: 24 additions & 12 deletions custom_components/radar_warnings/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,32 +80,44 @@ def _init_regular_updates(self) -> None:

def _remove_entity(self) -> None:
device_reg = dr.async_get(self._hass)
LOGGER.warn("Remove device1:")
device1 = device_reg.async_get_device("zoo_1")
self._hass.add_job(device1.async_remove())
device2 = device_reg.async_get_device("zoo_2")
self._hass.add_job(device2.async_remove())
LOGGER.warn("Removed devices:")
max_iterations=1000
for i in range(max_iterations):
unique_id_radar = f"{self._unique_id}_{i}"
device = device_reg.async_get_device({(DOMAIN, unique_id_radar)})
LOGGER.warn("Found device: %d", device)
for i in range(1,max_iterations):
unique_id_radar = self._radar_map_name(i)
LOGGER.warn("Remove device: %s", unique_id_radar)
device = device_reg.async_get_device(unique_id_radar)
LOGGER.warn("Remove Found device: %s", device)
if device is None:
return
self._hass.add_job(device.async_remove())

def _update(self) -> None:
async def _update(self) -> None:
"""Update Map entry."""
LOGGER.warn("Update Map entry, devices to update: %d", len(self._managed_devices))
self._remove_entity()

for device in list(self._managed_devices):
LOGGER.warn("Remove device")
self._managed_devices.remove(device)
self._hass.add_job(device.async_remove())
#for device in list(self._managed_devices):
# LOGGER.warn("Remove device 2")
# self._managed_devices.remove(device)
# self._hass.add_job(device.async_remove())

pois = self._coordinator.api.pois
for i, poi in enumerate(pois, 1):
unique_id_radar = f"{self._unique_id}_{i}"
unique_id_radar = self._radar_map_name(i)
LOGGER.warn("New device: %s", unique_id_radar)
new_device = RadarMapWarningsSensor(unique_id_radar,poi[API_ATTR_WARNING_DISTANCE],poi[ATTR_LATITUDE],poi[ATTR_LONGITUDE])
self._managed_devices.append(new_device)
self._add_entities(self._managed_devices)
await self._add_entities(self._managed_devices)
LOGGER.warn("Added New device")

def _radar_map_name(self, count: int) -> str:
"""Radar Map Sensor name."""
return f"{DOMAIN}_{self._unique_id}_{count}"


class RadarWarningsSensor(
Expand Down Expand Up @@ -168,7 +180,7 @@ def __init__(
longitude: float
) -> None:
"""Initialize entity with data provided."""
self._attr_name = f"{DOMAIN}_{name}"
self._attr_name = name
self._attr_icon = "mdi:cctv"
self._distance = distance
self._latitude = latitude
Expand Down
4 changes: 2 additions & 2 deletions custom_components/radar_warnings/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"radius": "[%key:common::config_flow::data::radius%]",
"name": "[%key:common::config_flow::data::name%]",
"api_key": "[%key:common::config_flow::data::api_key%]",
"scan_interval": "[%key:common::config_flow::data::scan%]",
"show_on_map": "[%key:common::config_flow::data::map%]"
"scan_interval": "[%key:common::config_flow::data::scan_interval%]",
"show_on_map": "[%key:common::config_flow::data::show_on_map%]"
}
}
}
Expand Down
33 changes: 15 additions & 18 deletions custom_components/radar_warnings/translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,29 @@
"longitude": "Längengrad",
"name": "Name",
"radius": "Radius (km)",
"scan": "Scan intervall (minutes)",
"map": "Anzeigen POIs in Map"
"scan_interval": "Scan intervall (minutes)",
"show_on_map": "Anzeigen POIs in Map"
},
"description": "Gib die Daten deiner Zone ein. Der optionale Api Key wird für die google GeoLocation Api verwendet"
}
}
},
"entity": {
"sensor": {
"current_warning_level": {
"name": "Current warning level",
"state_attributes": {
"region_name": {
"name": "Region name"
},
"region_id": {
"name": "Region ID"
},
"last_update": {
"name": "Last update"
},
"warning_count": {
"name": "Warning count"
}
"sensor": {
"radar_warnings": {
"name": "Anzahl Radar Warnungen",
"state_attributes": {
"last_update": {
"name": "Letzte Aktualisierung"
},
"warning_count": {
"name": "Anzahl Warnungen"
},
"warnings": {
"name": "Warnungen"
}
}
}
}
}
}
4 changes: 2 additions & 2 deletions custom_components/radar_warnings/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"longitude": "Longitude",
"name": "Name",
"radius": "Radius (km)",
"scan": "Scan intervall (minutes)",
"map": "Show POIs in map"
"scan_interval": "Scan intervall (minutes)",
"show_on_map": "Show POIs in map"
},
"description": "Fill in the data of your zone. The optional Api Key is used for the google GeoLocation Api"
}
Expand Down

0 comments on commit 34e7dda

Please sign in to comment.