Skip to content

Commit

Permalink
Multiple fixes (#135)
Browse files Browse the repository at this point in the history
* Remove deprecated constant.

* Make device tracker GPS if given coords.

* Fix restore on lights.

* Bump the revision...

* Add GPS accuracy.

* Tidy up PR.

* Tidy up PR.
  • Loading branch information
twrecked authored Sep 30, 2024
1 parent 0f35b57 commit 498e374
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 13 deletions.
5 changes: 5 additions & 0 deletions changelog
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion custom_components/virtual/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from .cfg import BlendedCfg, UpgradeCfg


__version__ = '0.9.0b16'
__version__ = '0.9.0b17'

_LOGGER = logging.getLogger(__name__)

Expand Down
17 changes: 14 additions & 3 deletions custom_components/virtual/device_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

CONF_LOCATION = 'location'
CONF_GPS = 'gps'
CONF_GPS_ACCURACY = 'gps_accuracy'
DEFAULT_DEVICE_TRACKER_VALUE = 'home'
DEFAULT_LOCATION = 'home'

Expand All @@ -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 = {}
Expand Down Expand Up @@ -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}")
Expand Down Expand Up @@ -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
Expand All @@ -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()


Expand All @@ -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}")

18 changes: 12 additions & 6 deletions custom_components/virtual/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion custom_components/virtual/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
4 changes: 2 additions & 2 deletions custom_components/virtual/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
CONF_UNIT_OF_MEASUREMENT,
LIGHT_LUX,
PERCENTAGE,
POWER_VOLT_AMPERE_REACTIVE,
SIGNAL_STRENGTH_DECIBELS,
UnitOfApparentPower,
UnitOfElectricCurrent,
Expand All @@ -31,6 +30,7 @@
UnitOfFrequency,
UnitOfPower,
UnitOfPressure,
UnitOfReactivePower,
UnitOfVolume,
)
from homeassistant.core import HomeAssistant
Expand Down Expand Up @@ -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³)
Expand Down
6 changes: 6 additions & 0 deletions custom_components/virtual/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,9 @@ move:
selector:
location:
radius: false
gps_accuracy:
selector:
number:
min: 0
mode: box
unit_of_measurement: "m"
4 changes: 4 additions & 0 deletions custom_components/virtual/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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."
}
}
}
Expand Down

0 comments on commit 498e374

Please sign in to comment.