1 """Support for EufyLife sensors."""
3 from __future__
import annotations
7 from eufylife_ble_client
import MODEL_TO_NAME
9 from homeassistant
import config_entries
23 from .const
import DOMAIN
24 from .models
import EufyLifeData
26 IGNORED_STATES = {STATE_UNAVAILABLE, STATE_UNKNOWN}
32 async_add_entities: AddEntitiesCallback,
34 """Set up the EufyLife sensors."""
35 data: EufyLifeData = hass.data[DOMAIN][entry.entry_id]
42 if data.client.supports_heart_rate:
49 """Representation of an EufyLife sensor."""
51 _attr_has_entity_name =
True
53 def __init__(self, data: EufyLifeData) ->
None:
54 """Initialize the weight sensor entity."""
58 name=MODEL_TO_NAME[data.model],
59 connections={(dr.CONNECTION_BLUETOOTH, data.address)},
64 """Determine if the entity is available."""
65 if self.
_data_data.client.advertisement_data_contains_state:
70 return self.
_data_data.client.is_connected
74 """Handle state update."""
78 """Register callback."""
85 """Representation of an EufyLife real-time weight sensor."""
87 _attr_translation_key =
"real_time_weight"
88 _attr_native_unit_of_measurement = UnitOfMass.KILOGRAMS
89 _attr_device_class = SensorDeviceClass.WEIGHT
91 def __init__(self, data: EufyLifeData) ->
None:
92 """Initialize the real-time weight sensor entity."""
98 """Return the native value."""
99 if self.
_data_data.client.state
is not None:
100 return self.
_data_data.client.state.weight_kg
105 """Set the suggested unit based on the unit system."""
106 if self.
hasshass.config.units
is US_CUSTOMARY_SYSTEM:
107 return UnitOfMass.POUNDS
109 return UnitOfMass.KILOGRAMS
113 """Representation of an EufyLife weight sensor."""
115 _attr_translation_key =
"weight"
116 _attr_native_unit_of_measurement = UnitOfMass.KILOGRAMS
117 _attr_device_class = SensorDeviceClass.WEIGHT
120 """Initialize the weight sensor entity."""
126 """Determine if the entity is available."""
131 """Set the suggested unit based on the unit system."""
132 if self.
hasshass.config.units
is US_CUSTOMARY_SYSTEM:
133 return UnitOfMass.POUNDS
135 return UnitOfMass.KILOGRAMS
139 """Handle state update."""
140 state = self.
_data_data.client.state
141 if state
is not None and state.final_weight_kg
is not None:
147 """Restore state on startup."""
150 last_state = await self.async_get_last_state()
153 if not last_state
or not last_sensor_data
or last_state.state
in IGNORED_STATES:
160 """Representation of an EufyLife heart rate sensor."""
162 _attr_translation_key =
"heart_rate"
163 _attr_native_unit_of_measurement =
"bpm"
166 """Initialize the heart rate sensor entity."""
172 """Determine if the entity is available."""
177 """Handle state update."""
178 state = self.
_data_data.client.state
179 if state
is not None and state.heart_rate
is not None:
185 """Restore state on startup."""
188 last_state = await self.async_get_last_state()
191 if not last_state
or not last_sensor_data
or last_state.state
in IGNORED_STATES:
None async_added_to_hass(self)
None _handle_state_update(self, *Any args)
None __init__(self, EufyLifeData data)
str|None suggested_unit_of_measurement(self)
None __init__(self, EufyLifeData data)
float|None native_value(self)
None _handle_state_update(self, *Any args)
None async_added_to_hass(self)
None __init__(self, EufyLifeData data)
None __init__(self, EufyLifeData data)
str|None suggested_unit_of_measurement(self)
None async_added_to_hass(self)
None _handle_state_update(self, *Any args)
SensorExtraStoredData|None async_get_last_sensor_data(self)
None async_write_ha_state(self)
None async_on_remove(self, CALLBACK_TYPE func)
bool async_address_present(HomeAssistant hass, str address, bool connectable=True)
None async_setup_entry(HomeAssistant hass, config_entries.ConfigEntry entry, AddEntitiesCallback async_add_entities)