diff --git a/changelog b/changelog index 6798528..d0cd17b 100644 --- a/changelog +++ b/changelog @@ -1,3 +1,8 @@ +0.9.0b17: + Switch device tracker to GPS if needed + Fix warning about POWER_VOLT_AMPERE_REACTIVE + Fix restore state of lights + Add GPS accuracy 0.9.0b16: Use async interfaces where possible. Add github stale action. diff --git a/custom_components/virtual/__init__.py b/custom_components/virtual/__init__.py index 97b62bc..368a21f 100644 --- a/custom_components/virtual/__init__.py +++ b/custom_components/virtual/__init__.py @@ -30,7 +30,7 @@ from .cfg import BlendedCfg, UpgradeCfg -__version__ = '0.9.0b16' +__version__ = '0.9.0b17' _LOGGER = logging.getLogger(__name__) diff --git a/custom_components/virtual/device_tracker.py b/custom_components/virtual/device_tracker.py index e024285..9d0c10a 100644 --- a/custom_components/virtual/device_tracker.py +++ b/custom_components/virtual/device_tracker.py @@ -38,6 +38,7 @@ CONF_LOCATION = 'location' CONF_GPS = 'gps' +CONF_GPS_ACCURACY = 'gps_accuracy' DEFAULT_DEVICE_TRACKER_VALUE = 'home' DEFAULT_LOCATION = 'home' @@ -59,6 +60,7 @@ vol.Required(ATTR_LONGITUDE): cv.longitude, vol.Optional(ATTR_RADIUS): cv.string, }, + vol.Optional(CONF_GPS_ACCURACY): cv.positive_int, }) tracker_states = {} @@ -190,6 +192,7 @@ def __init__(self, config): self._location = None self._coords = {} + self._gps_accuracy = 0 _LOGGER.debug(f"{self._attr_name}, available={self._attr_available}") _LOGGER.debug(f"{self._attr_name}, entity={self.entity_id}") @@ -227,6 +230,8 @@ def location_name(self) -> str | None: @property def source_type(self) -> SourceType | str: + if self._coords: + return "gps" return "virtual" @property @@ -239,16 +244,21 @@ def longitude(self) -> float | None: """Return longitude value of the device.""" return self._coords.get(ATTR_LONGITUDE, None) + @property + def location_accuracy(self) -> int: + return self._gps_accuracy + def move_to_location(self, new_location): _LOGGER.debug(f"{self._attr_name} moving to {new_location}") self._location = new_location self._coords = {} self.async_schedule_update_ha_state() - def move_to_coords(self, new_coords): - _LOGGER.debug(f"{self._attr_name} moving via GPS to {new_coords}") + def move_to_coords(self, new_coords, accuracy): + _LOGGER.debug(f"{self._attr_name} moving via GPS to {new_coords} ({accuracy}m)") self._location = None self._coords = new_coords + self._gps_accuracy = accuracy self.async_schedule_update_ha_state() @@ -266,7 +276,8 @@ async def async_virtual_move_service(hass, call): if location is not None: entity.move_to_location(location) elif coords is not None: - entity.move_to_coords(coords) + accuracy = call.data.get(CONF_GPS_ACCURACY, 0) + entity.move_to_coords(coords, accuracy) else: _LOGGER.debug(f"not moving {entity_id}") diff --git a/custom_components/virtual/light.py b/custom_components/virtual/light.py index 5ca5efb..5970ad1 100644 --- a/custom_components/virtual/light.py +++ b/custom_components/virtual/light.py @@ -159,15 +159,13 @@ def _restore_state(self, state, config): self._attr_is_on = state.state.lower() == STATE_ON - if ColorMode.BRIGHTNESS in self._attr_supported_color_modes: - self._attr_color_mode = ColorMode.BRIGHTNESS + self._attr_color_mode = state.attributes.get(ATTR_COLOR_MODE, ColorMode.ONOFF) + if self._attr_color_mode == ColorMode.BRIGHTNESS: self._attr_brightness = state.attributes.get(ATTR_BRIGHTNESS, config.get(CONF_INITIAL_BRIGHTNESS)) - if ColorMode.HS in self._attr_supported_color_modes: - self._attr_color_mode = ColorMode.HS + if self._attr_color_mode == ColorMode.HS: self._attr_hs_color = state.attributes.get(ATTR_HS_COLOR, config.get(CONF_INITIAL_COLOR)) self._attr_brightness = state.attributes.get(ATTR_BRIGHTNESS, config.get(CONF_INITIAL_BRIGHTNESS)) - if ColorMode.COLOR_TEMP in self._attr_supported_color_modes: - self._attr_color_mode = ColorMode.COLOR_TEMP + if self._attr_color_mode == ColorMode.COLOR_TEMP: self._attr_color_temp = state.attributes.get(ATTR_COLOR_TEMP, config.get(CONF_INITIAL_COLOR_TEMP)) self._attr_brightness = state.attributes.get(ATTR_BRIGHTNESS, config.get(CONF_INITIAL_BRIGHTNESS)) if self._attr_supported_features & SUPPORT_EFFECT: @@ -191,19 +189,27 @@ async def async_turn_on(self, **kwargs: Any) -> None: """Turn the light on.""" _LOGGER.debug(f"turning {self.name} on {pprint.pformat(kwargs)}") hs_color = kwargs.get(ATTR_HS_COLOR, None) + if hs_color is not None and ColorMode.HS in self._attr_supported_color_modes: + self._attr_color_mode = ColorMode.HS self._attr_hs_color = hs_color self._attr_color_temp = None ct = kwargs.get(ATTR_COLOR_TEMP, None) if ct is not None and ColorMode.COLOR_TEMP in self._attr_supported_color_modes: + self._attr_color_mode = ColorMode.COLOR_TEMP self._attr_color_temp = ct self._attr_hs_color = None brightness = kwargs.get(ATTR_BRIGHTNESS, None) if brightness is not None: + if self._attr_color_mode == ColorMode.UNKNOWN: + self._attr_color_mode = ColorMode.BRIGHTNESS self._attr_brightness = brightness + if self._attr_color_mode == ColorMode.UNKNOWN: + self._attr_color_mode = ColorMode.ONOFF + effect = kwargs.get(ATTR_EFFECT, None) if effect is not None and self._attr_supported_features & SUPPORT_EFFECT: self._attr_effect = effect diff --git a/custom_components/virtual/manifest.json b/custom_components/virtual/manifest.json index f4c37c1..d031830 100644 --- a/custom_components/virtual/manifest.json +++ b/custom_components/virtual/manifest.json @@ -8,5 +8,5 @@ "documentation": "https://github.com/twrecked/hass-virtual/blob/master/README.md", "iot_class": "local_push", "issue_tracker": "https://github.com/twrecked/hass-virtual/issues", - "version": "0.9.0b16" + "version": "0.9.0b17" } diff --git a/custom_components/virtual/sensor.py b/custom_components/virtual/sensor.py index 38f824b..bf3db37 100644 --- a/custom_components/virtual/sensor.py +++ b/custom_components/virtual/sensor.py @@ -22,7 +22,6 @@ CONF_UNIT_OF_MEASUREMENT, LIGHT_LUX, PERCENTAGE, - POWER_VOLT_AMPERE_REACTIVE, SIGNAL_STRENGTH_DECIBELS, UnitOfApparentPower, UnitOfElectricCurrent, @@ -31,6 +30,7 @@ UnitOfFrequency, UnitOfPower, UnitOfPressure, + UnitOfReactivePower, UnitOfVolume, ) from homeassistant.core import HomeAssistant @@ -88,7 +88,7 @@ SensorDeviceClass.ENERGY: UnitOfEnergy.KILO_WATT_HOUR, # energy (Wh/kWh/MWh) SensorDeviceClass.FREQUENCY: UnitOfFrequency.GIGAHERTZ, # energy (Hz/kHz/MHz/GHz) SensorDeviceClass.POWER_FACTOR: PERCENTAGE, # power factor (no unit, min: -1.0, max: 1.0) - SensorDeviceClass.REACTIVE_POWER: POWER_VOLT_AMPERE_REACTIVE, # reactive power (var) + SensorDeviceClass.REACTIVE_POWER: UnitOfReactivePower.VOLT_AMPERE_REACTIVE, # reactive power (var) SensorDeviceClass.VOLATILE_ORGANIC_COMPOUNDS: CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, # µg/m³ of vocs SensorDeviceClass.VOLTAGE: UnitOfElectricPotential.VOLT, # voltage (V) SensorDeviceClass.GAS: UnitOfVolume.CUBIC_METERS, # gas (m³) diff --git a/custom_components/virtual/services.yaml b/custom_components/virtual/services.yaml index a4110a8..c1b3488 100644 --- a/custom_components/virtual/services.yaml +++ b/custom_components/virtual/services.yaml @@ -76,3 +76,9 @@ move: selector: location: radius: false + gps_accuracy: + selector: + number: + min: 0 + mode: box + unit_of_measurement: "m" diff --git a/custom_components/virtual/translations/en.json b/custom_components/virtual/translations/en.json index 7dc69c3..1a43d93 100644 --- a/custom_components/virtual/translations/en.json +++ b/custom_components/virtual/translations/en.json @@ -82,6 +82,10 @@ "gps": { "name": "GPS", "description": "Which coordinates to move the device tracker to." + }, + "gps_accuracy": { + "name": "GPS accuracy", + "description": "Accuracy of the GPS coordinates." } } }