added battery sensor
This commit is contained in:
1 parent
53fe7d9309
commit
84ef34d01d
5 files changed
+112
-5
No files matched your search
@@ -2,14 +2,18 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import re
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import CONF_ADDRESS, Platform
|
from homeassistant.const import CONF_ADDRESS, Platform
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.helpers.entity import DeviceInfo
|
||||||
|
|
||||||
|
from .const import DOMAIN, MANUFACTURER
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
PLATFORMS: [Platform] = []
|
PLATFORMS: [Platform] = [Platform.SENSOR]
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
@@ -20,5 +24,25 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
if address is None:
|
if address is None:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
# Setup platforms
|
||||||
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||||
|
|
||||||
_LOGGER.error("Device address: %s", address)
|
_LOGGER.error("Device address: %s", address)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def device_info(entry: ConfigEntry) -> DeviceInfo:
|
||||||
|
"""Device info."""
|
||||||
|
return DeviceInfo(
|
||||||
|
identifiers={(DOMAIN, entry.data.get(CONF_ADDRESS))},
|
||||||
|
name=entry.title,
|
||||||
|
manufacturer=MANUFACTURER,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def get_unique_id(name: str, sensor_type: str | None = None):
|
||||||
|
"""Generate an unique id."""
|
||||||
|
res = re.sub("[^A-Za-z0-9]+", "_", name).lower()
|
||||||
|
if sensor_type is not None:
|
||||||
|
return f"{sensor_type}.{res}"
|
||||||
|
return res
|
||||||
@@ -34,12 +34,12 @@ class BluettiConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
await self.async_set_unique_id(discovery_info.address)
|
await self.async_set_unique_id(discovery_info.address)
|
||||||
self._abort_if_unique_id_configured()
|
self._abort_if_unique_id_configured()
|
||||||
self._discovery_info = discovery_info
|
self._discovery_info = discovery_info
|
||||||
self.context["title_placeholders"] = {
|
self.context["title_placeholders"] = {"name": discovery_info.name}
|
||||||
"name": discovery_info.name
|
|
||||||
}
|
|
||||||
return await self.async_step_user()
|
return await self.async_step_user()
|
||||||
|
|
||||||
async def async_step_user(self, user_input: dict[str, Any] | None = None) -> FlowResult:
|
async def async_step_user(
|
||||||
|
self, user_input: dict[str, Any] | None = None
|
||||||
|
) -> FlowResult:
|
||||||
"""Handle user input."""
|
"""Handle user input."""
|
||||||
|
|
||||||
if user_input is not None:
|
if user_input is not None:
|
||||||
@@ -59,6 +59,7 @@ class BluettiConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
else:
|
else:
|
||||||
current_addresses = self._async_current_ids()
|
current_addresses = self._async_current_ids()
|
||||||
for discovery in async_discovered_service_info(self.hass):
|
for discovery in async_discovered_service_info(self.hass):
|
||||||
|
address = discovery.address
|
||||||
if address in current_addresses or address in self._discovered_devices:
|
if address in current_addresses or address in self._discovered_devices:
|
||||||
continue
|
continue
|
||||||
self._discovered_devices[discovery.address] = discovery
|
self._discovered_devices[discovery.address] = discovery
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
"""Constants for the Bluetti BT integration."""
|
"""Constants for the Bluetti BT integration."""
|
||||||
|
|
||||||
DOMAIN = "bluetti_bt"
|
DOMAIN = "bluetti_bt"
|
||||||
|
MANUFACTURER = "Bluetti"
|
||||||
@@ -0,0 +1,80 @@
|
|||||||
|
"""Bluetti BT sensors."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from datetime import timedelta
|
||||||
|
import logging
|
||||||
|
|
||||||
|
import async_timeout
|
||||||
|
|
||||||
|
from homeassistant.components.sensor import SensorEntity
|
||||||
|
from homeassistant.config_entries import ConfigEntry
|
||||||
|
from homeassistant.core import HomeAssistant, callback
|
||||||
|
from homeassistant.const import CONF_ADDRESS
|
||||||
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
from homeassistant.helpers.entity import DeviceInfo
|
||||||
|
from homeassistant.helpers.update_coordinator import (
|
||||||
|
CoordinatorEntity,
|
||||||
|
DataUpdateCoordinator,
|
||||||
|
UpdateFailed,
|
||||||
|
)
|
||||||
|
|
||||||
|
from . import device_info as dev_info, get_unique_id
|
||||||
|
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
async def async_setup_entry(
|
||||||
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||||
|
) -> None:
|
||||||
|
"""Setup sensor entities."""
|
||||||
|
|
||||||
|
# Create coordinator for polling
|
||||||
|
coordinator = PollingCoordinator(hass)
|
||||||
|
await coordinator.async_config_entry_first_refresh()
|
||||||
|
|
||||||
|
# Generate device info
|
||||||
|
_LOGGER.error("Creating sensors for device %s", entry.data.get(CONF_ADDRESS))
|
||||||
|
device_info = dev_info(entry)
|
||||||
|
async_add_entities(Battery(coordinator, device_info))
|
||||||
|
|
||||||
|
|
||||||
|
class PollingCoordinator(DataUpdateCoordinator):
|
||||||
|
"""Polling coordinator."""
|
||||||
|
|
||||||
|
def __init__(self, hass):
|
||||||
|
"""Initialize coordinator."""
|
||||||
|
super().__init__(
|
||||||
|
hass,
|
||||||
|
_LOGGER,
|
||||||
|
name="Bluetti polling coordinator",
|
||||||
|
update_interval=timedelta(seconds=10),
|
||||||
|
)
|
||||||
|
|
||||||
|
async def _async_update_data(self):
|
||||||
|
"""Fetch data from API endpoint.
|
||||||
|
|
||||||
|
This is the place to pre-process the data to lookup tables
|
||||||
|
so entities can quickly look up their data.
|
||||||
|
"""
|
||||||
|
self.logger.error("Polling data")
|
||||||
|
|
||||||
|
|
||||||
|
class Battery(CoordinatorEntity, SensorEntity):
|
||||||
|
"""Bluetti battery."""
|
||||||
|
|
||||||
|
def __init__(self, coordinator: PollingCoordinator, device_info: DeviceInfo):
|
||||||
|
"""Init battery entity."""
|
||||||
|
super().__init__(coordinator)
|
||||||
|
|
||||||
|
self._attr_device_info = device_info
|
||||||
|
self._attr_name = f"{device_info.get('name')} Battery level"
|
||||||
|
self._attr_unique_id = get_unique_id(f"{device_info.get('name')} Battery level")
|
||||||
|
self._attr_unit_of_measurement = "%"
|
||||||
|
self._attr_device_class = "battery"
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def _handle_coordinator_update(self) -> None:
|
||||||
|
"""Handle updated data from the coordinator."""
|
||||||
|
_LOGGER.error("Updating state of %s", self._attr_unique_id)
|
||||||
|
self.async_write_ha_state()
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
robocopy .\custom_components\bluetti_bt T:\custom_components\bluetti_bt /E
|
||||||
Reference in new issue
Block a user